Skip to content

Instantly share code, notes, and snippets.

@gmgent
Created March 8, 2011 18:36
Show Gist options
  • Save gmgent/860734 to your computer and use it in GitHub Desktop.
Save gmgent/860734 to your computer and use it in GitHub Desktop.
class Mod
def self.ago(seconds)
case true
when seconds.to_i < 60
s = seconds.to_i
return "#{s} second#{s>1 ? "s" : ""} ago."
when seconds.to_i < 3600
m = seconds.to_i/60
s = seconds.to_i%60
return "#{m} minute#{m>1 ? "s" : ""}, #{s} second#{s>1 ? "s" : ""} ago."
when seconds.to_i < (3600 * 24)
h = seconds.to_i/3600
m = (seconds.to_i/60)%60
s = seconds.to_i%60
return "#{h} hour#{h>1 ? "s" : ""}, #{m} minute#{m>1 ? "s" : ""}, #{s} second#{s>1 ? "s" : ""} ago."
else
d = seconds.to_i / (3600 * 24)
h = (seconds.to_i/3600)%24
m = (seconds.to_i/60)%60
s = seconds.to_i%60
return "#{d} day#{d>1 ? "s" : ""}, #{h} hour#{h>1 ? "s" : ""}, #{m} minute#{m>1 ? "s" : ""}, #{s} second#{s>1 ? "s" : ""} ago."
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment