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.

config.time_zone not setting default_timezone for AR, causing errors with Postgres

#5324

It appears that setting Rails::Application.config.time_zone (in application.rb) does not set ActiveRecord::Base.default_timezone.

If I set the config.time_zone in my application.rb to, for example, Central Time, then firing up the console shows:

    > Rails::application.config.time_zone
   => "Central Time (US & Canada)"
    > Rails::Application.config.active_record.default_timezone
   => nil

That may be expected behavior, and according to the postgres adapter, a nil value would cause activerecord to read the timezone from the default value of the postgres database, as per the following code (line 956):

      if ActiveRecord::Base.default_timezone == :utc
        execute("SET time zone 'UTC'")
      elsif @local_tz
        execute("SET time zone '#{@local_tz}'")
      end

However, the problem is that somewhere in the ActiveRecord stack, before calling the postgres adapter, the value of AR::Base.default_timezone is being changed from nil to 'utc'. This causes AR to try to execute the postgres command 'set time zone 'UTC'', which on a system where 'utc' is not recognized, will give the following error:

    PGError: ERROR:  unrecognized time zone name: "UTC"
    : SET time zone 'UTC'

(This can easily go unnoticed, because in most cases 'UTC' is valid. I discovered this problem, after much agony and hair pulling, on a freebsd system where it turns out postgres would only accept 'etc/utc' as a valid timezone, not 'utc'.)

I can verify that AR::Base.default_timezone is being set to 'utc' by checking the value just before the above lines, in the postgresql_adapter.rb:

      puts ActiveRecord::Base.default_timezone
      puts Rails::Application.config.time_zone          

      if ActiveRecord::Base.default_timezone == :utc
        execute("SET time zone 'UTC'")
      elsif @local_tz
        execute("SET time zone '#{@local_tz}'")
      end

The above, with config.time_zone set to Central Time, and AR::Base.default_timezone not set in application.rb, will return:

     utc
     Central Time (US & Canada)

So it seems that config.time_zone is not having an effect on AR::Base.default_timezone.

Reported by Sean · August 7th, 2010 @ 02:17 AM

State: stale
Milestone: none
Assigned to: nobody
Importance: none

Activity

  1. MikZ
    MikZ
    • Tag set to time zone, activerecord, postgres, postgresql, timezone

    I've run into this issue too. My views depends on time zone settings and postgres adapter wrongly set it to UTC.
    ActiveRecord::Base.default_timezone is :utc even if it isn't configured.

    September 10th, 2010 @ 12:25 AM

  2. JackC
    JackC

    I have the same issue as MikZ. Wrong time zone setting is breaking a view that does time calculations.

    A workaround appears to be adding the following line to application.rb:

    config.active_record.default_timezone = :local

    This fixes the view, and the rest of my app seems unaffected.

    November 19th, 2010 @ 09:41 PM

  3. MikZ
    MikZ

    i've figured this out

    rails are setting accessors with automatic timezone conversions
    when you override acessor (with table colum) you will get to serious problems

    when leaving rails default (or setting timezone) you will get nice results
    but if you override accessors you will get to serious problems with that

    so: don't ever override "time" attibutes' accessors

    November 20th, 2010 @ 02:45 AM

  4. Steve Madsen
    Steve Madsen

    I am experiencing this bug, too.

    I found it after I noticed that some of my reporting, which groups records by date, was returning erroneous results. PostgreSQLAdapter is setting the connection's time zone to UTC, which throws off queries that follow this pattern:

    SELECT DATE(created_at AT TIME ZONE 'UTC') ...
    

    If the connection is properly set to the local time zone, the AT TIME ZONE part will adjust to local time before truncating to a date. If the connection is set to UTC, PostgreSQL thinks that no adjustment is needed, and times when the UTC date and local time date are different result in the wrong answer.

    December 27th, 2010 @ 07:13 PM

  5. rails
    rails
    • State changed from new to open

    This issue has been automatically marked as stale because it has not been commented on for at least three months.

    The resources of the Rails core team are limited, and so we are asking for your help. If you can still reproduce this error on the 3-0-stable branch or on master, please reply with all of the information you have about it and add "[state:open]" to your comment. This will reopen the ticket for review. Likewise, if you feel that this is a very important feature for Rails to include, please reply with your explanation so we can consider it.

    Thank you for all your contributions, and we hope you will understand this step to focus our efforts where they are most helpful.

    March 28th, 2011 @ 01:00 AM

  6. rails
    rails
    • State changed from open to stale

    March 28th, 2011 @ 01:00 AM