1
0
Files
sql-study/PostgreSQL/time.sql
2025-12-02 19:16:31 +08:00

14 lines
736 B
SQL
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
-- 设置会话时区
SET TIME ZONE 'Asia/Shanghai';
-- AT TIME ZONE 在 PostgreSQL 中是重载的,行为取决于左侧的类型:
-- timestamp WITHOUT time zone AT TIME ZONE zone → 把该 wall time 视为 zone 的本地时间,然后转换为 UTC结果为 timestamptz显示为 UTC 偏移后的时间)。
-- timestamptz AT TIME ZONE zone → 把该瞬间转换成 zone 的本地 wall time结果为 timestamp WITHOUT time zone。
select msg_time at time zone 'Asia/Shanghai' from msg where id = 1;
select msg_time at time zone 'UTC+8' from msg where id = 1;
select to_timestamp(1761850339) AT TIME ZONE 'Asia/Shanghai';
-- 将当前时间转换为Unix时间戳
select EXTRACT(EPOCH FROM current_timestamp(0)) - 60;