mirror of
https://github.com/ClickHouse/ClickHouse.git
synced 2024-10-20 23:41:01 +00:00
3a8dbf4341
* DOCAPI-4146: Updated examples. * DOCAPI-4146: Fix. * DOCAPI-4146: Clarifications about dictionaries. * Update datetime.md (#69) * DOCAPI-4146: EN review, RU translation * DOCAPI-4146: Fix. * DOCAPI-4146: Attempted to clarify. * DOCAPI-4146: More clarifications * DOCAPI-4146: Extended example. * Update docs/en/data_types/datetime.md Co-Authored-By: Ivan Blinkov <github@blinkov.ru> * Update docs/en/data_types/datetime.md Co-Authored-By: Ivan Blinkov <github@blinkov.ru> * Update docs/en/data_types/datetime.md Co-Authored-By: Ivan Blinkov <github@blinkov.ru> * Fix. * DOCAPI-4146: Clarifications and fixes. Co-authored-by: FeehanG <51821376+FeehanG@users.noreply.github.com> Co-authored-by: Ivan Blinkov <github@blinkov.ru>
91 lines
4.3 KiB
Markdown
91 lines
4.3 KiB
Markdown
# DateTime {#data_type-datetime}
|
|
|
|
Allows to store an instant in time, that can be expressed as a calendar date and a time of a day. `DateTime` allows to take into account time zones for stored values.
|
|
|
|
Syntax:
|
|
|
|
```sql
|
|
DateTime([timezone])
|
|
```
|
|
|
|
Supported range of values: [1970-01-01 00:00:00, 2105-12-31 23:59:59].
|
|
|
|
Resolution: 1 second.
|
|
|
|
SELECT toDateTime(0)
|
|
FORMAT TSV
|
|
|
|
0000-00-00 00:00:00
|
|
|
|
|
|
## Usage Remarks
|
|
|
|
A moment of time is stored as [Unix timestamp](https://en.wikipedia.org/wiki/Unix_time), independently of time zones and daylight savings. Additionally `DateTime` can store time zone, that affects how `DateTime` values are displayed in text format and how input strings are parsed for storage. The `tzdata` package, containing [IANA Time Zone Database](https://www.iana.org/time-zones), should be installed in the system. Use the `timedatectl list-timezones` command to list timezones known by a local system.
|
|
|
|
You can explicitly set a time zone for `DateTime`-type columns when creating a table. If the time zone isn't set, ClickHouse uses the value of the [timezone](../operations/server_settings/settings.md#server_settings-timezone) parameter in the server settings or the operating system settings at the moment of the ClickHouse server start.
|
|
|
|
The [clickhouse-client](../interfaces/cli.md) applies the server time zone by default if a time zone isn't explicitly set when initializing the data type. To use the client time zone, run `clickhouse-client` with the `--use_client_time_zone` parameter.
|
|
|
|
ClickHouse outputs values in `YYYY-MM-DD hh:mm:ss` text format by default. You can change the format with the [formatDateTime](../query_language/functions/date_time_functions.md#formatdatetime) function.
|
|
|
|
When inserting data into ClickHouse, you can use different formats of date and time strings, depending on the value of the [date_time_input_format](../operations/settings/settings.md#settings-date_time_input_format) setting.
|
|
|
|
## Examples
|
|
|
|
**1.** Creating a table with a `DateTime`-type column and inserting data into it:
|
|
|
|
```sql
|
|
CREATE TABLE dt
|
|
(
|
|
`timestamp` DateTime('Europe/Moscow'),
|
|
`event_id` UInt8
|
|
)
|
|
ENGINE = TinyLog
|
|
```
|
|
```sql
|
|
INSERT INTO dt Values (1546300800, 1), ('2019-01-01 00:00:00', 2)
|
|
```
|
|
```sql
|
|
SELECT * FROM dt
|
|
```
|
|
```text
|
|
┌───────────timestamp─┬─event_id─┐
|
|
│ 2019-01-01 03:00:00 │ 1 │
|
|
│ 2019-01-01 00:00:00 │ 2 │
|
|
└─────────────────────┴──────────┘
|
|
```
|
|
|
|
Unix timestamp `1546300800` represents the `'2019-01-01 00:00:00'` date and time in `Europe/London` (UTC+0) time zone, but the `timestamp` column stores values in the `Europe/Moscow` (UTC+3) timezone, so the value inserted as Unix timestamp is formatted as `2019-01-01 03:00:00`.
|
|
|
|
```sql
|
|
SELECT * FROM dt WHERE timestamp = toDateTime('2019-01-01 00:00:00', 'Europe/Moscow')
|
|
```
|
|
```text
|
|
┌───────────timestamp─┬─event_id─┐
|
|
│ 2019-01-01 00:00:00 │ 2 │
|
|
└─────────────────────┴──────────┘
|
|
```
|
|
|
|
**2.** Getting a time zone for a `DateTime`-type value:
|
|
|
|
```sql
|
|
SELECT toDateTime(now(), 'Europe/Moscow') AS column, toTypeName(column) AS x
|
|
```
|
|
```text
|
|
┌──────────────column─┬─x─────────────────────────┐
|
|
│ 2019-10-16 04:12:04 │ DateTime('Europe/Moscow') │
|
|
└─────────────────────┴───────────────────────────┘
|
|
```
|
|
|
|
## See Also
|
|
|
|
- [Type conversion functions](../query_language/functions/type_conversion_functions.md)
|
|
- [Functions for working with dates and times](../query_language/functions/date_time_functions.md)
|
|
- [Functions for working with arrays](../query_language/functions/array_functions.md)
|
|
- [The `date_time_input_format` setting](../operations/settings/settings.md#settings-date_time_input_format)
|
|
- [The `timezone` server configuration parameter](../operations/server_settings/settings.md#server_settings-timezone)
|
|
- [Operators for working with dates and times](../query_language/operators.md#operators-datetime)
|
|
- [The `Date` data type](date.md)
|
|
|
|
[Original article](https://clickhouse.yandex/docs/en/data_types/datetime/) <!--hide-->
|