Adventures into CouchDB and Rails

February 6, 2009

Rails Time Zone Bug — An Edge Case

Filed under: Uncategorized — zdzolton @ 1:02 am


I have found a Mac OS X-specific edge case in the Rails time zone support.


irb(main):001:0> require 'rubygems'; require 'activesupport'
=> true
irb(main):002:0> ENV['TZ'] = 'US/Central'
=> "US/Central"
irb(main):003:0> t =
=> Thu Feb 05 16:03:56 -0600 2009
irb(main):004:0> t.to_s :json
=> "Thu Feb 05 16:03:56 -0600 2009"
irb(main):005:0> t.to_s :rfc822
=> "Thu, 05 Feb 2009 16:03:56 -0500"

Notice that when invoking Time#to_s method, and providing the parameter :rfc822, the UTC


A small modification to the :rfc822 format specifier in DATE_FORMATS hash, defined within the ActiveSupport::CoreExtensions::Time::Conversions module, does the trick.

Specifically, line #13 of activesupport/lib/active_support/core_ext/time/conversions.rb:

          :rfc822       => "%a, %d %b %Y %H:%M:%S %z"

Should be changed to this:

          :rfc822       => lambda { |time| time.strftime("%a, %d %b %Y %H:%M:%S #{time.formatted_offset(false)}") } 


This is related to my previous blog post, in which I found a similar bug in the serialization of CouchSurfer::Model timestamps.

I am currently testing this on Mac OS X 10.5.6.


February 4, 2009

CouchSurfer – Timezone Problems

Filed under: Uncategorized — zdzolton @ 3:37 pm

Problem Definition

There seems to be a timezone-related bug for the CouchSurfer::Model::ClassMethods.timestamps! method.


Here is the failing RSpec example:

'CouchSurfer::Model a model with timestamps should set the time on create' FAILED
expected: < 2,
     got:   3600.004118

Finished in 13.386698 seconds

107 examples, 1 failure

Root Cause

The problem seems to lie in line #158:

self['updated_at'] = time.strftime("%Y/%m/%d %H:%M:%S.#{time.usec} %z")

In particular, the %z used in the format specifier, performs unreliably across operating systems.


When executing this on an Ubuntu server to which I have access:

$ irb
irb(main):001:0> ENV['TZ'] = 'US/Central'
=> "US/Central"
=> Wed Feb 04 09:12:34 -0600 2009
=> "-0600"
Now, when I execute this code on my local Mac OS X 10.5.6 machine:
$ irb
irb(main):001:0> ENV['TZ'] = 'US/Central'
=> "US/Central"
=> Wed Feb 04 09:15:19 -0600 2009
=> "-0500"

Notice how the %z resulted in “-0500” on Mac OS X, instead of “-0600” as it should have.


I added a helper method to CouchSurfer::Model, named format_utc_offset, that returns a the correct UTC offset string. This was basically adapted from the ActiveSupport Time#formatted_offset code in Rails.

You can see the changes made in my fork.

Create a free website or blog at