How can I get correctly displaced UTC timestamp from a unix timestamp in hsqldb

你离开我真会死。 提交于 2019-12-11 04:17:15

问题


In hsqldb the function TIMESTAMP ( ) returns a WITHOUT TIME ZONE timestamp which is adjusted to session's time zone before any further conversion.

So lets say my session is at UTC+1 and I have a unix timestamp of 1364353339 (Wed, 27 Mar 2013 03:02:19 GMT, according to http://www.onlineconversion.com/unix_time.htm) coming from elsewhere. If I call:

VALUES( TIMESTAMP( 1364353339 ) AT TIME ZONE INTERVAL '0:00' HOUR TO MINUTE );

which gives 2013-03-27 02:02:19.000000+0:00. This has the correct tz but actual value is one hour less tha it should.

Other possibilities I've explored

VALUES( TIMESTAMP( 1364353339 ) AT LOCAL ) --> 2013-03-27 03:02:19.000000+1:00
Value right, TZ wrong

VALUES CAST(TIMESTAMP(1364353339) AT TIME ZONE INTERVAL '0:00' HOUR TO MINUTE AS TIMESTAMP(0) WITH TIME ZONE); --> 2013-03-27 02:02:19+0:00
Value wrong, TZ right

VALUES CAST(TIMESTAMP(1364353339) AT LOCAL AS TIMESTAMP(0) WITH TIME ZONE) --> 2013-03-27 03:02:19+1:00
Value right, TZ wrong

All of these return correct values (either 2013-03-27 03:02:19+0:00 or 2013-03-27 04:02:19+1:00) if session's time zone is previously switched to UTC (for instance with SET TIME ZONE INTERVAL '0:00' HOUR TO MINUTE).

I cannot do that as this is being computed for a view field. Actual timestamps are coming from a field on a text table.


回答1:


This will probably work in all time zones:

VALUES TIMESTAMP(1364353339) + SESSION_TIMEZONE()

These forms are also possible:

VALUES TIMESTAMP(1364353339) + DATABASE_TIMEZONE()
VALUES TIMESTAMP(1364353339) + TIMEZONE()

The DATABASE_TIMEZONE() can be different from the session in client-server setups. The TIMEZONE() can be different from SESSION_TIMEZONE() if SET TIME ZONE is used



来源:https://stackoverflow.com/questions/15712832/how-can-i-get-correctly-displaced-utc-timestamp-from-a-unix-timestamp-in-hsqldb

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