Skip to content

Instantly share code, notes, and snippets.

@ik5
Last active December 20, 2015 04:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ik5/6072075 to your computer and use it in GitHub Desktop.
Save ik5/6072075 to your computer and use it in GitHub Desktop.
simple way to calculate time duration based on ruby-duration
# start = Time.now
# sleep 20
# finish = Time.now
# calc(finish - start) => [0, 0, 0, 0, 20]
# [weeks, days, hours, minutes, seconds]
#
def calc(seconds)
# weeks days hours min s
mul = [604800, 86400, 3600, 60, 1]
values = [:weeks, :days, :hours, :minutes, :seconds]
units = []
total = seconds.to_f.round
mul.inject(total) do |t, m|
units << t / m
t % m
end
Hash[values.zip(units)]
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment