My rails 2.3 app is interpreting times retrieved from the database as
local (in my case, Eastern) instead of UTC. I created a test table in
a SQL Server database with one row. The table is called events and
contains an “edate” column. The value is “2009-03-30 12:00”, and is a
config.time_zone = ‘UTC’
The output from script/console is below. (results are identical to
using a browser)
It looks like rails is assuming the database value is really a local
time, and it adds four hours to get UTC. But that is wrong, since the
time is already UTC.
What else do I need to do to convince rails to leave the time value
alone, since it is a UTC value?
I can get the correct value by extracting the “Time” with no zone, and
then recreating a new TimeWithZone. But that can’t be the best way!
Loading development environment (Rails 2.3.2)
=> #<ActiveSupport::TimeZone:0xb795cbc8 @tzinfo=nil, @utc_offset=0,
=> #<Event id: 1, edate: “2009-03-30 12:00:00”>
=> Mon, 30 Mar 2009 16:00:00 UTC +00:00
@tzinfo=#<TZInfo::DataTimezone: Etc/UTC>, @utc_offset=0, @name=“UTC”>
correct_time = ActiveSupport::TimeWithZone.new(event.edate.localtime,zutc)
=> Mon, 30 Mar 2009 12:00:00 UTC +00:00