PostgreSQL 中的本地时区偏移量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2110216/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Local time zone offset in PostgreSQL
提问by TimH
My web app stores all timestamps in UTC without time zones. I have a shell/psql script that returns a list of recent logins, and I want that script to display login times in the server's local time zone (which may vary depending on where the server is and with daylight savings).
我的网络应用程序以 UTC 格式存储所有时间戳,没有时区。我有一个 shell/psql 脚本,它返回最近登录的列表,我希望该脚本在服务器的本地时区中显示登录时间(这可能会因服务器所在的位置和夏令时而异)。
To get an interval representing the difference between my database server's time zone and UTC, I'm currently using this hack:
为了获得代表我的数据库服务器的时区和 UTC 之间差异的间隔,我目前正在使用这个 hack:
SELECT age(now() at time zone 'UTC', now());
That works, but is there a more straightforward way?
那行得通,但有没有更直接的方法?
There's a server configuration parameter called "timezone" that returns a valid timezone string, but I don't think it's possible to access those parameters in a query. (I guess that's a separate question, but an answer to it would resolve the time zone issue.)
有一个名为“timezone”的服务器配置参数返回一个有效的时区字符串,但我认为不可能在查询中访问这些参数。(我想这是一个单独的问题,但对它的回答将解决时区问题。)
回答by Quassnoi
SELECT current_setting('TIMEZONE')
This can be used in a query, however, this does not give a numerical difference.
这可以在查询中使用,但是,这不会给出数字差异。
Your solution is fine.
你的解决方案很好。
回答by shaunc
To get # of seconds difference as an integer, I have used simply:
为了将秒差作为整数,我简单地使用了:
select extract( timezone from now() );