Created
July 23, 2012 19:05
-
-
Save trshafer/3165471 to your computer and use it in GitHub Desktop.
Humanized Seconds
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def humanized_seconds secs | |
[[60, :second], [60, :minute], [24, :hour], [1000, :day]].map{ |count, name| | |
name = name.to_s | |
if secs > 0 | |
secs, n = secs.divmod(count) | |
pluralized_name = n == 1 ? name : name.pluralize | |
"#{n.to_i} #{pluralized_name}" | |
end | |
}.compact.reverse.join(' ') | |
end | |
# humanized_seconds 29485 | |
# => 8 hours 11 minutes 25 seconds | |
# http://stackoverflow.com/questions/4136248/how-to-generate-a-human-readable-time-range-using-ruby-on-rails |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment