Skip to content

Instantly share code, notes, and snippets.

@krainboltgreene
Created August 31, 2010 11:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save krainboltgreene/558899 to your computer and use it in GitHub Desktop.
Save krainboltgreene/558899 to your computer and use it in GitHub Desktop.
def smallest_unit_of_time(before)
seconds = Time.now.to_i - before.to_i
minute = seconds / 60; hour = minute / 60; day = hour / 24
week = day / 7; month = day / 29; quarter = month / 3; year = quarter / 4
decade = year / 10; century = decade / 10; millennium = century / 10
case
when millennium > 0 then "#{millennium} millennium" + "#{'s' if millennium > 1}"
when century > 0 then "#{century} centur" + "#{'ies' ? century > 1 : 'y'}"
when decade > 0 then "#{decade} decade" + "#{'s' ? decade > 1}"
when year > 0 then "#{year} year" + "#{'s' ? year > 1}"
when quarter > 0 then "#{quarter} quarter" + "#{'s' ? quarter > 1}"
when month > 0 then "#{month} month" + "#{'s' ? month > 1}"
when week > 0 then "#{week} week" + "#{'s' ? week > 1}"
when day > 0 then "#{day} day" + "#{'s' ? day > 1}"
when hour > 0 then "#{hour} hour" + "#{'s' ? hour > 1}"
when minute > 0 then "#{minute} minute#{'s' ? minute > 1}"
else "#{seconds} second" + "#{'s' ? seconds > 1 || seconds == 0}"
end
end
@frostbytten
Copy link

when century > 0 then "#{century} centur"+"{# century > 1 ? 'ies' : 'y'}"

@krainboltgreene
Copy link
Author

@frostbytten: Good catch!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment