How to use Google Calendar and Rufus-Google for Basic Time Tracking

November 27, 2009

Today I’ll share what I use for some of my DIY time-tracking when I do freelancing (sometimes I use other tools, depending on the customer).

Conventions I currently use:

  • one Google Calendar per customer (I name these “LoGeek CustomerName” to avoid conflicts with other calendars I would have shared with my customer)
  • event title stores the project name

Code

require 'rufus/gcal'

customer = "LoGeek Acme Company" 

calendars = Rufus::Google::Calendar.get_calendars(
  :account => user_name, :password => password)

calendar = calendars[customer]

# don't keep the default (low) :max_results
# or you'll probably miss events
events = calendar.events(
  "max_results" => 1000, 
  "start_min" => '2009-09-09T00:00:00',
  "start_max" => '2009-11-25T11:00:00')

events.each do |e|
  duration = (e.end_time - e.start_time) / 3600.0
  raise "corrupt event" if duration > 8

  print e.title # project name
  print " - " 
  print e.start_time.strftime(...)
  print e.end_time.strftime(...)
  print " - " 
  print duration
  print " hours" 
  puts

  total += duration
end

Once you have this data, you can either export it as CSV, or reinject it into Prawn or Documatic to create an invoice.

Resources

Known dependencies:

Thanks to John Mettraux for creating these useful libraries.