gpt4 book ai didi

sql - 获取给定时间戳和 TZ 名称的 TZ 缩写

转载 作者:行者123 更新时间:2023-11-29 11:54:10 26 4
gpt4 key购买 nike

转换 timestamptz 类型的列后,是否有可能获得正确的 TZ 缩写?

例如,我所追求的是显示“美国/太平洋”时区的“2016-06-16 16:00:00-04” 目标 TZ 缩写。我可以很容易地得到这个:

06/16/2016 01:00 pm

但我无法得到以下结果:

06/16/2016 01:00 pm PDT

转换后,timestamptz 变为 timestamp without timezone 并且 to_char specifier 'TZ' 变得毫无意义...

这是我尝试过的例子:

SELECT
scheduled_dt AS scheduled_dt
, to_char(scheduled_dt, 'TZ') AS scheduled_dt_orig_tz
-- correct value, but missing TZ
, timezone('US/Pacific', scheduled_dt) AS schedules_dt_converted_tz
, to_char(timezone('US/Pacific', scheduled_dt), 'MM/DD/YYYY HH:MI pm') AS scheduled_dt_converted_tz_localized
-- after conversion TZ is lost
, to_char(timezone('US/Pacific', scheduled_dt), 'MM/DD/YYYY HH:MI pm TZ') AS scheduled_dt_converted_tz_localized_missing_tz
-- with cast, wrong TZ is displayed
, to_char(timezone('US/Pacific', scheduled_dt)::timestamptz, 'MM/DD/YYYY HH:MI pm TZ') AS scheduled_dt_converted_tz_localized_wrong_tz
FROM orders
LIMIT 5

和示例输出:

"scheduled_dt","scheduled_dt_orig_tz","schedules_dt_converted_tz","scheduled_dt_converted_tz_localized","scheduled_dt_converted_tz_localized_missing_tz","scheduled_dt_converted_tz_localized_wrong_tz"
"2016-06-16 16:00:00-04","EDT","2016-06-16 13:00:00","06/16/2016 01:00 pm","06/16/2016 01:00 pm ","06/16/2016 01:00 pm EDT"
"2014-07-15 08:00:00-04","EDT","2014-07-15 05:00:00","07/15/2014 05:00 am","07/15/2014 05:00 am ","07/15/2014 05:00 am EDT"
"2012-11-27 10:15:00-05","EST","2012-11-27 07:15:00","11/27/2012 07:15 am","11/27/2012 07:15 am ","11/27/2012 07:15 am EST"
"2011-03-02 08:30:00-05","EST","2011-03-02 05:30:00","03/02/2011 05:30 am","03/02/2011 05:30 am ","03/02/2011 05:30 am EST"
"2016-02-12 14:23:09-05","EST","2016-02-12 11:23:09","02/12/2016 11:23 am","02/12/2016 11:23 am ","02/12/2016 11:23 am EST"

最佳答案

不,因为timestamptz(又名timestamp with time zone)isn't really a timestamp with a time zone ,就存储的内容而言...您提供时间戳和时区,并且在内部它只是存储为 UTC 值。

听起来基本上您应该将目标时区与时间戳一起存储在单独的字段中。

关于sql - 获取给定时间戳和 TZ 名称的 TZ 缩写,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39472247/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com