Last active
October 13, 2015 03:28
-
-
Save danielnc/4132332 to your computer and use it in GitHub Desktop.
humanize milliseconds (ruby)
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 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