Forum: Ruby on Rails TimeWithZone seems in rails 2.3 seems this the correct behavior?

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
0c51049fb747cd60088e4878458e05d1?d=identicon&s=25 Rick Schumeyer (Guest)
on 2009-03-31 16:09
(Received via mailing list)
My environment.rb contains
config.time_zone = 'UTC'

If my understanding is correct, rails should assume time values coming
from the database are UTC, and since config.time_zone is set to 'UTC',
it should not try to convert the times.

Instead, rails is assuming that the db values are Eastern (my local
zone) and is incorrectly adding four hours to convert to what it
thinks is UTC.  Either my understanding of how this is supposed to
work is wrong, or something needs to be fixed.

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
UTC time.

The output from script/console is below. (results are identical to
using a browser)

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=Event.find(1)

=> #<Event id: 1, edate: "2009-03-30 12:00:00">
>> event.edate

=> Mon, 30 Mar 2009 16:00:00 UTC +00:00
>> zutc='UTC')

=> #<ActiveSupport::TimeZone:0xb795cbc8
@tzinfo=#<TZInfo::DataTimezone: Etc/UTC>, @utc_offset=0, @name="UTC">
>> correct_time =,zutc)

=> Mon, 30 Mar 2009 12:00:00 UTC +00:00
5f94b9b346c2aa648a80bc259978e5bc?d=identicon&s=25 Colin Law (Guest)
on 2009-03-31 17:35
(Received via mailing list)
I am no expert on this but Timezone works ok for me on Rails 2.2.2 so my
comments below may help.
Which version of Rails are you using?

2009/3/31 Rick Schumeyer <>

> My environment.rb contains
> config.time_zone = 'UTC'

Where is this located in environent.rb.  Mine is inside the do |config|
end block.  Also I have 'London' rather than 'UTC' though I doubt if
this is
the problem.  Anything is worth trying though.

> 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
> UTC time.

Are you sure SQL Server is not adjusting the value for you before it
gets to
Rails, thinking that you want it in local time?
Have you tried picking the query up out of the rails log and entering
directly into SQL server to see what you get?

> => Mon, 30 Mar 2009 16:00:00 UTC +00:00

The fact that this is showing the wrong time but the one above shows
apparently the right one does suggest that Rails thinks that the zone is
your local zone for some reason, as you have surmised.
E3e98bfa99e82ac8b0cb63660dc23b14?d=identicon&s=25 Geoff Buesing (gbuesing)
on 2009-04-01 05:38
(Received via mailing list)
Indeed, this isn't the expected behavior. My best guess here is, maybe
ActiveRecord::Base.default_timezone is being set to :local somewhere?
If that were the case, then all times returned from the database would
be shifted based on your system local UTC offset, and thus mess things

If you run Event.default_timezone in the console, what does it return?
This topic is locked and can not be replied to.