Twitter icon.
Random notes on software, programming and languages.
By Adrian Kuhn

How do I track time?


I have a ruby script that appends a line with timestamp and all commandline arguments to a text file. Then I have an alias from “I” to this script, such that I can just type on the command line…

% I start working
0d 14:06'11"
% I have read mail
0d 00:27'45"
% I answered a question on stackoverflow.com
0d 00:11'32"
%

…and all that is recorded in a text file with timestamps.

Other scripts later use that text file to generate work reports, or just tag clouds of what I have done during the last weeks or months.

require 'date'
File.open('time.txt', 'a') do |f|
  f.puts "#{DateTime.now.strftime("%Y/%m/%d\t%H:%M:%S\t")}#{$*.join(' ')}"
end unless $*.empty?
back = DateTime.new
now = DateTime.new
File.open('time.txt') do |f|
  f.each do |line|
    unless line.strip.empty? then
      back = now
      now = DateTime.strptime(line,"%Y/%m/%d\t%H:%M:%S\t")
    end
  end
end
if $*.empty? then
  back = now
  now = DateTime.now.new_offset(of=0) + DateTime.now.offset # fix timezone!
end
diff = (now - back)
puts "#{diff.to_i}d #{(DateTime.new + (diff - diff.to_i)).strftime("%H:%M'%S\"")}"

2 Responses to “How do I track time?”

  1. Mircea Says:

    I like this :)

  2. For.example » Blog Archive » How do I track time? II Says:

    [...] Recently, I showed how to use the command line and a ruby script to keep track of time. Due to popular demand, here is the full script (including time cloud generation). [...]

For.example is Digg proof thanks to caching by WP Super Cache!