# Timezones in Rails 2.3.2

**URL:** <https://www.ruby-forum.com/t/timezones-in-rails-2-3-2/164694>\
**Category:** Rails\
**Created:** [April 4, 2009, 4:01am UTC](https://www.ruby-forum.com/t/timezones-in-rails-2-3-2/164694 "2009-04-04T04:01:27Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [April 4, 2009, 4:01am UTC](https://www.ruby-forum.com/t/timezones-in-rails-2-3-2/164694/1 "2009-04-04T04:01:27Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [April 4, 2009, 6:07am UTC](https://www.ruby-forum.com/t/timezones-in-rails-2-3-2/164694/2 "2009-04-04T06:07:26Z")

</div>

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

ALTER DATABASE SET timezone TO “UTC”;

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [April 8, 2009, 9:41pm UTC](https://www.ruby-forum.com/t/timezones-in-rails-2-3-2/164694/3 "2009-04-08T21:41:30Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [April 8, 2009, 9:44pm UTC](https://www.ruby-forum.com/t/timezones-in-rails-2-3-2/164694/4 "2009-04-08T21:44:26Z")

</div>

Sorry, I should have used ‘2009-04-03 21:00:00:00’.to\_time  
(:local).in\_time\_zone.
