From: Colin Patrick McCabe Date: Mon, 25 Oct 2010 19:46:37 +0000 (-0700) Subject: Add time_sheet.rb X-Git-Url: http://club.cc.cmu.edu/~cmccabe/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=926e95af933e26a0a8ade0e19272c8e4ab48dd73;p=cmccabe-bin Add time_sheet.rb --- diff --git a/time_sheet.rb b/time_sheet.rb new file mode 100755 index 0000000..d222e12 --- /dev/null +++ b/time_sheet.rb @@ -0,0 +1,81 @@ +#!/usr/bin/ruby -w + +def pretty_time(min) + hours = min / 60 + hours = hours.floor + tmp = hours * 60 + min = min - tmp + return sprintf("%d:%02d", hours, min) +end + +punches = Array.new + +f = File.open(ARGV[0], "r") +f.each_line do |line| + next if (line =~ /^[\t ]*$/) + raise "can't parse line" unless (line =~ /^([0-9]*):([0-9][0-9])/) + hour = $1.to_i + min = $2.to_i +# puts "hour = #{hour}" +# puts "min = #{min}" + raise "invalid hour count of #{hour}" if (hour >= 24) + raise "invalid minutes count of #{min}" if (min >= 60) + total_min = (hour * 60) + min + punches << total_min +end + +prev = -1 +index = 0 +punches.each do |p| + index = index + 1 + if (p <= prev) then + raise "punch #{index} is not greater than the previous one" + end + prev = p +end + +total_clocked_time = 0 +total_break_time = 0 +prev = nil +state = :none +index = 0 +mandatory_breaks = [ 30, 15, 15 ] +mandatory_breaks = mandatory_breaks.sort! +mandatory_breaks = mandatory_breaks.reverse! +punches.each do |p| + index = index + 1 + case (state) + when :none then + state = :in + when :in then + inc = p - prev + puts "punch #{index-1} to #{index}:\t\t#{pretty_time(inc)}" + total_clocked_time = total_clocked_time + inc + state = :out + when :out + inc = p - prev + total_break_time = total_break_time + inc + state = :in + str = "[unknown break]" + mandatory_breaks.each do |b| + if (b <= inc) then + str = "[break #{b}]" + total_clocked_time = total_clocked_time + b + mandatory_breaks.shift + break + end + end + puts "#{str} #{index-1} to #{index}:\t#{pretty_time(inc)}" + end + + prev = p +end + +puts +puts "total_clocked_time: #{pretty_time(total_clocked_time)}" +if (total_clocked_time < (7 * 60)) then + puts "ERROR: total clocked time is less than 7 hours!" +end +unless (mandatory_breaks.size() == 0) then + puts "ERROR: not all mandatory breaks were taken! Still have to take: #{mandatory_breaks.join(" ")}" +end