Skip to content

Instantly share code, notes, and snippets.

@dkam
Last active June 1, 2016 12:04
Show Gist options
  • Save dkam/6ce1dd0f7ed55dc517fab53496b42e31 to your computer and use it in GitHub Desktop.
Save dkam/6ce1dd0f7ed55dc517fab53496b42e31 to your computer and use it in GitHub Desktop.
class Fixnum
def to_pretty_time
a = self
case a
when 0 then 'just now'
when 1 then 'a second ago'
when 2..59 then a.to_s+' seconds ago'
when 60..119 then 'a minute ago' #120 = 2 minutes
when 120..3540 then (a/60).to_i.to_s+' minutes ago'
when 3541..7100 then 'an hour ago' # 3600 = 1 hour
when 7101..82800 then ((a+99)/3600).to_i.to_s+' hours ago'
when 82801..172000 then 'a day ago' # 86400 = 1 day
when 172001..518400 then ((a+800)/(60*60*24)).to_i.to_s+' days ago'
when 518400..1036800 then 'a week ago'
else ((a+180000)/(60*60*24*7)).to_i.to_s+' weeks ago'
end
end
end
708.to_pretty_time
=> "11 minutes ago"
1708.to_pretty_time
=> "28 minutes ago"
41708.to_pretty_time
=> "11 hours ago"
141708.to_pretty_time
=> "a day ago"
1417038.to_pretty_time
=> "2 weeks ago"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment