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.

Time columns are not zone aware

#2527

#1095 Time columns don't handle timezone correctly seems to be the same issue.

Time columns do not seem to be zone aware. I am storing a schedule and I want to show that time in the users chosen time zone. Changing the time zone with Time.zone does not affect the output of the time. However, if you call it with column_name.in_time_zone, it does work.

Reported by Justin Mazzi · April 20th, 2009 @ 01:37 PM

State: wontfix
Milestone: 2.x
Assigned to: nobody
Importance: none

Activity

  1. Justin Mazzi
    Justin Mazzi
    • Tag changed from 2.3.2, in_time_zone, time, zone to 2.3.2, active_record, in_time_zone, time, zone

    April 20th, 2009 @ 01:37 PM

  2. Justin Mazzi
    Justin Mazzi
    • Tag changed from 2.3.2, active_record, in_time_zone, time, zone to 2.3.2, active_record, bug, in_time_zone, time, zone

    April 20th, 2009 @ 01:38 PM

  3. Ryan Bigg
    Ryan Bigg

    I think this is more of a bug in your Rails application, not Rails.

    Do you have config.active_record.time_zone set in config/environment.rb? If that is set and Time.zone is set to the user's timezone, then any time retrieved from the database should be relative to that user. Take this example:

    >> Topic.first.created_at
    => Sun, 19 Apr 2009 11:17:35 UTC +00:00
    >> Time.zone = "Brisbane"
    => "Brisbane"
    >> Topic.first.created_at
    => Sun, 19 Apr 2009 21:17:35 EST +10:00
    
    

    April 21st, 2009 @ 12:31 AM

  4. Justin Mazzi
    Justin Mazzi

    Yes, this works fine with a datetime, but not with a time (unless you call it with in_time_zone)

    
    create_table :schedules do |t|
      t.time :start_time
    end
    
    >> Schedule.first.start_time
    => Sat Jan 01 08:19:00 UTC 2000
    >> Time.zone = "Brisbane"
    => "Brisbane"
    >> Schedule.first.start_time
    => Sat Jan 01 08:19:00 UTC 2000
    

    April 21st, 2009 @ 01:41 AM

  5. Geoff Buesing
    Geoff Buesing
    • State changed from new to wontfix

    I assume you're talking about a sql :time (i.e., time-only) column, which AR returns as an instance of Time, with year, month and day set to Jan 1, 2000.

    Rails doesn't do automatic time zone conversions on time-only columns, because there's not enough information to do so, given that the UTC offset changes depending upon the date. If we did do automatic time zone conversions, they would be for that time on Jan 1, 2000, which is probably not what you want.

    So it's appropriate that you'll have to do a bit more fiddling to do the proper time zone conversions, since the rules will be app-specific.

    April 22nd, 2009 @ 03:38 PM