This project is archived and is in readonly mode.

#2527 ✓wontfix
Justin Mazzi

Time columns are not zone aware

Reported by Justin Mazzi | April 20th, 2009 @ 01:37 PM | in 2.x

#1095 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.

Comments and changes to this ticket

  • Justin Mazzi

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

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

    Justin Mazzi April 20th, 2009 @ 01:38 PM

    • 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
  • Ryan Bigg

    Ryan Bigg April 21st, 2009 @ 12:31 AM

    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
    
    
  • Justin Mazzi

    Justin Mazzi April 21st, 2009 @ 01:41 AM

    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
    
  • Geoff Buesing

    Geoff Buesing April 22nd, 2009 @ 03:38 PM

    • 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.

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>

People watching this ticket

Pages