PostgreSQL的timestamptz中如何处理TIMEZONE
How to deal with TIMEZONE in PostgreSQL's timestamptz
我有 Users
table 和 timestamptz
类型的 birthdate
列。
TIMEZONE
在 PostgreSQL 中设置为 UTC
。
我在代码(.NET Core)中按照以下方式设置 birthdate
:
user.birthDate = DateTime.UtcNow;
如果我将 TIMEZONE
设置为 America\Los Angeles
并保存我的日期(.NET 代码的 UTC 表示)会发生什么?我的 UTC
表示将被视为 America\Los Angeles
并且会导致无效数据表示?
我应该始终将 TIMEZONE
设置为 UTC
吗?这是 timestamptz
的最佳做法吗?
https://www.postgresql.org/docs/12/datatype-datetime.html#DATATYPE-TIMEZONES 说
All timezone-aware dates and times are stored internally in UTC. They
are converted to local time in the zone specified by the TimeZone
configuration parameter before being displayed to the client.
在您的客户端代码中,您可以在会话级别更改时区参数;在数据库服务器级别使用 timestamptz(带时区的时间戳),无需更改。
使用带时区的时间戳而不是不带时区的时间戳被认为是最佳实践:参见https://tapoueh.org/blog/2018/04/postgresql-data-types-date-timestamp-and-time-zones/#date-time-and-time-zonesenter link description here
我有 Users
table 和 timestamptz
类型的 birthdate
列。
TIMEZONE
在 PostgreSQL 中设置为 UTC
。
我在代码(.NET Core)中按照以下方式设置 birthdate
:
user.birthDate = DateTime.UtcNow;
如果我将 TIMEZONE
设置为 America\Los Angeles
并保存我的日期(.NET 代码的 UTC 表示)会发生什么?我的 UTC
表示将被视为 America\Los Angeles
并且会导致无效数据表示?
我应该始终将 TIMEZONE
设置为 UTC
吗?这是 timestamptz
的最佳做法吗?
https://www.postgresql.org/docs/12/datatype-datetime.html#DATATYPE-TIMEZONES 说
All timezone-aware dates and times are stored internally in UTC. They are converted to local time in the zone specified by the TimeZone configuration parameter before being displayed to the client.
在您的客户端代码中,您可以在会话级别更改时区参数;在数据库服务器级别使用 timestamptz(带时区的时间戳),无需更改。
使用带时区的时间戳而不是不带时区的时间戳被认为是最佳实践:参见https://tapoueh.org/blog/2018/04/postgresql-data-types-date-timestamp-and-time-zones/#date-time-and-time-zonesenter link description here