15 October 2008
Here is a simple routine that returns a nice friendly elapsed time message; just like the ones you see in all those fancy social networking sites & twitter.
def elapsed_time(start_time)
diff = Time.now - start_time
case diff
when 0..9
'just a moment ago'
when 10..40
'less than ' + (diff * 1.5).to_i.to_s.slice(0,1) + '0 seconds ago'
when 41..60
'less than a minute ago'
when 60..120
"1 minute ago"
when 120..3540
"#{(diff / 60).to_i} minutes ago"
when 3540..5400 then
'about 1 hour ago'
when 54010..86400 then
"about #{(diff / 60 / 60 * 1.02).to_i} hours ago"
else
start_time.strftime("%H:%M %p %B %d, %Y")
end
end

Trackback URL