Timezones in Rails 2.3.2

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
seeing.

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?

Thanks,
Tom

If you are using postgresql database, then the Solution is below :-

ALTER DATABASE SET timezone TO “UTC”;

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.

Sorry, I should have used ‘2009-04-03 21:00:00:00’.to_time
(:local).in_time_zone.