Skip to content

Instantly share code, notes, and snippets.

@eladmeidar
Forked from ik5/time_duration.rb
Last active December 22, 2015 20:19
Show Gist options
  • Save eladmeidar/6525567 to your computer and use it in GitHub Desktop.
Save eladmeidar/6525567 to your computer and use it in GitHub Desktop.
# timeit { sleep 20 }
# => {:weeks=>0, :days=>0, :hours=>0, :minutes=>0, :seconds=>20}
def timeit(&block)
mul = [604800, 86400, 3600, 60, 1]
values = [:weeks, :days, :hours, :minutes, :seconds]
units = []
started = Time.now.to_i
yield block
ended = Time.now.to_i
total = (ended - started).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