Skip to content

Instantly share code, notes, and snippets.

@rietta
Created September 12, 2016 19:45
Show Gist options
  • Save rietta/1b90d088455b689212d762befd9f68e1 to your computer and use it in GitHub Desktop.
Save rietta/1b90d088455b689212d762befd9f68e1 to your computer and use it in GitHub Desktop.
Command line tool to convert hours into decimal notation suitable for invoices. 2:50 = 2.8 hours, etc.
#!/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