Add time_sheet.rb
authorColin Patrick McCabe <cmccabe@alumni.cmu.edu>
Mon, 25 Oct 2010 19:46:37 +0000 (12:46 -0700)
committerColin Patrick McCabe <cmccabe@alumni.cmu.edu>
Mon, 25 Oct 2010 19:47:00 +0000 (12:47 -0700)
time_sheet.rb [new file with mode: 0755]

diff --git a/time_sheet.rb b/time_sheet.rb
new file mode 100755 (executable)
index 0000000..d222e12
--- /dev/null
@@ -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