Skip to content

Instantly share code, notes, and snippets.

@trshafer
Created July 23, 2012 19:05
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 trshafer/3165471 to your computer and use it in GitHub Desktop.
Save trshafer/3165471 to your computer and use it in GitHub Desktop.
Humanized Seconds
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