Forum: Ruby on Rails Timezones in Rails 2.3.2

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.
TomRossi7 (Guest)
on 2009-04-04 06:01
(Received via mailing list)
I'm trying to do some queries on the database of a Rails 2.3.2 app
configured with config.time_zone = 'Eastern Time (US & Canada)'.  I
was under the impression that if I were to query the db for entries
that fall within a time range, that the time would automatically be
converted to UTC before executing the SQL, but that is not what I am

For example:
>> my_time = '2009-04-03 21:00:00'.to_time(:local)
>> Record.create(:time => my_time)
>> Record.find_by_time(my_time)
** Returns nothing **

Shouldn't that work?  When I look at the SQL, it is querying for a
time of '2009-04-03 21:00:00' even though the time object was not UTC?

Siddick E. (Guest)
on 2009-04-04 08:07
If you are using postgresql database, then the Solution is below :-

 ALTER DATABASE <database name> SET timezone TO "UTC";
tomrossi7 (Guest)
on 2009-04-08 23:41
(Received via mailing list)
If anyone else runs into this issue, it appears to be an inconsistency
in the way Rails handles a time.

'2009-04-03 21:00:00:00'.to_time(:local)
=> Fri Apr 03 21:00:00 -0400 2009

'2009-04-03 21:00:00:00'.to_time.in_time_zone
=> Fri, 03 Apr 2009 17:00:00 EDT -04:00

Notice with .in_time_zone, it actually put the GMT offset as well as a
timezone identifier.

If you run queries with the first time object, it will disregard the
gmt offset and assume the time is local.  If you run queries with the
second time object, it will convert the time to UTC first.
tomrossi7 (Guest)
on 2009-04-08 23:44
(Received via mailing list)
Sorry, I should have used '2009-04-03 21:00:00:00'.to_time
This topic is locked and can not be replied to.