[英]Insert time with timezone daylight savings
I would like to insert time data type in postgresql that includes the timezone and is aware of daylight savings time.我想在包含时区并知道夏令时的 postgresql 中插入时间数据类型。 This is what I have done:
这就是我所做的:
CREATE TABLE mytable(
...
start_time time(0) with time zone,
end_time time(0) with time zone
)
INSERT INTO mytable(start_time, end_time)
VALUES(TIME '08:00:00 MST7MDT', TIME '18:00:00 MST7MDT')
I get the following error:我收到以下错误:
invalid input syntax for type time: "08:00:00 MST7MDT"
类型时间的无效输入语法:“08:00:00 MST7MDT”
It works if I use 'MST' instead of 'MST7MDT', but I need it to be aware of DST.如果我使用“MST”而不是“MST7MDT”,它会起作用,但我需要它了解夏令时。 I also tried using 'America/Edmonton' as the timezone, but I got the same error.
我也尝试使用“美国/埃德蒙顿”作为时区,但我遇到了同样的错误。
What is the proper way to insert a time value (not timestamp) with timezone and DST?使用时区和 DST 插入时间值(不是时间戳)的正确方法是什么?
EDIT: I would actually like to use the 'America/Edmonton' syntax编辑:我实际上想使用“美国/埃德蒙顿”语法
The proper way is not to use time with time zone
(note the space between time
and zone
) at all, since it is broken by design.正确的方法是根本不使用
time with time zone
(注意time
和zone
之间的空间),因为它被设计破坏了。 It is in the SQL standard, so Postgres supports the type - but advises not to use it.它在 SQL 标准中,因此 Postgres 支持该类型 - 但建议不要使用它。 More in this related answer:
更多在这个相关的答案:
Since you are having problems with DST , timetz
(short name) is a particularly bad choice.由于您遇到了DST问题,
timetz
(简称)是一个特别糟糕的选择。 It is ill-equipped to deal with DST.处理夏令时的能力不足。 It's impossible to tell whether
8:00:00
is in winter or summer time.无法判断
8:00:00
是冬令时还是夏令时。
Use timestamp with time zone
( timstamptz
) instead.改用
timestamp with time zone
( timstamptz
) 。 You can always discard the date part.您可以随时丢弃日期部分。 Simply use
start_time::time
to get the local time from a timestamptz
.只需使用
start_time::time
time 从timestamptz
获取本地时间。 Or use AT TIME ZONE
to transpose to your time zone.或使用
AT TIME ZONE
转换为您的时区。
Generally, to take DST into account automatically , use a time zone name instead of a time zone abbreviation.通常,要自动考虑 DST ,请使用时区名称而不是时区缩写。 More explanation in this related question & answer:
此相关问答中的更多解释:
In your particular case, you could probably use America/Los_Angeles
(example with timestamptz
):在您的特定情况下,您可能可以使用
America/Los_Angeles
(例如timestamptz
):
INSERT INTO mytable(start_time, end_time)
VALUES
('1970-01-01 08:00:00 America/Los_Angeles'
, '1970-01-01 18:00:00 America/Los_Angeles')
I found this by checking:我通过检查发现了这一点:
SELECT * FROM pg_timezone_names
WHERE utc_offset = '-07:00'
AND is_dst;
Basics about time zone handling:关于时区处理的基础知识:
How about this?这个怎么样?
INSERT INTO mytable(start_time, end_time)
VALUES('08:00:00'::time at time zone 'MST7MDT', '18:00:00'::time at time zone 'MST7MDT')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.