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
|
# File 'lib/resque/server.rb', line 121
def distance_of_time_in_words(from_time, to_time = 0, include_seconds = true, options = {})
from_time = from_time.to_time if from_time.respond_to?(:to_time)
to_time = to_time.to_time if to_time.respond_to?(:to_time)
distance_in_minutes = (((to_time - from_time).abs)/60).round
distance_in_seconds = ((to_time - from_time).abs).round
ago = from_time > to_time ? ' ago' : ''
case distance_in_minutes
when 0..1
return distance_in_minutes == 0 ?
"less than 1 minute" + ago :
"#{distance_in_minutes} minutes" + ago unless include_seconds
case distance_in_seconds
when 0..4 then "less than 5 seconds" + ago
when 5..9 then "less than 10 seconds" + ago
when 10..19 then "less than 20 seconds" + ago
when 20..39 then "half a minute" + ago
when 40..59 then "less than 1 minute" + ago
else "1 minute" + ago
end
when 2..44 then "#{distance_in_minutes} minutes" + ago
when 45..89 then "about 1 hour" + ago
when 90..1439 then "about #{(distance_in_minutes.to_f / 60.0).round} hours" + ago
when 1440..2529 then "about 1 day" + ago
when 2530..43199 then "about #{(distance_in_minutes.to_f / 1440.0).round} days" + ago
when 43200..86399 then "about 1 month" + ago
when 86400..525599 then "about #{(distance_in_minutes.to_f / 43200.0).round} months" + ago
else
distance_in_years = distance_in_minutes / 525600
minute_offset_for_leap_year = (distance_in_years / 4) * 1440
remainder = ((distance_in_minutes - minute_offset_for_leap_year) % 525600)
if remainder < 131400
"about #{distance_in_years} years" + ago
elsif remainder < 394200
"over #{distance_in_years} years" + ago
else
"almost #{distance_in_years} years" + ago
end
end
end
|