简体   繁体   中英

Inserting timestamps with a time zone offset in MySQL is inconsistent

I have a single table called "X" with one datetime column called X in MySQL.

When I try to insert a value into it with a time zone offset, it's inconsistent in how it stores the data. Sometimes it transposes the time based on the time zone, sometimes not. Here's an example, where only the fraction of a second is changed:

insert into x(x) values('2021-01-01 01:01:01.5-05:00');

insert into x(x) values('2021-01-01 01:01:01.4-05:00');

Here's the results:

2021-01-01 01:01:02

2021-01-01 06:01:01

In the first case it ignores the time zone, and in the second case, the time zone is changed and stored in UTC.

I can't seem to find any documentation about this that would cause this. Any ideas? Thanks!

use convert_tz

SELECT CONVERT_TZ(SUBSTR('2021-01-01 01:01:01.4-05:00',1,21),'+00:00',SUBSTR('2021-01-01 01:01:01.4-05:00',22));

Result:

2020-12-31 20:01:01.4

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