简体   繁体   English

如何在 postgresql 中使用时区更新日期?

[英]How to update a date with a time zone in postgresql?

I want to update a date with a timezone (+2 hours) but it ends up as UTC (0 hours)我想用时区(+2 小时)更新日期,但它最终为 UTC(0 小时)

Date type is 'timestamp-with-timezone'日期类型是 'timestamp-with-timezone'

Query...询问...

update table set date = '2022-05-25 13:28+02:00'

will end up as this in the database.将在数据库中结束。

2022-05-25 11:28:00+00

What's wrong here?这里有什么问题?

tl;dr tl;博士

Nothing wrong.没有错。 Postgres stores values of TIMESTAMP WITH TIME ZONE in UTC, always an offset from UTC of zero. Postgres 以 UTC 存储TIMESTAMP WITH TIME ZONE的值,始终与 UTC 的偏移量为零。 Any submitted offset or zone is used to adjust to UTC.任何提交的偏移量或区域都用于调整到 UTC。

Details细节

Date type is 'timestamp-with-timezone'日期类型是 'timestamp-with-timezone'

No such type in standard SQL, nor in Postgres.标准 SQL 和 Postgres 中都没有这种类型。

I'll assume you meant TIMESTAMP WITH TIME ZONE .我假设您的意思是TIMESTAMP WITH TIME ZONE

it ends up as UTC (0 hours)它以 UTC 结尾(0 小时)

Read the fine manual.阅读精美的手册。 You are seeing documented behavior.您正在看到记录在案的行为。

Postgres always stores values in a column of type TIMESTAMP WITH TIME ZONE in UTC, that is, with an offset of zero hours-minutes-seconds. Postgres 总是将值存储在 UTC 中的TIMESTAMP WITH TIME ZONE类型的列中,也就是说,偏移量为零小时-分钟-秒。

Any time zone or offset provided with an input is used to adjust into UTC.输入提供的任何时区或偏移量都用于调整为 UTC。 That provided zone or offset is then discarded.然后丢弃提供的区域或偏移量。

So the name of the type TIMESTAMP WITH TIME ZONE is a misnomer.所以TIMESTAMP WITH TIME ZONE类型的名称是用词不当。 First, the authors of the SQL were thinking in terms of offset, not real time zones.首先,SQL 的作者考虑的是偏移量,而不是实时时区。 Second, any submitted time zone is not stored.其次,存储任何提交的时区。 A submitted zone is used to adjust and then discarded.提交的区域用于调整然后丢弃。

If you need to track the original offset or zone, add an extra column.如果您需要跟踪原始偏移量或区域,请添加额外的列。 You'll have to add code to store the offset amount or the time zone name.您必须添加代码来存储偏移量或时区名称。

update table set date = '2022-05-25 13:28+02:00' will end up as this in the database. update table set date = '2022-05-25 13:28+02:00'将在数据库中结束。 2022-05-25 11:28:00+00 What's wrong here? 2022-05-25 11:28:00+00这里怎么了?

Nothing is wrong.没有什么是错的。 That is a feature, not a bug.这是一个特性,而不是一个错误。 Both of those strings represent the very same simultaneous moment.这两个字符串都代表相同的同时时刻。


FYI, database engines vary widely in their behavior handling date-time types and behaviors.仅供参考,数据库引擎在处理日期时间类型和行为的行为方面差异很大

Some do as Postgres does regarding TIMESTAMP WITH TIME ZONE , adjusting to UTC and then discarding any provided time zone or offset.有些人像 Postgres 对TIMESTAMP WITH TIME ZONE所做的那样,调整为 UTC,然后丢弃任何提供的时区或偏移量。 Some others may not.其他一些可能不会。

The SQL standard barely touches on the topic of date-time handling. SQL 标准几乎没有涉及日期时间处理的主题。 It declares a few types, and does that poorly with incomplete coverage of all cases.它声明了几种类型,并且在所有情况下都覆盖不完整的情况下做得很差。 And the standard neglects to define behavior.该标准忽略了定义行为。

So, be very careful when it comes to date-time handling in your database work.因此,在数据库工作中处理日期时间时要非常小心。 Read very carefully the documentation for your particular database engine.仔细阅读特定数据库引擎的文档。 Do not make assumptions.不要做假设。 Run experiments to validate your understanding.运行实验以验证您的理解。 And know that writing portable SQL code for date-time may not be feasible.并且知道为日期时间编写可移植的 SQL 代码可能不可行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM