Adventures into CouchDB and Rails

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.

Symptoms

Here is the failing RSpec example:

1)
'CouchSurfer::Model a model with timestamps should set the time on create' FAILED
expected: < 2,
     got:   3600.004118
./spec/lib/model_spec.rb:532:

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.

Evidence

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

$ irb
irb(main):001:0> ENV['TZ'] = 'US/Central'
=> "US/Central"
irb(main):002:0> Time.now
=> Wed Feb 04 09:12:34 -0600 2009
irb(main):003:0> Time.now.strftime("%z")
=> "-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"
irb(main):002:0> Time.now
=> Wed Feb 04 09:15:19 -0600 2009
irb(main):003:0> Time.now.strftime("%z")
=> "-0500"

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

Solution

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.

Advertisements

1 Comment »

  1. […] is related to my previous blog post, in which I found a similar bug in the serialization of CouchSurfer::Model […]

    Pingback by Rails Time Zone Bug — An Edge Case « Adventures into CouchDB and Rails — February 6, 2009 @ 1:02 am


RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a free website or blog at WordPress.com.

%d bloggers like this: