Module: ChronicDuration

Extended by:
ChronicDuration
Included in:
ChronicDuration
Defined in:
lib/chronic_duration.rb,
lib/chronic_duration/version.rb

Defined Under Namespace

Classes: DurationParseError

Constant Summary collapse

VERSION =
'0.10.6'
@@raise_exceptions =
false
@@hours_per_day =
24
@@days_per_week =
7

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.days_per_weekObject


30
31
32
# File 'lib/chronic_duration.rb', line 30

def self.days_per_week
  @@days_per_week
end

.days_per_week=(value) ⇒ Object


34
35
36
# File 'lib/chronic_duration.rb', line 34

def self.days_per_week=(value)
  @@days_per_week = value
end

.hours_per_dayObject


22
23
24
# File 'lib/chronic_duration.rb', line 22

def self.hours_per_day
  @@hours_per_day
end

.hours_per_day=(value) ⇒ Object


26
27
28
# File 'lib/chronic_duration.rb', line 26

def self.hours_per_day=(value)
  @@hours_per_day = value
end

.raise_exceptionsObject


14
15
16
# File 'lib/chronic_duration.rb', line 14

def self.raise_exceptions
  !!@@raise_exceptions
end

.raise_exceptions=(value) ⇒ Object


18
19
20
# File 'lib/chronic_duration.rb', line 18

def self.raise_exceptions=(value)
  @@raise_exceptions = !!value
end

Instance Method Details

#output(seconds, opts = {}) ⇒ Object

Given an integer and an optional format, returns a formatted string representing elapsed time


48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/chronic_duration.rb', line 48

def output(seconds, opts = {})
  int = seconds.to_i
  seconds = int if seconds - int == 0 # if seconds end with .0

  opts[:format] ||= :default
  opts[:keep_zero] ||= false

  years = months = weeks = days = hours = minutes = 0

  decimal_places = seconds.to_s.split('.').last.length if seconds.is_a?(Float)

  minute = 60
  hour = 60 * minute
  day = ChronicDuration.hours_per_day * hour
  month = 30 * day
  year = 31557600

  if seconds >= 31557600 && seconds%year < seconds%month
    years = seconds / year
    months = seconds % year / month
    days = seconds % year % month / day
    hours = seconds % year % month % day / hour
    minutes = seconds % year % month % day % hour / minute
    seconds = seconds % year % month % day % hour % minute
  elsif seconds >= 60
    minutes = (seconds / 60).to_i
    seconds = seconds % 60
    if minutes >= 60
      hours = (minutes / 60).to_i
      minutes = (minutes % 60).to_i
      if !opts[:limit_to_hours]
        if hours >= ChronicDuration.hours_per_day
          days = (hours / ChronicDuration.hours_per_day).to_i
          hours = (hours % ChronicDuration.hours_per_day).to_i
          if opts[:weeks]
            if days >= ChronicDuration.days_per_week
              weeks = (days / ChronicDuration.days_per_week).to_i
              days = (days % ChronicDuration.days_per_week).to_i
              if weeks >= 4
                months = (weeks / 4).to_i
                weeks = (weeks % 4).to_i
              end
            end
          else
            if days >= 30
              months = (days / 30).to_i
              days = (days % 30).to_i
            end
          end
        end
      end
    end
  end

  joiner = opts.fetch(:joiner) { ' ' }
  process = nil

  case opts[:format]
  when :micro
    dividers = {
      :years => 'y', :months => 'mo', :weeks => 'w', :days => 'd', :hours => 'h', :minutes => 'm', :seconds => 's' }
    joiner = ''
  when :short
    dividers = {
      :years => 'y', :months => 'mo', :weeks => 'w', :days => 'd', :hours => 'h', :minutes => 'm', :seconds => 's' }
  when :default
    dividers = {
      :years => ' yr', :months => ' mo', :weeks => ' wk', :days => ' day', :hours => ' hr', :minutes => ' min', :seconds => ' sec',
      :pluralize => true }
  when :long
    dividers = {
      :years => ' year', :months => ' month', :weeks => ' week', :days => ' day', :hours => ' hour', :minutes => ' minute', :seconds => ' second',
      :pluralize => true }
  when :chrono
    dividers = {
      :years => ':', :months => ':', :weeks => ':', :days => ':', :hours => ':', :minutes => ':', :seconds => ':', :keep_zero => true }
    process = lambda do |str|
      # Pad zeros
      # Get rid of lead off times if they are zero
      # Get rid of lead off zero
      # Get rid of trailing :
      divider = ':'
      str.split(divider).map { |n|
        # add zeros only if n is an integer
        n.include?('.') ? ("%04.#{decimal_places}f" % n) : ("%02d" % n)
      }.join(divider).gsub(/^(00:)+/, '').gsub(/^0/, '').gsub(/:$/, '')
    end
    joiner = ''
  end

  result = [:years, :months, :weeks, :days, :hours, :minutes, :seconds].map do |t|
    next if t == :weeks && !opts[:weeks]
    num = eval(t.to_s)
    num = ("%.#{decimal_places}f" % num) if num.is_a?(Float) && t == :seconds
    keep_zero = dividers[:keep_zero]
    keep_zero ||= opts[:keep_zero] if t == :seconds
    humanize_time_unit( num, dividers[t], dividers[:pluralize], keep_zero )
  end.compact!

  result = result[0...opts[:units]] if opts[:units]

  result = result.join(joiner)

  if process
    result = process.call(result)
  end

  result.length == 0 ? nil : result

end

#parse(string, opts = {}) ⇒ Object

Given a string representation of elapsed time, return an integer (or float, if fractions of a second are input)


41
42
43
44
# File 'lib/chronic_duration.rb', line 41

def parse(string, opts = {})
  result = calculate_from_words(cleanup(string), opts)
  (!opts[:keep_zero] and result == 0) ? nil : result
end