简体   繁体   English

如何将日期时间值存储在mysql中?

[英]How datetime values are stored in mysql?

I am new to db world. 我是db world的新手。 I am sending a datetime value such as '2016-04-27 09:00:00' from java program, for getting it saved into mysql database. 我正在从Java程序发送日期时间值,例如“ 2016-04-27 09:00:00”,以将其保存到mysql数据库中。 My question is that How will this value be saved into datetime type of field in mysql db table. 我的问题是,如何将该值保存到mysql db表的datetime类型的字段中。 I mean, whether It'll be stored as string as I have passed or mysql will parse the string, and constructs UTC timestamp out of it and store that number into db. 我的意思是,无论是将其存储为我已经通过的字符串还是mysql,它将解析该字符串,并从中构造UTC时间戳并将该数字存储到db中。 Moreover, Will this stored value be retrieved as it is from java program, irrespective of different timezone settings of mysql server and java server ? 此外,无论mysql服务器和java服务器的时区设置是否不同,是否都可以从java程序中检索此存储值?

Sorry for such a silly question, but It's hurting me a lot and didn't get simple and less confusing answer, while searching over internet. 很抱歉提出这样一个愚蠢的问题,但是在通过Internet搜索时,这让我非常痛苦,而且没有得到简单且不太混乱的答案。

The datetime datatype for mysql 5.6.4 and after is saved as described in the manual page Date and Time Data Type Representation . mysql 5.6.4及更高版本的datetime数据类型按照手册页“ 日期和时间数据类型表示形式”中的描述进行保存。 In short, it is a 5-byte format including fractional seconds and is divorced of timezone information. 简而言之,它是5字节格式,包括小数秒,并且与时区信息分离。

Test table 测试台

create table dtTest
(
    id int auto_increment primary key,
    dt datetime not null
);

insert dtTest (dt) values ('2016-04-27 09:00:00');

If one needed to confirm this, one could jump into a hex editor of the saved file and verify the big-endian format of that datetime. 如果需要确认,可以跳到保存文件的十六进制编辑器,并验证该日期时间的大端格式。

show variables like '%datadir%';
C:\ProgramData\MySQL\MySQL Server 5.6\Data\

Traverse into folders and the db table (dtTest.ibd) 遍历文件夹和db表(dtTest.ibd)

There are system and session variables for timezone: 时区有系统和会话变量:

show variables like '%system_time_zone%';
Eastern Daylight Time

show variables like '%time_zone%';
SYSTEM

Let me be clear that I am completely clueless of the importance of these. 让我清楚一点,我完全不知道这些重要性。 Hopefully a peer can add clarity. 希望同伴可以增加清晰度。

SELECT @@global.time_zone, @@session.time_zone;
SYSTEM    SYSTEM

Test a reset to something different: 测试重置为其他内容:

SET SESSION time_zone = '+10:00';
select * from dtTest;

Note, the above setting change does not affect output of data. 注意,以上设置更改不影响数据输出。 It is still raw and unrelated to timezone. 它仍然是原始的,与时区无关。

SET SESSION time_zone = 'America/Chicago';

for the above to work, see: 有关上述工作,请参阅:

http://dev.mysql.com/doc/refman/5.6/en/mysql-tzinfo-to-sql.html http://dev.mysql.com/doc/refman/5.6/en/mysql-tzinfo-to-sql.html

Otherwise, without that timezone support loaded, one would need to perform something like: 否则,如果没有加载时区支持,则需要执行以下操作:

SET SESSION time_zone = '+10:00';

SELECT @@global.time_zone, @@session.time_zone;
SYSTEM      +10:00

setting session back to my default: 将会话设置回我的默认值:

SET SESSION time_zone = 'SYSTEM';

SELECT COUNT(*) FROM mysql.time_zone_name;

My count is 0 because I have not loaded the timezone support table yet 我的计数为0,因为我尚未加载时区支持表

SELECT CONVERT_TZ('2007-03-11 3:00:00','US/Eastern','US/Central');
-- so this is NULL for me. No support table loaded yet on my system.

SELECT CONVERT_TZ('2007-03-11 3:00:00','-4:00','-7:00');
2007-03-11 00:00:00 -- means that 3am NYC is midnight in San Fran

So the above is not NULL (less friendly to read UTC time offsets. Hard-coded). 因此,上面的内容不是NULL(读取UTC时间偏移量不太友好。硬编码)。

SELECT id, CONVERT_TZ(dt,'-4:00','-7:00') from dtTest;  -- dt was my column name
1   2016-04-27 06:00:00

The above select statement take the 9am value in the db, can converts it from NYC time to San Fran time. 上面的select语句在数据库中采用9am值,可以将其从NYC时间转换为San Fran时间。

That is the best I've got for you. 那是我为您准备的最好的。 Hopefully someone can fill in the missing details of timezone support. 希望有人可以填写缺少时区支持的详细信息。

From the 10.9 Date and Time Data Type Representation : 从10. 9日期和时间数据类型表示形式开始

DATETIME encoding for nonfractional part: 非小数部分的DATETIME编码:

  1 bit sign (1= non-negative, 0= negative) 17 bits year*13+month (year 0-9999, month 0-12) 5 bits day (0-31) 5 bits hour (0-23) 6 bits minute (0-59) 6 bits second (0-59) --------------------------- 40 bits = 5 bytes 

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

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