简体   繁体   English

如何使用 Npgsql 从 Postgresql 检索 UTC 日期时间

[英]How can you retrieve a UTC DateTime from Postgresql using Npgsql

I am using the "timestamp with time zone" column type and setting my datetimes like:我正在使用“带时区的时间戳”列类型并将我的日期时间设置为:

INSERT INTO mytable(col1)
VALUES(timestamp with time zone '2020-07-16 17:45:00.000000+00');

Whenever I try and retrieve my DateTimes they are being converted to the 'local' kind.每当我尝试检索我的 DateTimes 时,它们都会被转换为“本地”类型。

This is not what I want, its set as a UTC datetime and I want it back as UTC, I do not want it converting to local going into the database, nor coming back out ... doing so makes it impossible to get to UTC without causing ambiguity.这不是我想要的,它设置为 UTC 日期时间,我希望它作为 UTC 返回,我不希望它转换为本地进入数据库,也不返回......这样做使得无法到达 UTC不会造成歧义。

I have tried setting the "PGTZ" environment variable;我试过设置“PGTZ”环境变量; I have also executed the following sql when the connection opens:连接打开时,我还执行了以下sql:

Connection.ExecuteAsync( "SET TIMEZONE TO 'UTC'" );

Nothing I have tried seems to work and it seems to be by-design according to various posts like: https://github.com/npgsql/npgsql/issues/347我试过的任何东西似乎都不起作用,根据各种帖子,它似乎是设计的: https : //github.com/npgsql/npgsql/issues/347

This seems utterly wrong to me, but I am hoping someone can point me to a workaround for this.这对我来说似乎完全错误,但我希望有人能指出我的解决方法。

I am using .net5/asp.net and Dapper (not EFCore) and Npgsql version 4.1.5.我正在使用 .net5/asp.net 和 Dapper(不是 EFCore)和 Npgsql 4.1.5 版。

Postgresql timestamptz does not actually store the timezone, it just stores the values as at UTC. Postgresql timestamptz实际上并不存储时区,它只是存储 UTC 时的值。 This means '2020-07-16 17:45:00.000000+00' will not transformed to UTC on entry.这意味着'2020-07-16 17:45:00.000000+00'不会在输入时转换为 UTC。 Without a specified time zone it would be rotated from the TimeZone setting to UTC .如果没有指定时区,它将从TimeZone设置轮换到UTC On retrieval though it will use the TimeZone setting to rotate back to that time zone.在检索时,它会使用TimeZone设置旋转回该时区。 I suspect your setting is not happening in the same session that the timestamp retrieval is taking place.我怀疑您的设置没有发生在时间戳检索发生的同一会话中。 I would try something like SELECT timestamp_fld AT TIME ZONE 'UTC'我会尝试类似SELECT timestamp_fld AT TIME ZONE 'UTC'

This is indeed by design, and matches the PostgreSQL when querying timestamptz values in regular text queries.这确实是设计使然,并且在常规文本查询中查询 timestamptz 值时与 PostgreSQL 匹配。 If you use fire up psql or pgadmin and select a timestamptz column, you'll see a local timestamp based on on your TimeZone session parameter.如果您使用启动 psql 或 pgadmin 并选择一个时间戳列,您将看到基于您的TimeZone会话参数的本地时间戳。

As mentioned by @adrian-klaver, timestamptz isn't about storing the timezone in the database - it's about applying timezone conversions when reading and writing timestamp values to the database, based on the TimeZone parameter.正如@adrian-klaver 所提到的,timestamptz 并不是将时区存储在数据库中——而是在基于TimeZone参数将时间戳值读取和写入数据库时​​应用时区转换。 If you don't want any such conversions, consider switching to timestamp (without time zone), and having all timestamps as UTC by convention.如果您不想要任何此类转换,请考虑切换到时间戳(不带时区),并按照约定将所有时间戳设为 UTC。

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

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