示例:
SELECT
SESSIONTIMEZONE,
DT,
TRUNC( (DT - date '1970-01-01') * 86400) AS EPOCH_DT
FROM "test_table"
输出
会话时区 | DT | EPOCH_DT |
---|---|---|
欧洲/巴黎 | 1970-01-02 00:00:00 | 86400 |
所需的输出(tz_offset +01:00)将如下所示:
会话时区 | DT | EPOCH_DT |
---|---|---|
欧洲/巴黎 | 1970-01-02 00:00:00 | 82800 |
如何更改以下内容以便在计算 Unix 时间时考虑时区?
TRUNC( (DT - date '1970-01-01') * 86400)
您可以使用:
SELECT SESSIONTIMEZONE,
DT,
(
CAST(
FROM_TZ(
CAST(DT AS TIMESTAMP),
SESSIONTIMEZONE
) AT TIME ZONE 'UTC'
AS DATE
)
- DATE '1970-01-01'
) * 86400 AS EPOCH_DT
FROM test_table
对于给定的设置:
ALTER SESSION SET TIME_ZONE = 'Europe/Paris';
CREATE TABLE test_table (dt) AS
SELECT DATE '1970-01-02' FROM DUAL;
输出:
会话时区 | DT | EPOCH_DT |
---|---|---|
欧洲/巴黎 | 1970-01-02 00:00:00 | 82800 |