Created
August 3, 2012 18:07
-
-
Save dpoggi/3250081 to your computer and use it in GitHub Desktop.
YAML Hours Tracker
This file contains 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
#!/usr/bin/env ruby | |
require 'date' | |
require 'yaml' | |
class Float | |
def nearest_half | |
(self * 2.0).ceil / 2.0 | |
end | |
end | |
class String | |
def time_range_to_f | |
parts = self.split('-') | |
start = DateTime.parse(parts.first) | |
finish = DateTime.parse(parts.last) | |
(finish - start).to_f * 24.0 | |
end | |
end | |
if ARGV.size < 1 | |
$stderr.puts "Usage: #{__FILE__} <month>.yml" | |
exit 1 | |
end | |
month = ARGV.first.split('.')[0..-2].join.capitalize | |
puts "Hours for the month of #{month}:" | |
puts | |
YAML.load_file(ARGV.first).each do |date, hours| | |
total = hours.map(&:time_range_to_f).reduce(&:+).nearest_half | |
puts "#{month} #{date}: #{total} hours" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment