Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

ActiveRecord timezone issues

#454
>> 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

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

State: invalid
Milestone: 2.1.1
Assigned to: Geoff Buesing Geoff Buesing
Importance: Low

Activity

  1. Eugene Pimenov
    Eugene Pimenov

    sorry, it's already fixed

    June 19th, 2008 @ 10:31 PM

  2. Eugene Pimenov
    Eugene Pimenov

    okay, hash conditions works, but string do not.

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

    June 19th, 2008 @ 10:55 PM

  3. Jeremy Kemper
    Jeremy Kemper
    • Milestone set to 2.1.1
    • Assigned user set to Geoff Buesing
    • State changed from new to open

    June 19th, 2008 @ 11:45 PM

  4. Geoff Buesing
    Geoff Buesing

    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')  
    

    June 20th, 2008 @ 03:34 AM

  5. Eugene Pimenov
    Eugene Pimenov

    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…

    June 20th, 2008 @ 03:46 AM

  6. Jeremy Kemper
    Jeremy Kemper
    • State changed from open to invalid

    June 20th, 2008 @ 04:03 AM

  7. Richie Vos
    Richie Vos
    • 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)
    }

    December 1st, 2009 @ 08:48 PM

  8. Richie Vos
    Richie Vos

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

    December 1st, 2009 @ 08:50 PM

  9. Richie Vos
    Richie Vos
    • 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.

    December 1st, 2009 @ 09:03 PM

  10. Ryan Bigg
    Ryan Bigg
    • Tag cleared.
    • Importance changed from to Low

    Automatic cleanup of spam.

    October 9th, 2010 @ 10:03 PM

  11. Ryan Bigg
    Ryan Bigg

    Automatic cleanup of spam.

    October 21st, 2010 @ 03:36 AM

  12. Allan Wake
  13. booamd
    booamd

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

    March 15th, 2011 @ 12:07 PM

  14. marusca91
  15. semiramida
    semiramida

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

    March 22nd, 2011 @ 09:46 AM

  16. Alexa VIDEOS
    Alexa VIDEOS

    Thanks gyus! you helped me a lot!

    March 31st, 2011 @ 02:51 PM