问题
I would like my reports to display in CST while my database is in UTC. For this to work I need to be able to group by a time column and have the db do timezone conversions.
This works on my dev postgresql box:
offset = -5
result = ActiveRecord::Base.connection.execute("select date(srl.created_at AT TIME
ZONE '#{offset.to_s}') AS date, count(*) from server_request_logs srl where
srl.created_at between '#{@from.to_s(:db)}' and '#{@to.to_s(:db)}' group by
date(srl.created_at AT TIME ZONE '#{offset.to_s}')")
on heroku it errors with:
ActiveRecord::StatementInvalid: PGError: ERROR: time zone "-5" not recognized
Its also not working with TZInfo names like 'America/Winnipeg' or supported timezones like 'CST' from http://www.postgresql.org/docs/7.2/static/timezones.html
has anyone been able to get this working?
回答1:
First off, make sure that you define your timestamp columns and variables as TIMESTAMP WITH TIME ZONE
(or timestamptz
for short). In PostgreSQL this doesn't actually cause any time stamp to be saved; but makes it a fixed point in time, stored in UTC. You can view it AT TIME ZONE
of your choosing with clean semantics. TIMESTAMP WITHOUT TIME ZONE
(which is what you get if you just say TIMESTAMP
) is not a fixed point in time until it is resolved against a time zone, and is therefore much harder to work with.
The documentation page you cite regarding time zones is from a very old version of PostgreSQL which has gone out of support. Maybe this page will be of more help to you:
http://www.postgresql.org/docs/current/interactive/datetime-config-files.html
来源:https://stackoverflow.com/questions/10274375/ror-postgresql-timezone-group-by-not-working-on-heroku