Module: ChronicDuration

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

Defined Under Namespace

Classes: DurationParseError

Constant Summary collapse

FULL_WEEKS_PER_MONTH =

On average, there’s a little over 4 weeks in month.

4
VERSION =
'0.12.0'.freeze
@@raise_exceptions =
false
@@hours_per_day =
24
@@days_per_month =
30

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.days_per_monthObject



37
38
39
# File 'lib/gitlab_chronic_duration.rb', line 37

def self.days_per_month
  @@days_per_month
end

.days_per_month=(value) ⇒ Object



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

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

.hours_per_dayObject



29
30
31
# File 'lib/gitlab_chronic_duration.rb', line 29

def self.hours_per_day
  @@hours_per_day
end

.hours_per_day=(value) ⇒ Object



33
34
35
# File 'lib/gitlab_chronic_duration.rb', line 33

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

.raise_exceptionsObject



21
22
23
# File 'lib/gitlab_chronic_duration.rb', line 21

def self.raise_exceptions
  !!@@raise_exceptions
end

.raise_exceptions=(value) ⇒ Object



25
26
27
# File 'lib/gitlab_chronic_duration.rb', line 25

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



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
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/gitlab_chronic_duration.rb', line 55

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

  hours_per_day = opts[:hours_per_day] || ChronicDuration.hours_per_day
  days_per_month = opts[:days_per_month] || ChronicDuration.days_per_month
  days_per_week = days_per_month / FULL_WEEKS_PER_MONTH

  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 = hours_per_day * hour
  month = days_per_month * 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 >= hours_per_day
          days = (hours / hours_per_day).to_i
          hours = (hours % hours_per_day).to_i
          if opts[:weeks]
            if days >= days_per_week
              weeks = (days / days_per_week).to_i
              days = (days % days_per_week).to_i
              if weeks >= FULL_WEEKS_PER_MONTH
                months = (weeks / FULL_WEEKS_PER_MONTH).to_i
                weeks = (weeks % FULL_WEEKS_PER_MONTH).to_i
              end
            end
          else
            if days >= days_per_month
              months = (days / days_per_month).to_i
              days = (days % days_per_month).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)



48
49
50
51
# File 'lib/gitlab_chronic_duration.rb', line 48

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