Skip to content

Instantly share code, notes, and snippets.

@aerickson
Created March 30, 2011 08:33
Show Gist options
  • Star 11 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aerickson/894068 to your computer and use it in GitHub Desktop.
Save aerickson/894068 to your computer and use it in GitHub Desktop.
def humanize secs
[[60, :seconds], [60, :minutes], [24, :hours], [1000, :days]].map{ |count, name|
if secs > 0
secs, n = secs.divmod(count)
"#{n.to_i} #{name}"
end
}.compact.reverse.join(' ')
end
@aerickson
Copy link
Author

for milliseconds

def humanize millis
  [[1000, :milliseconds], [60, :seconds], [60, :minutes], [24, :hours], [1000, :days]].map{ |count, name|
    if millis > 0
      millis, n = millis.divmod(count)
      "#{n.to_i} #{name}"
    end
  }.compact.reverse.join(' ')
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment