From aa2318b5eaf5edf34d68a7ddb6a74097105e908c Mon Sep 17 00:00:00 2001 From: Fortern Date: Fri, 31 Oct 2025 03:36:30 +0800 Subject: [PATCH] PostgreSQL time --- PostgreSQL/time.sql | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 PostgreSQL/time.sql diff --git a/PostgreSQL/time.sql b/PostgreSQL/time.sql new file mode 100644 index 0000000..8ee4427 --- /dev/null +++ b/PostgreSQL/time.sql @@ -0,0 +1,10 @@ +-- 设置会话时区 +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';