日期类型,用两个字节存储,表示从 1970-01-01 (无符号) 到当前的日期值。允许存储从 Unix 纪元开始到编译阶段定义的上限阈值常量(目前上限是2106年,但最终完全支持的年份为2105)。最小值输出为1970-01-01。
值的范围: [1970-01-01, 2149-06-06]。
日期中没有存储时区信息。
Example
Creating a table with a Date
-type column and inserting data into it:
CREATE TABLE dt ( `timestamp` Date, `event_id` UInt8 ) ENGINE = TinyLog;
INSERT INTO dt VALUES (1546300800, 1), ('2019-01-01', 2); SELECT * FROM dt;
┌──timestamp─┬─event_id─┐ │ 2019-01-01 │ 1 │ │ 2019-01-01 │ 2 │ └────────────┴──────────┘
===================================================
时间戳类型。用四个字节(无符号的)存储 Unix 时间戳)。允许存储与日期类型相同的范围内的值。最小值为 1970-01-01 00:00:00。时间戳类型值精确到秒(没有闰秒)。
值的范围: [1970-01-01 00:00:00, 2106-02-07 06:28:15]。
使用启动客户端或服务器时的系统时区,时间戳是从文本(分解为组件)转换为二进制并返回。在文本格式中,有关夏令时的信息会丢失。
默认情况下,客户端连接到服务的时候会使用服务端时区。您可以通过启用客户端命令行选项 --use_client_time_zone
来设置使用客户端时间。
因此,在处理文本日期时(例如,在保存文本转储时),请记住在夏令时更改期间可能存在歧义,如果时区发生更改,则可能存在匹配数据的问题。
1. Creating a table with a DateTime
-type column and inserting data into it:
CREATE TABLE dt ( `timestamp` DateTime('Asia/Istanbul'), `event_id` UInt8 ) ENGINE = TinyLog;
INSERT INTO dt Values (1546300800, 1), ('2019-01-01 00:00:00', 2);
SELECT * FROM dt;
┌───────────timestamp─┬─event_id─┐ │ 2019-01-01 03:00:00 │ 1 │ │ 2019-01-01 00:00:00 │ 2 │ └─────────────────────┴──────────┘
When inserting datetime as an integer, it is treated as Unix Timestamp (UTC). 1546300800
represents '2019-01-01 00:00:00'
UTC. However, as timestamp
column has Asia/Istanbul
(UTC+3) timezone specified, when outputting as string the value will be shown as '2019-01-01 03:00:00'
When inserting string value as datetime, it is treated as being in column timezone. '2019-01-01 00:00:00'
will be treated as being in Asia/Istanbul
timezone and saved as 1546290000
.
2. Filtering on DateTime
values
SELECT * FROM dt WHERE timestamp = toDateTime('2019-01-01 00:00:00', 'Asia/Istanbul')
┌───────────timestamp─┬─event_id─┐ │ 2019-01-01 00:00:00 │ 2 │ └─────────────────────┴──────────┘
DateTime
column values can be filtered using a string value in WHERE
predicate. It will be converted to DateTime
automatically:
SELECT * FROM dt WHERE timestamp = '2019-01-01 00:00:00'
┌───────────timestamp─┬─event_id─┐ │ 2019-01-01 03:00:00 │ 1 │ └─────────────────────┴──────────┘
3. Getting a time zone for a DateTime
-type column:
SELECT toDateTime(now(), 'Asia/Istanbul') AS column, toTypeName(column) AS x
┌──────────────column─┬─x─────────────────────────┐ │ 2019-10-16 04:12:04 │ DateTime('Asia/Istanbul') │ └─────────────────────┴───────────────────────────┘
4. Timezone conversion
SELECT toDateTime(timestamp, 'Europe/London') as lon_time, toDateTime(timestamp, 'Asia/Istanbul') as mos_time FROM dt
┌───────────lon_time──┬────────────mos_time─┐ │ 2019-01-01 00:00:00 │ 2019-01-01 03:00:00 │ │ 2018-12-31 21:00:00 │ 2019-01-01 00:00:00 │ └─────────────────────┴─────────────────────┘
As timezone conversion only changes the metadata, the operation has no computation cost.
最新评论: