您的位置:首页 > 数据库 > MySQL

【翻译】【MySQL】Mysql数据库中时间类型说明(Mybatis,Date,DateTime,Timestamp);

2017-12-21 16:30 1071 查看

11.3.1 The DATE, DATETIME, and TIMESTAMP Types

这里对MySQL官方文档做了一些翻译,英文段落为原版复制下来的;中文部分为翻译内容,并且在标题等格式处理上加了自己的东西,特此说明;一般的,Date翻译作【日期】,狭义上仅仅表示【年月日】;而Time翻译作【时间】,狭义上仅仅表示【时分秒(毫秒)】

一、综述

The DATE, DATETIME, and TIMESTAMP types are related. This section describes their characteristics, how they are similar, and how they differ. MySQL recognizes DATE, DATETIME, and TIMESTAMP values in several formats, described in Section 9.1.3, “Date and Time Literals”. For the DATE and DATETIME range descriptions, “supported” means that although earlier values might work, there is no guarantee.

Date,DateTime和Timestamp类型是相关的。本章节描述了他们的特性,他们的相似之处和不同之处;在9.1.3章节中(“Date和Time的本义”),MySQL能够识别Date,DateTime和TimeStamp这几种格式的值。对于Date和DateTime范围描述,“supported”表示尽管早期这些值可能有效,但是不能保证现在或以后还会有效

PS:这里的Literal,表示本义,即不带函数的,字符原始的意思;因为有多义字,多义词;例如!这个符号的原始意思是感叹号;!作为IT技术的非的意思就不算原始意思;例如%的literal就表示百分号的意思;%作为取余数的功能等全部不算在原始意思里面;

二、Date类型

The DATE type is used for values with a date part but no time part. MySQL retrieves and displays DATE values in ‘YYYY-MM-DD’ format. The supported range is ‘1000-01-01’ to ‘9999-12-31’.

Date类型是带有【年月日】部分,但是不带【时分秒】部分;MySQL检索并展示‘YYYY-MM-DD’格式的值;支持的范围是’1000-01-01’ 到 ‘9999-12-31’

三、DateTime类型

The DATETIME type is used for values that contain both date and time parts. MySQL retrieves and displays DATETIME values in ‘YYYY-MM-DD HH:MM:SS’ format. The supported range is ‘1000-01-01 00:00:00’ to ‘9999-12-31 23:59:59’.

DateTime类型被用于既有【年月日】部分又有【时分秒】部分的值;MySQL检索并展示’YYYY-MM-DD HH:MM:SS’格式的DateTime格式的值;支持的范围是:’1000-01-01 00:00:00’ to ‘9999-12-31 23:59:59’。

四、TimeStamp类型

The TIMESTAMP data type is used for values that contain both date and time parts. TIMESTAMP has a range of ‘1970-01-01 00:00:01’ UTC to ‘2038-01-19 03:14:07’ UTC.

TimeStamp日期类型被用于既含有日期【年月日】,又含有时间【时分秒】部分的值;TimeStamp的范围是:’1970-01-01 00:00:01’ UTC到 ‘2038-01-19 03:14:07’ UTC.

PS:协调世界时,又称世界统一时间、世界标准时间、国际协调时间。由于英文(CUT)和法文(TUC)的缩写不同,作为妥协,简称UTC。

协调世界时是以原子时秒长为基础,在时刻上尽量接近于世界时的一种时间计量系统。

五、时区

MySQL converts TIMESTAMP values from the current time zone to UTC for storage, and back from UTC to the current time zone for retrieval. (This does not occur for other types such as DATETIME.) By default, the current time zone for each connection is the server’s time. The time zone can be set on a per-connection basis. As long as the time zone setting remains constant, you get back the same value you store. If you store a TIMESTAMP value, and then change the time zone and retrieve the value, the retrieved value is different from the value you stored. This occurs because the same time zone was not used for conversion in both directions. The current time zone is available as the value of the time_zone system variable. For more information, see Section 10.6, “MySQL Server Time Zone Support”.

MySQL将TimeStamp从当前时区转换成UTC时间,并且从UTC时间回到当前时区来检索;(某些类型不会发生,例如DateTime类型);默认情况下,当前时区的每一个连接都是服务器时间。我们可以为每一个连接设置时区;只要时区被设置成保持不间断的;你可以获取你保存的相同值;如果你存储了一个TimeStamp值,并且接着改变了时区,当你重新检查该值的时候,这个值和你之前设置的值不一样了;这种现象的发生是因为相同的时区,在双向转换上并不管用;当前时区是适用的的前提是因为时区系统的变化;如果需要更多信息,参见10.6“MySQL Server 时区支持”;

六、TimeStamp的自动初始化和更新

The TIMESTAMP data type offers automatic initialization and updating to the current date and time. For more information, see Section 11.3.5, “Automatic Initialization and Updating for TIMESTAMP”.

TimeStamp数据类型提供自动的初始化和更新到当前日期和时间。如果需要更多信息,参见11.3.5章节“TimeStamp的自动初始化和更新”。

七、微小秒数(秒,毫秒等)

