Skip to content

Instantly share code, notes, and snippets.

@markryall
Created July 21, 2012 16:20
Show Gist options
  • Save markryall/3156310 to your computer and use it in GitHub Desktop.
Save markryall/3156310 to your computer and use it in GitHub Desktop.
time_report
#!/usr/bin/env ruby
require 'erb'
FromTo = Struct.new :from, :to
template = ERB.new <<EOF, nil, '%>'
<html>
<head></head>
<body>
% times.keys.sort.each do |name|
<h2><%= name %></h2>
<table border=1>
% total = 0
% times[name].each do |from_to|
<tr>
<td><%= date_format from_to.from %></td>
<td><%= date_format from_to.to %></td>
% duration = from_to.to - from_to.from
% total += duration
<td><%= duration / 60 %></td>
</tr>
% end
<tr>
<td /><td /><td><%= total / 60 %></td>
</tr>
</table>
% end
</body>
</html>
EOF
times = {}
line_expression = /^(.)(\d+)/
current = nil
File.open(File.expand_path('~')+'/.timing/timings.txt') do |f|
while line = f.gets
match = line_expression.match line.chomp
if match
direction, time, name = match[1], match[2].to_i, match.post_match
if direction == '+'
current = FromTo.new time
else
current = FromTo.new time unless current
current.to = time
times[name] ||= []
times[name] << current
current = nil
end
end
end
end
def date_format time
Time.at(time).strftime '%a %m/%d/%Y %I:%M %p'
end
puts template.result binding
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment