如何将最后四个字符是时区偏移量的 'YYYY-DD-MM HH:MI:SS.MS +00:00' 形式的字符串转换为 postgres timestamptz?

How do I convert a string of the form 'YYYY-DD-MM HH:MI:SS.MS +00:00' where last four chars are a timezone offset into a postgres timestamptz?

如何将最后四个字符是时区偏移量的 'YYYY-DD-MM HH:MI:SS.MS +00:00' 形式的字符串转换为 postgres timestamptz?

这似乎不起作用:

select '2018-06-13 04:11:46.873 -07:00'::timestamptz;
        timestamptz         
----------------------------
 2018-06-13 11:11:46.873+00
(1 row)

输出正确。类型 timestamp with time zone 有点误导,行为不同于例如在甲骨文中。

quote from the manual

For timestamp with time zone, the internally stored value is always in UTC (Universal Coordinated Time, traditionally known as Greenwich Mean Time, GMT). An input value that has an explicit time zone specified is converted to UTC using the appropriate offset for that time zone.

输入的'2018-06-13 04:11:46.873 -07:00'::timestamptz根据给定的时区偏移量转换为对应的UTC值。 That 值然后转换为您的会话时区并发送到客户端,然后在您的会话时区中产生 2018-06-13 11:11:46.873+00