Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.