Thursday, October 25, 2007

A note on UTC support in Rails

I was doing some work on Bitswiki yesterday, and figured it would be a good idea to store time data in UTC. I thought this was as easy as uncommenting the "config.active_record.default_timezone = :utc" line in environment.rb, but this was not the case. The models' created_at fields still got populated with my local timezone, even tho Rails claimed it was in UTC. I was quite puzzled. It turned out that it was as easy as adding this to environment.rb (or into its own file in /config/initializers):


# Make Time.now return time in UTC
ENV['TZ'] = 'UTC'


Maybe this should have been set implicitly by the default_timezone setting, as you probably want the two in conjunction anyway?

No comments: