Local time zone offset in PostgreSQL

懵懂的女人 提交于 2019-12-18 12:45:35

问题


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

To get an interval representing the difference between my database server's time zone and UTC, I'm currently using this 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.)


回答1:


SELECT  current_setting('TIMEZONE')

This can be used in a query, however, this does not give a numerical difference.

Your solution is fine.




回答2:


To get # of seconds difference as an integer, I have used simply:

select extract( timezone from now() );


来源:https://stackoverflow.com/questions/2110216/local-time-zone-offset-in-postgresql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!