简体   繁体   中英

mysql CONVERT_TZ inconsistent behavior

The following behavior puzzles me. My mysql server is installed on a PC with EEST timezone. I want to convert a datetime in mysql to UTC date.

When executing

select CONVERT_TZ('1970-01-01 02:00:01', 'SYSTEM', '+00:00')

the reponse is: '1970-01-01 00:00:01'

Similarly, when executing

select CONVERT_TZ('1970-01-01 04:00:00', 'SYSTEM', '+00:00')

the response is:'1970-01-01 02:00:00'

However, when I basically want the 0 unix time, by executing:

select CONVERT_TZ('1970-01-01 02:00:00', 'SYSTEM', '+00:00')

The reponse is again: '1970-01-01 02:00:00'

Why is that? Am I doing something wrong? How can I get the correct value which is '1970-01-01 00:00:00'?

I think the issue here is that an adjustment made in CONVERT_TZ which would result in a timestamp on or before the start of the UNIX epoch will be ignored, and the original input timestamp will be returned. Consider the following three queries:

SELECT CONVERT_TZ('1970-01-01 01:00:01', 'SYSTEM', '+00:00');
-- '1970-01-01 00:00:01'
SELECT CONVERT_TZ('1970-01-01 01:00:00', 'SYSTEM', '+00:00');
-- '1970-01-01 01:00:00'
SELECT CONVERT_TZ('1970-01-01 00:30:00', 'SYSTEM', '+00:00');
-- '1970-01-01 00:30:00'

Only the first query adjusts the timestamp; the second two just return the input.

From the excellent comments given by @Thomas above, I pulled the following from the MySQL documentation for CONVERT_TZ :

If the value falls out of the supported range of the TIMESTAMP type when converted from from_tz to UTC, no conversion occurs.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM