Last active
December 20, 2015 04:39
-
-
Save ik5/6072075 to your computer and use it in GitHub Desktop.
simple way to calculate time duration based on ruby-duration
This file contains hidden or 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
# 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