Darwinweb

Ruby Time Tracker

November 9, 2008     

There’s a lot of time tracking software out there. Web-based, commercial and shareware. I found one desktop time tracker that cost $50 and that’s all it does! In reality the most efficient system I’ve ever used is writing down times and tasks on paper. However maybe a little automation is in order. Here’s a script I wrote in 20 minutes (maybe 21 minutes, but it wasn’t done yet so I can’t be sure). It does what I need, which isn’t much.

def format_seconds(seconds)
  seconds = seconds.to_i
  minutes = seconds / 60
  seconds = seconds % 60
  hours = minutes / 60
  minutes = minutes % 60
  sprintf("%2d:%02d:%02d", hours, minutes, seconds)
end

puts "=== Time Tracker ==="
print "What are you working on? "
input = task_name = $stdin.readline.chomp
start_time = Time.now
tasks = []
intermediate_seconds = 0
while input != ''
  print "Enter new task (or 'pause', return to quit): "
  input = $stdin.readline.chomp
  if input == 'pause'
    pause_time = Time.now
    print "(Paused, hit any key to resume)"
    $stdin.readline.chomp
    intermediate_seconds += pause_time - start_time
    start_time = Time.now
  else
    intermediate_seconds += Time.now - start_time
    tasks << format_seconds(intermediate_seconds) + " " + task_name + " (" + Time.now.strftime('%b %d %H:%M') + ")"
    intermediate_seconds = 0
    start_time = Time.now
    task_name = input
  end
end

puts tasks

Geez I need a syntax highlighter on here.