Makefile: add pickrand
[cmccabe-bin] / time_sheet.rb
1 #!/usr/bin/ruby -w
2
3 def pretty_time(min)
4   hours = min / 60
5   hours = hours.floor
6   tmp = hours * 60
7   min = min - tmp
8   return sprintf("%d:%02d", hours, min)
9 end
10
11 punches = Array.new
12
13 f = File.open(ARGV[0], "r")
14 f.each_line do |line|
15   next if (line =~ /^[\t ]*$/)
16   raise "can't parse line" unless (line =~ /^([0-9]*):([0-9][0-9])/)
17   hour = $1.to_i
18   min = $2.to_i
19 #  puts "hour = #{hour}"
20 #  puts "min = #{min}"
21   raise "invalid hour count of #{hour}" if (hour >= 24)
22   raise "invalid minutes count of #{min}" if (min >= 60)
23   total_min = (hour * 60) + min
24   punches << total_min
25 end
26
27 prev = -1
28 index = 0
29 punches.each do |p|
30   index = index + 1
31   if (p <= prev) then
32     raise "punch #{index} is not greater than the previous one"
33   end
34   prev = p
35 end
36
37 total_clocked_time = 0
38 total_break_time = 0
39 prev = nil
40 state = :none
41 index = 0
42 mandatory_breaks = [ 30, 15, 15 ]
43 mandatory_breaks = mandatory_breaks.sort!
44 mandatory_breaks = mandatory_breaks.reverse!
45 punches.each do |p|
46   index = index + 1
47   case (state)
48     when :none then
49       state = :in
50     when :in then
51       inc = p - prev
52       puts "punch #{index-1} to #{index}:\t\t#{pretty_time(inc)}"
53       total_clocked_time = total_clocked_time + inc
54       state = :out
55     when :out
56       inc = p - prev
57       total_break_time = total_break_time + inc
58       state = :in
59       str = "[unknown break]"
60       mandatory_breaks.each do |b|
61         if (b <= inc) then
62           str = "[break #{b}]"
63           total_clocked_time = total_clocked_time + b
64           mandatory_breaks.shift
65           break
66         end
67       end
68       puts "#{str} #{index-1} to #{index}:\t#{pretty_time(inc)}"
69   end
70
71   prev = p
72 end
73
74 puts
75 puts "total_clocked_time: #{pretty_time(total_clocked_time)}"
76 if (total_clocked_time < (7 * 60)) then
77   puts "ERROR: total clocked time is less than 7 hours!"
78 end
79 unless (mandatory_breaks.size() == 0) then
80   puts "ERROR: not all mandatory breaks were taken! Still have to take: #{mandatory_breaks.join(" ")}"
81 end