Fun With Time Zones Rails 2.3.8

Hello everyone,

A user enters the time of an event and the time zone of the event. Before validation in the model, I set the zone of the time entered to the one chosen (only changing the zone, not the time) = time_zone

extending ActiveSupport thus …

module ActiveSupport

class TimeWithZone

def zone=(new_zone =

Reinitialize with the new zone and the local time

initialize(nil, ::Time.send(:get_zone, new_zone), time)




and that gets saved nicely in the DB as UTC.

Upon editing that record, I keep getting different time zones being displayed and they change upon page refresh or a new request.

Here is an example of the output after a refresh …

Here are the relevant fields after being retrieved in the edit action @event.to_yaml


starts_at: 2011-07-08 17:00:00

invitation_expiry: 2011-07-08 16:50:00

time_zone: Pacific Time (US & Canada)


starts_at: 2011-07-08 17:00:00 Z

invitation_expiry: 2011-07-08 16:50:00 Z

And here’s some simple debug output from the relevant requests

logger.debug # → PDT

logger.debug @event.starts_at # → 2011-07-08 10:00:00 -0700

logger.debug @event.starts_at.in_time_zone(@event.time_zone) # → 2011-07-08 10:00:00 -0700

logger.debug @event.starts_at.in_time_zone(‘UTC’) # → 2011-07-08 17:00:00 UTC

logger.debug # → UTC

logger.debug @event.starts_at # → 2011-07-08 17:00:00 UTC

logger.debug @event.starts_at.in_time_zone(@event.time_zone) # → 2011-07-08 10:00:00 -0700

logger.debug @event.starts_at.in_time_zone(‘UTC’) # → 2011-07-08 17:00:00 UTC

Now, I thought Rails would do all the magic and convert my UTC time (from the DB) in to the time zone that I set. As you can see, it does sometimes but as it’s random, it’s obviously not doing what I thought it would.

This takes me back to my mod_perl days of sometimes it works, sometimes it doesn’t.

Does anyone have any insight in to this, please.

Any pointers would be great. Thanks