This project is archived and is in readonly mode.

#454 ✓invalid
Eugene Pimenov

ActiveRecord timezone issues

Reported by Eugene Pimenov | June 19th, 2008 @ 10:55 PM | in 2.1.1

>> Fileset.find(:all, :conditions => ["created_at >= ?", Fileset.last.created_at])
=> [#<Fileset id: 7, name: "Untitled.xls", description: nil, created_at: "2008-06-19 20:31:07", updated_at: "2008-06-19 20:31:07", original_size: 433152>]
>> Fileset.find(:all, :conditions => ["created_at >= ?", Time.mktime(2008, 06, 20, 0, 31, 7)])
=> []
>> Fileset.find(:all, :conditions => {:created_at => Time.now.midnight..Time.now})
=> []
>> Fileset.find(:all, :conditions => {:created_at => Time.now.midnight.utc..Time.now})
=> [#<Fileset id: 7, name: "Untitled.xls", description: nil, created_at: "2008-06-19 20:31:07", updated_at: "2008-06-19 20:31:07", original_size: 433152>]
>> Time.mktime(2008, 06, 20, 0, 31, 7)
=> Fri Jun 20 00:31:07 +0400 2008
>> Fileset.last.created_at
=> пт, 20 июн 2008 00:31:07 MSD +04:00

#

>> Time.now
=> Fri Jun 20 01:27:26 +0400 2008
>> Time.now.utc
=> Thu Jun 19 21:27:39 UTC 2008
>> Time.now.zone
=> "MSD"

Since Time.now.zone shows me correct timezone, I do not see the reason why I should pass UTC to ActiveRecord::Base.find

Comments and changes to this ticket

  • Eugene Pimenov

    Eugene Pimenov June 19th, 2008 @ 10:31 PM

    sorry, it's already fixed

  • Eugene Pimenov

    Eugene Pimenov June 19th, 2008 @ 10:55 PM

    okay, hash conditions works, but string do not.

    edge rails, installed via rake rails:freeze:edge today (20 jun 2008)

  • Jeremy Kemper

    Jeremy Kemper June 19th, 2008 @ 11:45 PM

    • Milestone set to 2.1.1
    • Assigned user set to “Geoff Buesing”
    • State changed from “new” to “open”
  • Geoff Buesing

    Geoff Buesing June 20th, 2008 @ 03:34 AM

    Time#to_s(:db) ignores Time#zone -- example:

    >> Time.mktime(2008, 06, 20, 0, 31, 7).to_s(:db)
    => "2008-06-20 00:31:07"
    >> Time.utc(2008, 06, 20, 0, 31, 7).to_s(:db)
    => "2008-06-20 00:31:07"
    

    ...therefore, AR will treat Time.mktime values as if they were Time.utc values.

    >> Comment.find(:all, :conditions => {:created_at => Time.local(2008,1,1)})
    June 19, 2008 21:18 -- Comment Load (0.001406)  SELECT * FROM "comments" WHERE ("comments"."created_at" = '2008-01-01 00:00:00') 
    

    However, if you're setting config.time_zone, you should be able to specify a local time with Time.zone.local() for :conditions, and the correct time value will be sent to the db:

    >> Comment.find(:all, :conditions => ['created_at > ?', Time.zone.local(2008,1,1)])
    June 19, 2008 21:23 -- Comment Load (0.001179)  SELECT * FROM "comments" WHERE (created_at > '2008-01-01 06:00:00')  
    
  • Eugene Pimenov

    Eugene Pimenov June 20th, 2008 @ 03:46 AM

    I'm setting config.time_zone…

    I think you should consider automatic conversation to UTC…

    Because of midnight (and similar) functions. It simply do not point to midnight.

    Of course if there're some issues with it, simply close this ticket…

  • Jeremy Kemper

    Jeremy Kemper June 20th, 2008 @ 04:03 AM

    • State changed from “open” to “invalid”
  • Richie Vos

    Richie Vos December 1st, 2009 @ 08:48 PM

    • Tag set to activerecord, bug

    Jeremy, I know it's been awhile, but can you explain why this is invalid?

    It seems like an inconsistent api that the following doesn't work:

    @@@ ruby { time = Time.mktime(2000, 1, 1)
    Model.create!(:occurred_at => time)
    Model.find(:first, :conditions => { :occurred_at => time)
    }

  • Richie Vos

    Richie Vos December 1st, 2009 @ 08:50 PM

    I am confident I will never properly format a lighthouse post.

  • Richie Vos

    Richie Vos December 1st, 2009 @ 09:03 PM

    • Tag changed from activerecord, bug to activerecord, bug, timezone

    Sorry, for 3 separate comments, but I think I should clarify, since I had too much timezone info in my brain and didn't properly say it (will be quite happy when lighthouse gets that edit/preview button).

    My environment has my local box's timezone as Central. In my environment.rb I have:

    config.time_zone = "Eastern Time (US & Canada)"
    

    So when I do

    time = Time.mktime(2000, 1, 1)
    model = Model.create!(:occurred_at => time)
    model = Model.find(model.id) # just to get it clean from the AR/DB point of view
    

    I end up with a model in the db with the time as
    model.occurred_at.to_s: "2000-01-01 01:00:00 -0500"
    model.occurred_at_before_type_cast: "2000-01-01 06:00:00"

    This makes complete sense, since my time was created with an implicit timezone and AR respected that. However, right after doing that, if I do my find, AR doesn't do a conversion, leading to the find not finding anything.

    Model.first(:conditions => { :occurred_at => model.occurred_at }) !=
    Model.first(:conditions => { :occurred_at => time })
    

    That seems really confusing to me. I can't really think of a reason why that'd be desired behavior.

    So I'm thinking I'm misunderstanding something, and would appreciate some clarification.

  • Ryan Bigg

    Ryan Bigg October 9th, 2010 @ 10:03 PM

    • Tag cleared.
    • Importance changed from “” to “Low”

    Automatic cleanup of spam.

  • Ryan Bigg

    Ryan Bigg October 21st, 2010 @ 03:36 AM

    Automatic cleanup of spam.

  • Allan Wake
  • booamd

    booamd March 15th, 2011 @ 12:07 PM

    Almost instantly the Trianon became synonymous with gracious travelling and elegant travellers.http://www.vswatches.com

  • marusca91
  • semiramida

    semiramida March 22nd, 2011 @ 09:46 AM

    I found many valuable things to learn in this article. Please keep on posting such quality articles. search here

  • Alexa VIDEOS

    Alexa VIDEOS March 31st, 2011 @ 02:51 PM

    Thanks gyus! you helped me a lot!

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>

Pages