A DATETIME or TIMESTAMP value can include a trailing fractional seconds part in up to microseconds (6 digits) precision. Although this fractional part is recognized, it is discarded from values stored into DATETIME or TIMESTAMP columns. For information about fractional seconds support in MySQL, see Section 11.3.6, “Fractional Seconds in Time Values”.

一个DateTime或者TimeStamp的值可以包括一个追踪微不足道的秒数的部分甚至毫秒数(6位数字)精确度;尽管这个微小部分是众所周知的,但是在DATETIME和TIMESTAMP栏目里,是被抛弃的;需要MySQL中关于微小秒数支持信息的,参见11.3.6“时间值中的微小秒数”。

八、不存在的时间如何处理

Invalid DATE, DATETIME, or TIMESTAMP values are converted to the “zero” value of the appropriate type (‘0000-00-00’ or ‘0000-00-00 00:00:00’).

不存在的Date,DateTime或者TimeStamp值会被转换成各自类型的“Zero”值;(’0000-00-00’ or ‘0000-00-00 00:00:00’)

九、在MySQl中必须意识到的一些关于时间的常识

Be aware of certain properties of date value interpretation in MySQL:

1、MySQL permits a “relaxed” format for values specified as strings, in which any punctuation character may be used as the delimiter between date parts or time parts. In some cases, this syntax can be deceiving. For example, a value such as ‘10:11:12’ might look like a time value because of the :, but is interpreted as the year ‘2010-11-12’ if used in a date context. The value ‘10:45:15’ is converted to ‘0000-00-00’ because ‘45’ is not a valid month.

1、MySQL允许“轻松的”格式的,例如strings类型特定时间值格式,这里任何标点符号可能作为分隔符处理;在某些情况下,这种句法可以欺骗(可能有歧义);例如,一个像“10:11:12”可能看上去像一个时间值,因为冒号的缘故;但是实际应该被解释作为2010-11-12如果你使用一个日期上下文;’10:45:15’ 会被转化成’0000-00-00’,因为45不可能是月份;

2、The server requires that month and day values be valid, and not merely in the range 1 to 12 and 1 to 31, respectively. With strict mode disabled, invalid dates such as ‘2004-04-31’ are converted to ‘0000-00-00’ and a warning is generated. With strict mode enabled, invalid dates generate an error. To permit such dates, enable ALLOW_INVALID_DATES. See Section 5.1.8, “Server SQL Modes”, for more information.

2、服务器要求月份和日期天数值是合理的,不仅仅是月份1-12,日期1-31;在严格模式下不可能的,不存在的日期,例如:’2004-04-31’ 会被转换成“Zero”值,即 ‘0000-00-00’ ;并且会产生一个警告,因为4月没有31号;在严格模式下可以,合理的日期产生一个错误;为了照顾这种日期,允许合理的日期,请参见5.1.8章节“服务器SQL模式”来获取更多信息;

3、MySQL does not accept TIMESTAMP values that include a zero in the day or month column or values that are not a valid date. The sole exception to this rule is the special “zero” value ‘0000-00-00 00:00:00’.

3、MySQL不会接受TimeStamp值中在天数,或者月份栏目为0,或者不存在的日期;仅有的一个异常规则是特殊“Zero”值: ‘0000-00-00 00:00:00’;

4、CAST() treats a TIMESTAMP value as a string when not selecting from a table. (This is true even if you specify FROM DUAL.) See Section

12.10, “Cast Functions and Operators”.

4、CAST()方法针对一个string类型的Timestamp的值,当没有从一个表格中选择的缘故;(这是真的,如果你指定了从DUAL表中查询);参见12.10章节“Cast方法和操作器”;

PS:Cast()方法,是将一个参数转换成特定类型的值;CAST (A AS B);这里A代表参数,AS作为固定分隔符,是关键字,不能改;B是A要转换的类型;例如Cast(‘2’AS int)表示将字符串2转化成整数2;**

5、Dates containing two-digit year values are ambiguous because the century is unknown. MySQL interprets two-digit year values using these rules:

Dates包括两位数字的年份值是会引起歧义的,因为世纪是未知的;MySQL要描述两位的年份值从长用下面两种规则;

Year values in the range 00-69 are converted to 2000-2069.

00-69会被理解为2000-2069;

Year values in the range 70-99 are converted to 1970-1999.

70-99会被理解为1970-1999;

See also Section 11.3.8, “Two-Digit Years in Dates”.

你可以参见11.3.8章节“日期中的两位数年份”;

十、注意

Note

The MySQL server can be run with the MAXDB SQL mode enabled. In this case, TIMESTAMP is identical with DATETIME. If this mode is enabled at the time that a table is created, TIMESTAMP columns are created as DATETIME columns. As a result, such columns use DATETIME display format, have the same range of values, and there is no automatic initialization or updating to the current date and time. See Section 5.1.8, “Server SQL Modes”.

MySQL服务器可以和MAXDB SQL模式一起运行。在这种情况下,TimeStamp时间戳被和DateTime一起定义;当一个表格被创建的时间,如果这种模式被允许,TimeStamp栏目会被创建成一个DateTime栏目;结果,这个栏目使用DateTime展示格式,有相同的值范围,并且,没有自动初始化和自动更新当前日期和时间功能;请参见5.1.8“服务器SQL模式”;

欢迎关注我的公众号:

【幕桥社区】

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: