# ActiveRecord doesn't care about timezone?

**URL:** <https://www.ruby-forum.com/t/activerecord-doesnt-care-about-timezone/174854>\
**Category:** Rails\
**Created:** [September 3, 2009, 4:25am UTC](https://www.ruby-forum.com/t/activerecord-doesnt-care-about-timezone/174854 "2009-09-03T04:25:54Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![coolmenu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/eada6e/32.png) [@coolmenu](https://www.ruby-forum.com/u/coolmenu)\
**Post date:** [September 3, 2009, 4:25am UTC](https://www.ruby-forum.com/t/activerecord-doesnt-care-about-timezone/174854/1 "2009-09-03T04:25:54Z")

</div>

I’m having some trouble getting activerecord to adhere to my timezone  
settings when finding objects based on its created\_at field. I  
understand that datetime fields are stored in UTC but it doesn’t  
convert the datetimes when it performs the lookup. I basically  
followed Railscasts #106 and I’m using Authlogic for authentication.  
Thanks for any help.

[http://gist.github.com/180080](http://gist.github.com/180080)

---

<div class="post-metadata">

**Author:** ![coolmenu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/eada6e/32.png) [@coolmenu](https://www.ruby-forum.com/u/coolmenu)\
**Post date:** [September 3, 2009, 7:18pm UTC](https://www.ruby-forum.com/t/activerecord-doesnt-care-about-timezone/174854/2 "2009-09-03T19:18:45Z")

</div>

This looks like an instance of this bug:

[https://rails.lighthouseapp.com/projects/8994/tickets/2946](https://rails.lighthouseapp.com/projects/8994/tickets/2946)

The quick way to fix it in this case is to convert the incoming  
datetime objects to UTC in timeline\_created\_products.

–Matt J.

---

<div class="post-metadata">

**Author:** ![coolmenu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/eada6e/32.png) [@coolmenu](https://www.ruby-forum.com/u/coolmenu)\
**Post date:** [September 4, 2009, 3:41am UTC](https://www.ruby-forum.com/t/activerecord-doesnt-care-about-timezone/174854/3 "2009-09-04T03:41:07Z")

</div>

Yeah, I tried that in my example that I posted but the results aren’t  
then converted back to the users timezone. So if a Product is sitting  
in the database with created\_at = 2009-09-03 02:39:22 UTC (which is  
2009-09-02 21:39:22 CDT), it won’t come back converted to CDT.

---

<div class="post-metadata">

**Author:** ![coolmenu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/eada6e/32.png) [@coolmenu](https://www.ruby-forum.com/u/coolmenu)\
**Post date:** [September 4, 2009, 7:58pm UTC](https://www.ruby-forum.com/t/activerecord-doesnt-care-about-timezone/174854/4 "2009-09-04T19:58:50Z")

</div>

I don’t think this is going to work - you’re grouping by the date in  
the DB, where they are all UTC. The dates that come back don’t have  
times, so there isn’t any way to convert them. (eg, 2009-09-03  
02:39:22 UTC -\> 2009-09-02 CDT, but 2009-09-03 2009-09-03 12:39:22 UTC  
-\> 2009-09-03 CDT)

The only way around it would be to add the offset back in on the SQL  
side before converting to a date; how you manage that will be based on  
which DB you’re using. Note that you’ll still have a mess on your  
hands, as the two months where DST changes won’t have a consistent  
offset…

–Matt J.

---

<div class="post-metadata">

**Author:** ![coolmenu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/eada6e/32.png) [@coolmenu](https://www.ruby-forum.com/u/coolmenu)\
**Post date:** [September 5, 2009, 6:23pm UTC](https://www.ruby-forum.com/t/activerecord-doesnt-care-about-timezone/174854/5 "2009-09-05T18:23:28Z")

</div>

Yeah that sounds pretty nasty. To get around all this, I simply  
changed my timeline endpoints to start at 30 days ago and end at  
strictly less than today.midnight. That way I don’t include today’s  
numbers which could possibly be wrong because of timezone  
differences. Thanks Matt.
