Skip to content

Instantly share code, notes, and snippets.

@rietta

rietta/hours.rb

Created Nov 27, 2017
Embed
What would you like to do?
Keep a copy in your ~/bin and chmod to 755. Now, you can compute nicely rounded hours at will from the command line.
#!/usr/bin/env ruby
##
# Convert hours to invoice time, which is rounded to 6 minute increments.
time_value = ARGV.last.to_s.strip
if time_value =~ /\A[0-9]*:[0-9]*\Z/
time_elements = time_value.split(':')
minutes = time_elements.first.to_i * 60.0 + 1.0 * time_elements.last.to_f
elsif time_value.to_f > 0.0
minutes = time_value.to_f * 60.0
else
STDERR.puts "Usage: hours 2:50 or hours 2.534 or hours 2"
exit 1;
end
hours = (minutes / 60.0).round(1)
puts hours
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment