Integrating timezoned legacy dates into UTC based Rails application

July 23, 2009 by admin Leave a reply »

Ruby on Rails (RoR) has become one of industry’s fastest and the easiest web application framework to adapt to compared to some of the more mature ones such as J2EE, ASP.NET, etc.  So much so that other languages such as Perl and Python quickly starting spurring up their own web application frameworks that were modeled off of how Rails implemented the MVC pattern in such a pragmatic and simple manner.

As I was messing around with my first RoR project at work, we ran into an issue with incorrect rendering of Date/Time data retrieved from our legacy database.  After some research online, I realized that some incorrect decisions in our legacy data storage had finally crept up when the right approach was applied on the front-end.

The problem was that Rails stores all date values in UTC format in the data store and then displays a localized version based on the user’s locale in the front-end.  Unfortunately, the dates in our legacy data store were already being stored in Pacific time zone and so when rendering the date in the Rails based front-end application, an offset was being applied to the already offset date value resulting in an incorrect value.

Take a look at Rails 2.1 Time Zone Support: An Overview for a good overview on Rails time zone support.

So…Now what?

Well, I knew that we were constraint by the legacy data and because other legacy apps were already relying on the data stored as is, we cannot fix the problem on the data side by updating all date/time values using SQL.   Plus, these incorrect date/time values were spread out throughout the database in various tables making it very difficult and fairly easy to miss some value in some table.  We also needed to keep this date/time storage decision intact when updating data from our Rails app which means somehow we’d have to make Rails not store the date/time values for those specific dates in UTC again for any legacy apps consuming the same data.

But how do we do this by not mucking with Rails internals especially since this UTC conversion took place at the DB adapter levels?  The answer is a Rails plugin.

Rails Plugin

More on this soon…I promise!

Leave a Reply