Skip to content

Instantly share code, notes, and snippets.

@mindware
Last active October 9, 2015 14:09
Show Gist options
  • Save mindware/0ef21c32d73999df9c12 to your computer and use it in GitHub Desktop.
Save mindware/0ef21c32d73999df9c12 to your computer and use it in GitHub Desktop.
Ruby
# expects seconds, returns pretty string of how long ago event happened
def ago(seconds)
a = seconds
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
# expects seconds, returns pretty string of expected time
def time_from_now(a)
# where a is seconds
case a
when -1 then "never"
when 0 then "just now"
when 1 then "in one second"
when 2..59 then "in #{a.to_s} seconds"
when 60..119 then "in a minute" #120 = 2 minutes
when 120..3540 then "in #{(a/60).to_i.to_s} minutes"
when 3541..7100 then "in an hour" # 3600 = 1 hour
when 7101..82800 then "in #{((a+99)/3600).to_i.to_s} hours"
when 82801..172000 then "in one day" # 86400 = 1 day
when 172001..518400 then "in #{((a+800)/(60*60*24)).to_i.to_s} days"
when 518401..1036800 then "in a week"
when 1036801..2433600 then "in #{((a+180000)/(60*60*24*7)).to_i.to_s} weeks"
else "in #{((a+180000)/(60*60*24*7) / 4).to_i.to_s} month(s)"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment