问题
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