Skip to content

Instantly share code, notes, and snippets.

@l3kn
Created August 7, 2013 14:46
Show Gist options
  • Save l3kn/6174691 to your computer and use it in GitHub Desktop.
Save l3kn/6174691 to your computer and use it in GitHub Desktop.
Convert seconds to human readable string
def humanize(seconds)
units = [[60, 'sec'],[60, 'min'],[24, 'hour'],[365, 'day']]
time = []
units.each do |count, name|
if seconds > 0
seconds, rest = seconds.divmod(count)
time << "#{rest}#{name}#{rest > 1 ?'s':''}"
end
end
time.reverse.join(' ')
end
puts humanize(1337) # => 22mins 17secs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment