Niclas Nilsson home

Weeks in iCal

It’s nice to be a programmer. Often when something computer related annoys you, you can create a fix for the problem.

People always talk to me about week numbers (week this, week that), and I’m no good with week numbers. The fact that iCal don’t know anything about week numbers doesn’t help either. Previous years, I’ve found calendars somewhere on the internet that I imported into iCal, but when we entered 2008, I didn’t find one fast enough. But that was easy to fix.

Seraching for Ruby gems, I found iCalendar, and some lines of Ruby later, the problem was solved.

The code is trivial (due to good gems), but here it is in case someone finds it useful.

require 'rubygems'
require 'icalendar'
require 'date'
require 'active_support'
include Icalendar

cal = Calendar.new

date = Date.parse("2007-12-31")
end_date = date + 5.years

while date < end_date
  cal.event do
    dtstart       date
    dtend         date + 1.week
    summary       "Week #{date.cweek}"
  end

  date += 1.week
end

puts cal.to_ical

It (obviously) outputs an iCal calendar containing week numbers from 2008 to 2012 to stdout, so run it like this:

ruby ical-weeks.rb > weeks.ics

It’s nice to be a programmer, but in case you’re an unlucky non-programmer who just wants a calendar file, or (worse?) a programmer without a Ruby interpreter on your machine, here’s the resulting file.

Import the file into iCal (File -> Import) and choose “New calendar” when you’re prompted which calendar to import it to.

(Of course, when I mailed the code to my colleagues, Lennart Ohlsson sent a quick reply with a link to a Google calendar that would have worked just as well. :-) But the good thing was that I found iCalendar, which I’ve used for other things afterwards, and will use again.)