时区

flink包含大量时间相关函数,时间相关函数与时区紧密相关,在任务开发页面的运行参数配置时区偏移量。

默认时区

系统默认时区为东八区:GMT+08:00

更改任务默认时区

SQL任务中配置东八区:

set 'zone_offset.of_hour' = '8';

例如标准时区:

set 'zone_offset.of_hour' = '0';
参数 默认值 类型 描述
table.local-time-zone "default" String e.g. "GMT+8:00"代表东八区,或"Asia/Shanghai"

举例说明

原始字段类型 原始值 zone_offset.of_hour 转化函数 目标字段类型 结果
String '2020-01-01 11:12:13' 18 UNIX_TIMESTAMP(ts) Bigint 1577812333
String '2020-01-01 11:12:13' 8 UNIX_TIMESTAMP(ts) Bigint 1577848333
String '2020-01-01 11:12:13' 18 cast(ts as TIMESTAMP(3)) TIMESTAMP '2020-01-01T11:12:13'
String '2020-01-01 11:12:13' 8 cast(ts as TIMESTAMP(3)) TIMESTAMP '2020-01-01T11:12:13'
Bigint 1577812333 18 FROM_UNIXTIME(ts) String '2020-01-01 11:12:13'
Bigint 1577812333 8 FROM_UNIXTIME(ts) String '2020-01-01 01:12:13'
TIMESTAMP(3) '2020-01-01 11:12:13' 18 cast(ts as TIMESTAMP(3) WITH LOCAL TIME ZONE) TIMESTAMP(3) WITH LOCAL TIME ZONE '2019-12-31T17:12:13Z'
TIMESTAMP(3) '2020-01-01 11:12:13' 8 cast(ts as TIMESTAMP(3) WITH LOCAL TIME ZONE) TIMESTAMP(3) WITH LOCAL TIME ZONE '2019-01-01T03:12:13Z'
TIMESTAMP(3) WITH LOCAL TIME ZONE '2020-07-10T03:07:54.719Z' 8 UNIX_TIMESTAMP(ts) TIMESTAMP(3) '2020-07-10T11:07:54.719'
TIMESTAMP(3) WITH LOCAL TIME ZONE '2020-07-10T03:07:54.719Z' 2 UNIX_TIMESTAMP(ts) TIMESTAMP(3) '2020-07-10T05:07:54.719'

常用时间函数

  • UNIX_TIMESTAMP(string1[, string2]) - string1是时间字符串, string2默认'yyyy-MM-dd HH:mm:ss'格式可以不填,返回Unix timestamp,单位:秒。

  • FROM_UNIXTIME(numeric[, string]) - numeric是Unix timestamp单位:秒,string默认'yyyy-MM-dd HH:mm:ss'格式可以不填。

  • TO_TIMESTAMP(string1[, string2]) - string1是时间字符串, string2默认'yyyy-MM-dd HH:mm:ss'格式可以不填,返回timestamp类型。

  • select NOW(), PROCTIME(), CURRENT_TIMESTAMP, CURRENT_DATE, CURRENT_TIME, LOCALTIMESTAMP, LOCALTIME;

执行以上SQL的北京时间(UTC+8):2021-08-26T11:16:51.714

请注意部分函数时间相差8小时

NOW() PROCTIME() CURRENT_TIMESTAMP CURRENT_DATE CURRENT_TIME LOCALTIMESTAMP LOCALTIME
2021-08-26T03:16:51.714 2021-08-26T03:16:51.714 2021-08-26T03:16:51.714 2021-08-26 03:16:51.714 2021-08-26T11:16:51.714 11:16:51.714