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
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|
divider = ':'
str.split(divider).map { |n|
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
|