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 postgresql_adapter: Include time zone in result of quoted_date( )

#1562

When the time zone is not included in a quoted date (which was the case before this patch), we make the assumption that the database server resides in (or is configured as if it resides in) the same time zone as the application which is using the ActiveRecord library to perform SQL queries.

This patch removes the above assumption by explicitly including the time zone in quoted dates, so that ActiveRecord generates SQL queries that work regardless of whether your application happens to be running in the same time zone as your Postgres database server.

See http://www.postgresql.org/docs/8... for reference.

Reported by Suraj N. Kurapati · December 11th, 2008 @ 11:05 PM

State: stale
Milestone: 3.x
Assigned to: Tarmo Tänav Tarmo Tänav
Importance: none

Activity

  1. Suraj N. Kurapati
    Suraj N. Kurapati
    • Tag changed from patch to activerecord, patch, postgresql, quoting
    • Title changed from postgresql_adapter: Include time zone in result of quoted_date( ) to ActiveRecord postgresql_adapter: Include time zone in result of quoted_date( )

    December 17th, 2008 @ 07:01 PM

  2. Pratik
    Pratik
    • Assigned user set to Tarmo Tänav
    • State changed from new to incomplete

    Patch is missing tests.

    Thanks.

    December 21st, 2008 @ 11:02 PM

  3. Suraj N. Kurapati
    Suraj N. Kurapati

    Thanks for considering my patch. I will add the tests and attach a new version of this patch this week.

    December 22nd, 2008 @ 08:40 PM

  4. Geoff Buesing
    Geoff Buesing

    Just to clarify, this is only an issue with Postgres "timestamp with time zone" columns, correct? There shouldn't be an issue with the "timestamp without time zone" type, which is the type generated by ActiveRecord schema migrations.

    WIth this proposed solution, we'd be sending the zone for every Postgres datetime column, whether or not it needs it (if you're following Rails conventions, you'll never need it.) I'm assuming Postgres will ignore the zone when it doesn't need it, but I'm not sure.

    Your solution sends #zone, which returns a three-letter abbreviation -- this is ambiguous -- "CST", for example, can represent any number of zones, for example: US Central Standard Time (which has a utc offset of -6 or -5 hours), Chinese Standard Time (which has a utc offset of +8), etc. Better to send the unambiguous #formatted_offset value, e.g. "-06:00", and not rely on Postgres' interpretation of these three-letter abbreviations.

    Finally, how will Rails handle data returned from these columns? Will these columns be converted to ActiveRecord::Base.default_timezone, either in ActiveRecord code, or in Postgres (via a per-connection time zone setting, or something)?

    December 22nd, 2008 @ 11:07 PM

  5. Jeremy Kemper
    Jeremy Kemper
    • Milestone changed from 2.x to 3.x

    May 4th, 2010 @ 06:48 PM

  6. MikZ
    MikZ
    • Importance changed from to

    I've run into this issue yesterday.

    Proper way of solving this is to use utc time and format with time zone offset for column with time zone and whatever to column without time zone.

    Saving timestamp to column without time zone is also broken (when I have CEST timezone, adapter saves timestamp with timezone offset and Rails adds this offset again after reloading values). Postgres silently ignores time zone settings for columns without time zone.

    I've fixed all these problems by using timestamp with timezone and this http://gist.github.com/574015

    September 10th, 2010 @ 06:15 PM

  7. Suraj N. Kurapati
    Suraj N. Kurapati

    Here is a refactored version of MikZ's and my initial solution: http://gist.github.com/574080

    September 10th, 2010 @ 07:03 PM

  8. MikZ
    MikZ
    • Tag changed from activerecord, patch, postgresql, quoting to activerecord, patch, postgresql, quoting, timewithzone, timezone, utc

    I think there is one major problem with your solution. If its config.active_record.default_timezone set to something different from UTC (which you need if you want correct values from db), then calling super in quoted_date will convert value to that timezone. But when sending timezone offset, PostgreSQL needs UTC date. Thats why I'm calling value.to_s(:db) and why you can't just call super.

    September 15th, 2010 @ 06:56 AM

  9. Suraj N. Kurapati
    Suraj N. Kurapati

    Thanks for catching (and explaining) that MikZ. I have updated the solution accordingly: http://gist.github.com/574080 Cheers.

    September 15th, 2010 @ 07:13 AM

  10. Espen Antonsen
  11. MikZ
    MikZ

    I was wrong.
    Postgres expects local date + time zone of date.

    If you pass time without timezone to postgress and have set TIME ZONE its treated like with timezone in quoted date.

    Problem is somewhere else.
    ActiveRecord expects dates from DB in UTC (even if you set config.active_record.default_timezone) , but Postgres passes them in TIME ZONE.

    October 6th, 2010 @ 03:27 PM

  12. Espen Antonsen
    Espen Antonsen

    Any progress with this ticket?

    December 9th, 2010 @ 04:02 AM

  13. rails
    rails
    • State changed from incomplete 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 10th, 2011 @ 12:00 AM

  14. rails
    rails
    • State changed from open to stale

    March 10th, 2011 @ 12:00 AM