Posts for July, 2005

CalendarGrid 1.0.1

CalendarGrid 1.0.1 is a major bugfix to 1.0. Please upgrade now. If you tried 1.0 and were utterly disappointed, I apologize.

Note to Rails users. Never ever rely on the nifty date math methods for real date calculation. Try this:

Time.local(2005,10,30) + 1.day
=> Sun Oct 30 23:00:00 PST 2005

Um, what?

1.day
=> 86400
Time.local(2005,10,30) - Time.local(2005,10,29)
=> 86400.0
Time.local(2005,10,31) - Time.local(2005,10,30)
=> 90000.0

Lesson learned. Be careful with dates. This all came about because I switched from using Date to Time for everything internal.

Just in case anyone doesn't know how slow (but accurate!) Date is...

bm do |x|
  x.report("Date.today") do 
    1000.times { Date.today }
  end
  x.report("Time.now") do 
    1000.times { Time.now }
  end
end
            user       system     total       real
Date.today  0.420000   0.010000   0.430000 (  0.452072)
Time.now    0.000000   0.000000   0.000000 (  0.003978)

Thanks to Ben Bleything for the heads up!

CalendarGrid 1.0

I'm pleased to announce my first open source project, called CalendarGrid.

CalendarGrid lets you build output like a calendar, with padding days at the beginning and end of months. It sports a flexible data structure so you can format it any way you want. You can also provide a 'plugin' to each day to give it extra knowledge. For example, this code was originally created for a booking system, so each day knew whether it was booked or not.

Find out more in the API docs-grid.rubyforge.org/.

Installation

Install with gems - gem install calendar_grid

Download from the official home at RubyForge

Sample usage

require 'calendar_grid'

cal = CalendarGrid.build
puts "Calendar from #{cal.first.strftime("%D")} to #{cal.last.strftime("%D")}"

cal.years.each do |y|
  puts y.year
  y.months.each do |m|
    puts m.strftime("%B")
    s = m.weeks.collect do |w|
      w.collect { |day| "#{(day.proxy?) ? '* ' : day.strftime("%d")}" }.join(" ")
    end
    puts s
    puts
  end
end

Output

Calendar from 06/01/05 to 05/31/06
2005

June
*  *  01 02 03 04 05
06 07 08 09 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 *  *  * 

...

Thanks to Graham Arrowsmith for letting me build it, and then release it to the community.