Skip to content

Commit eb1d8aa

Browse files
toutdesuiteTomShawn
andauthoredJul 29, 2020
Reorganize "Date and Time Types" doc (pingcap#3462)
* Update data-type-date-and-time.md * Update data-type-date-and-time.md * Update data-type-date-and-time.md Co-authored-by: TomShawn <41534398+TomShawn@users.noreply.github.com>
1 parent e11b14c commit eb1d8aa

File tree

1 file changed

+72
-74
lines changed

1 file changed

+72
-74
lines changed
 

‎data-type-date-and-time.md

+72-74
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ aliases: ['/docs/dev/data-type-date-and-time/','/docs/dev/reference/sql/data-typ
66

77
# Date and Time Types
88

9-
TiDB supports all MySQL date and time data types to store temporal values: `DATE`, `TIME`, `DATETIME`, `TIMESTAMP`, and `YEAR`. For more information, see [Date and Time Data Types in MySQL](https://dev.mysql.com/doc/refman/5.7/en/date-and-time-types.html).
9+
TiDB supports all MySQL date and time data types to store temporal values: [`DATE`](#date-type), [`TIME`](#time-type), [`DATETIME`](#datetime-type), [`TIMESTAMP`](#timestamp-type), and [`YEAR`](#year-type). For more information, see [Date and Time Data Types in MySQL](https://dev.mysql.com/doc/refman/5.7/en/date-and-time-types.html).
1010

1111
Each of these types has its range of valid values, and uses a zero value to indicate that it is an invalid value. In addition, the `TIMESTAMP` and `DATETIME` types can automatically generate new time values on modification.
1212

@@ -78,9 +78,75 @@ Different types of zero value are shown in the following table:
7878

7979
Invalid `DATE`, `DATETIME`, `TIMESTAMP` values are automatically converted to the corresponding type of zero value ( '0000-00-00' or '0000-00-00 00:00:00' ) if the SQL mode permits such usage.
8080

81-
<!-- markdownlint-disable MD001 -->
81+
## Supported types
82+
83+
### `DATE` type
84+
85+
`DATE` only contains date-portion and no time-portion, displayed in `YYYY-MM-DD` format. The supported range is '1000-01-01' to '9999-12-31':
86+
87+
```sql
88+
DATE
89+
```
90+
91+
### `TIME` type
92+
93+
For the `TIME` type, the format is `HH:MM:SS[.fraction]` and valid values range from '-838:59:59.000000' to '838:59:59.000000'. `TIME` is used not only to indicate the time within a day but also to indicate the time interval between 2 events. An optional `fsp` value in the range from 0 to 6 may be given to specify fractional seconds precision. If omitted, the default precision is 0:
94+
95+
```sql
96+
TIME[(fsp)]
97+
```
98+
99+
> **Note:**
100+
>
101+
> Pay attention to the abbreviated form of `TIME`. For example, '11:12' means '11:12:00' instead of '00:11:12'. However, '1112' means '00:11:12'. These differences are caused by the presence or absence of the `:` character.
102+
103+
### `DATETIME` type
104+
105+
`DATETIME` contains both date-portion and time-portion. Valid values range from '1000-01-01 00:00:00.000000' to '9999-12-31 23:59:59.999999'.
106+
107+
TiDB displays `DATETIME` values in `YYYY-MM-DD HH:MM:SS[.fraction]` format, but permits assignment of values to `DATETIME` columns using either strings or numbers. An optional fsp value in the range from 0 to 6 may be given to specify fractional seconds precision. If omitted, the default precision is 0:
108+
109+
```sql
110+
DATETIME[(fsp)]
111+
```
112+
113+
### `TIMESTAMP` type
114+
115+
`TIMESTAMP` contains both date-portion and time-portion. Valid values range from '1970-01-01 00:00:01.000000' to '2038-01-19 03:14:07.999999' in UTC time. An optional fsp value in the range from 0 to 6 may be given to specify fractional seconds precision. If omitted, the default precision is 0.
116+
117+
In `TIMESTAMP`, zero is not permitted to appear in the month-portion or day-portion. The only exception is zero value itself '0000-00-00 00:00:00'.
118+
119+
```sql
120+
TIMESTAMP[(fsp)]
121+
```
122+
123+
#### Timezone Handling
124+
125+
When `TIMESTAMP` is to be stored, TiDB converts the `TIMESTAMP` value from the current time zone to UTC time zone. When `TIMESTAMP` is to be retrieved, TiDB converts the stored `TIMESTAMP` value from UTC time zone to the current time zone (Note: `DATETIME` is not handled in this way). The default time zone for each connection is the server's local time zone, which can be modified by the environment variable `time_zone`.
126+
127+
> **Warning:**
128+
>
129+
> As in MySQL, the `TIMESTAMP` data type suffers from the [Year 2038 Problem](https://en.wikipedia.org/wiki/Year_2038_problem). For storing values that may span beyond 2038, please consider using the `DATETIME` type instead.
130+
131+
### `YEAR` type
82132
83-
### Automatic initialization and update of `TIMESTAMP` and `DATETIME`
133+
The `YEAR` type is specified in the format 'YYYY'. Supported values range from 1901 to 2155, or the zero value of 0000:
134+
135+
```sql
136+
YEAR[(4)]
137+
```
138+
139+
`YEAR` follows the following format rules:
140+
141+
+ Four-digit numeral ranges from 1901 to 2155
142+
+ Four-digit string ranges from '1901' to '2155'
143+
+ One-digit or two-digit numeral ranges from 1 to 99. Accordingly, 1-69 is converted to 2001-2069 and 70-99 is converted to 1970-1999
144+
+ One-digit or two-digit string ranges from '0' to '99'
145+
+ Value 0 is taken as 0000 whereas the string '0' or '00' is taken as 2000
146+
147+
Invalid `YEAR` value is automatically converted to 0000 (if users are not using the `NO_ZERO_DATE` SQL mode).
148+
149+
## Automatic initialization and update of `TIMESTAMP` and `DATETIME`
84150
85151
Columns with `TIMESTAMP` or `DATETIME` value type can be automatically initialized or updated to the current time.
86152
@@ -104,7 +170,7 @@ CREATE TABLE t1 (
104170
);
105171
```
106172
107-
### Decimal part of time value
173+
## Decimal part of time value
108174
109175
`DATETIME` and `TIMESTAMP` values can contain a fractional part of up to 6 digits which is accurate to milliseconds. In any column of `DATETIME` or `TIMESTAMP` types, a fractional part is stored instead of being discarded. With a fractional part, the value is in the format of 'YYYY-MM-DD HH:MM:SS[.fraction]', and the fraction ranges from 000000 to 999999. A decimal point must be used to separate the fraction from the rest.
110176
@@ -137,7 +203,7 @@ CREATE TABLE t1 (
137203
1 row in set (0.00 sec)
138204
```
139205
140-
### Conversions between date and time types
206+
## Conversions between date and time types
141207
142208
Sometimes we need to make conversions between date and time types. But some conversions might lead to information loss. For example, `DATE`, `DATETIME` and `TIMESTAMP` values all have their own respective ranges. `TIMESTAMP` should be no earlier than the year 1970 in UTC time or no later than UTC time '2038-01-19 03:14:07'. Based on this rule, '1968-01-01' is a valid date value of `DATE` or `DATETIME`, but becomes 0 when it is converted to `TIMESTAMP`.
143209
@@ -178,7 +244,7 @@ mysql> SELECT NOW(), NOW()+0, NOW(3)+0;
178244
+---------------------|----------------|--------------------+
179245
```
180246
181-
### Two-digit year-portion contained in the date
247+
## Two-digit year-portion contained in the date
182248
183249
The two-digit year-portion contained in date does not explicitly indicate the actual year and is ambiguous.
184250
@@ -194,71 +260,3 @@ When numeral `00` is inserted to `YEAR(4)`, the result is 0000 rather than 2000.
194260
If you want the result to be 2000, specify the value to be 2000.
195261
196262
The two-digit year-portion might not be properly calculated in some functions such `MIN()` and `MAX()`. For these functions, the four-digit format suites better.
197-
198-
## Supported types
199-
200-
### `DATE` type
201-
202-
`DATE` only contains date-portion and no time-portion, displayed in `YYYY-MM-DD` format. The supported range is '1000-01-01' to '9999-12-31':
203-
204-
```sql
205-
DATE
206-
```
207-
208-
### `TIME` type
209-
210-
For the `TIME` type, the format is `HH:MM:SS[.fraction]` and valid values range from '-838:59:59.000000' to '838:59:59.000000'. `TIME` is used not only to indicate the time within a day but also to indicate the time interval between 2 events. An optional `fsp` value in the range from 0 to 6 may be given to specify fractional seconds precision. If omitted, the default precision is 0:
211-
212-
```sql
213-
TIME[(fsp)]
214-
```
215-
216-
> **Note:**
217-
>
218-
> Pay attention to the abbreviated form of `TIME`. For example, '11:12' means '11:12:00' instead of '00:11:12'. However, '1112' means '00:11:12'. These differences are caused by the presence or absence of the `:` character.
219-
220-
### `DATETIME` type
221-
222-
`DATETIME` contains both date-portion and time-portion. Valid values range from '1000-01-01 00:00:00.000000' to '9999-12-31 23:59:59.999999'.
223-
224-
TiDB displays `DATETIME` values in `YYYY-MM-DD HH:MM:SS[.fraction]` format, but permits assignment of values to `DATETIME` columns using either strings or numbers. An optional fsp value in the range from 0 to 6 may be given to specify fractional seconds precision. If omitted, the default precision is 0:
225-
226-
```sql
227-
DATETIME[(fsp)]
228-
```
229-
230-
### `TIMESTAMP` type
231-
232-
`TIMESTAMP` contains both date-portion and time-portion. Valid values range from '1970-01-01 00:00:01.000000' to '2038-01-19 03:14:07.999999' in UTC time. An optional fsp value in the range from 0 to 6 may be given to specify fractional seconds precision. If omitted, the default precision is 0.
233-
234-
In `TIMESTAMP`, zero is not permitted to appear in the month-portion or day-portion. The only exception is zero value itself '0000-00-00 00:00:00'.
235-
236-
```sql
237-
TIMESTAMP[(fsp)]
238-
```
239-
240-
#### Timezone Handling
241-
242-
When `TIMESTAMP` is to be stored, TiDB converts the `TIMESTAMP` value from the current time zone to UTC time zone. When `TIMESTAMP` is to be retrieved, TiDB converts the stored `TIMESTAMP` value from UTC time zone to the current time zone (Note: `DATETIME` is not handled in this way). The default time zone for each connection is the server's local time zone, which can be modified by the environment variable `time_zone`.
243-
244-
> **Warning:**
245-
>
246-
> As in MySQL, the `TIMESTAMP` data type suffers from the [Year 2038 Problem](https://en.wikipedia.org/wiki/Year_2038_problem). For storing values that may span beyond 2038, please consider using the `DATETIME` type instead.
247-
248-
### `YEAR` type
249-
250-
The `YEAR` type is specified in the format 'YYYY'. Supported values range from 1901 to 2155, or the zero value of 0000:
251-
252-
```sql
253-
YEAR[(4)]
254-
```
255-
256-
`YEAR` follows the following format rules:
257-
258-
+ Four-digit numeral ranges from 1901 to 2155
259-
+ Four-digit string ranges from '1901' to '2155'
260-
+ One-digit or two-digit numeral ranges from 1 to 99. Accordingly, 1-69 is converted to 2001-2069 and 70-99 is converted to 1970-1999
261-
+ One-digit or two-digit string ranges from '0' to '99'
262-
+ Value 0 is taken as 0000 whereas the string '0' or '00' is taken as 2000
263-
264-
Invalid `YEAR` value is automatically converted to 0000 (if users are not using the `NO_ZERO_DATE` SQL mode).

0 commit comments

Comments
 (0)
Please sign in to comment.