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.
5f3d52658aafcf0aabe76a5147e8909f?d=identicon&s=25 TomRossi7 (Guest)
on 2009-04-04 04: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?

60ddadf1a8a8ebac760e7e4cc1e342b3?d=identicon&s=25 Siddick Ebramsha (siddick)
on 2009-04-04 06:07
If you are using postgresql database, then the Solution is below :-

 ALTER DATABASE <database name> SET timezone TO "UTC";
5f3d52658aafcf0aabe76a5147e8909f?d=identicon&s=25 tomrossi7 (Guest)
on 2009-04-08 21: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.
5f3d52658aafcf0aabe76a5147e8909f?d=identicon&s=25 tomrossi7 (Guest)
on 2009-04-08 21: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.