Skip to content

Instantly share code, notes, and snippets.

@danielnc
Last active October 13, 2015 03:28
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 danielnc/4132332 to your computer and use it in GitHub Desktop.
Save danielnc/4132332 to your computer and use it in GitHub Desktop.
humanize milliseconds (ruby)
def humanize(milliseconds)
[[1000, :milliseconds], [60, :seconds], [60, :minutes], [24, :hours], [365, :days], [+1.0/0.0, :years]].inject([]){ |m, (count, name)|
if milliseconds > 0
milliseconds, n = milliseconds.divmod(count)
m.unshift "#{n.to_i} #{name}"
end
m
}.join(' ')
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment