在PostgreSQL中,如何处理同一地点在不同时间的情况?

huangapple go评论69阅读模式
英文:

Same location with different time in the year PostgreSQL

问题

我遇到了一个关于timestamptz(带有时区的时间戳)的问题。我有一个纽约时间,例如:下午2点纽约东部标准时间。我将其输入为14:00:00 - 04:00(UTC-4),因为现在是七月。然而,当时间从十一月开始时,它将变为14:00:00 - 05:00(UTC-5)。并且在接下来的一年中,它将回到UTC-4。我该如何自动进行转换?

我希望它能根据系统时间自动转换为UTC-5和UTC-4。

英文:

I got an issue with timestamptz (timestamp with timezone). I have New York time, ex: 2pm NY EST. I will input it as 14:00:00 - 04:00 (UTC-4) because it is July now. However, when it starts from November, it will be 14:00:00 - 05:00 (UTC-5). And it will be turn back to UTC -4 in the following year. How can I do to convert it automatically?

I would like that it will convert to the UTC -5 and UTC-4 automatically depends on the system time.

答案1

得分: 1

如果我理解正确,您可以在SQL查询中使用at time zone 'America/New_York'。类似这样的代码:

postgres=# select '2023-07-15 13:00:00-04:00'::timestamptz at time zone 'America/New_York';
      timezone
---------------------
 2023-07-15 13:00:00
(1 row)

postgres=# select '2023-01-15 13:00:00-04:00'::timestamptz at time zone 'America/New_York';
      timezone
---------------------
 2023-01-15 12:00:00
(1 row)
英文:

If I got the question right You can use at time zone 'America/New_York' in your SQL queries. Something like that:

postgres=# select '2023-07-15 13:00:00-04:00'::timestamptz at time zone 'America/New_York';
      timezone
---------------------
 2023-07-15 13:00:00
(1 row)

postgres=# select '2023-01-15 13:00:00-04:00'::timestamptz at time zone 'America/New_York';
      timezone
---------------------
 2023-01-15 12:00:00
(1 row)

huangapple
  • 本文由 发表于 2023年7月27日 15:20:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/76777332.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定