简体   繁体   English

意外的MySQL DATETIME行为

[英]Unexpected MySQL DATETIME behavior

I'm seeing what seems to me unexpected behavior from MySQL DATETIME when I'm trying to insert values into my table from my Java application. 当我尝试从Java应用程序向表中插入值时,我看到了MySQL DATETIME出现的意外行为。 I've read a lot of StackOverflow questions and answers regarding DATETIME, but this seems to be a totally different issue. 我已经阅读了很多有关DATETIME的StackOverflow问题和答案,但这似乎是一个完全不同的问题。

My Java application uses MyBatis and this DateTimeTypeHandler to insert a joda DateTime value 2015-03-04T22:30:59.922Z . 我的Java应用程序使用MyBatis和此DateTimeTypeHandler插入joda DateTime值2015-03-04T22:30:59.922Z (I also modified the above code to use the setTimestamp(int, Timestamp, Calendar) method but did not notice and difference in results). (我还修改了上面的代码以使用setTimestamp(int,Timestamp,Calendar)方法,但没有注意到结果的不同)。

What I see being inserted into the table is the value 2015-03-04 14:30:59.922 . 我看到插入表中的值是2015-03-04 14:30:59.922 I read that MySQL DATETIME uses the server default timezone. 我读到MySQL DATETIME使用服务器默认时区。 I live in the PDT timezone so I would expect the value to be 2015-03-04 15:30:59.922 (that's 15:30 instead of 14:30). 我居住在PDT时区,因此我希望该值是2015-03-04 15:30:59.922 (即15:30而不是14:30)。

What's surprising is nothing in MySQL seems to indicate the PDT timezone. 令人惊讶的是,MySQL中似乎没有任何迹象表明PDT时区。 So why would my datetime value in the table be 2015-03-04 14:30:59.922? 那么,为什么表中的日期时间值是2015-03-04 14:30:59.922?

mysql> select @@global.time_zone; +--------------------+ | @@global.time_zone | +--------------------+ | UTC | +--------------------+ 1 row in set (0.06 sec)

mysql> select @@session.time_zone; +---------------------+ | @@session.time_zone | +---------------------+ | UTC | +---------------------+ 1 row in set (0.07 sec)

mysql> SELECT @@system_time_zone; +--------------------+ | @@system_time_zone | +--------------------+ | UTC | +--------------------+ 1 row in set (0.07 sec)

March 4, 2015 was Standard time so the offset is -8 hours: 2015年3月4日为标准时间,因此偏移时间为-8小时:

UTC (Time Zone)                   Wednesday, March 4, 2015 at 10:30:00 PM     UTC         
Los Angeles (U.S.A. - California) Wednesday, March 4, 2015 at 2:30:00 PM  PST UTC-8 hours 

Today (May 28, 2015) is Daylight time so the offset is -7 hours: 今天(2015年5月28日)是夏令时,因此偏移时间是-7小时:

UTC (Time Zone)                   Thursday, May 28, 2015 at 10:30:00 PM     UTC         
Los Angeles (U.S.A. - California) Thursday, May 28, 2015 at 3:30:00 PM  PDT UTC-7 hours 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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