简体   繁体   English

在Npgsql中使用带时区的时间戳

[英]Using timestamp with time zone with Npgsql

There is a need to save timestamp with time zone in database. 需要在数据库中保存带有时区的时间戳。 Target field in table is declared as 表中的目标字段声明为

"UploadTime" timestamp with time zone

I'm inserting data using command parameters : 我正在使用命令参数插入数据:

var uploadTime = DateTimeOffset.Now;
var insertQuery = @"INSERT INTO ""Table"" (""UploadTime"") VALUES :uploadTime"; 
var uploadTimeParam = new NpgsqlParameter("uploadTime", NpgsqlDbType.TimestampTZ);
uploadTimeParam.Value = uploadTime;
var insertCommand = new NpgsqlCommand(insertQuery, databaseConnection);
insertCommand.Parameters.Add(uploadTimeParam);

Value is stored and returned as DateTime without time zone. 值被存储并作为不带时区的DateTime返回。 I guess that's because "timestamp with timezone" database type is mapped to DateTime by default. 我猜这是因为默认情况下,“带时区的时间戳”数据库类型被映射到DateTime。 Probably that can be fixed via checking out sources and making respective changes to TimestampTZ, but it will interfere with idea of using Nuget to manage project dependencies. 可能可以通过检出源并对TimestampTZ进行相应更改来解决此问题,但这会干扰使用Nuget管理项目依赖项的想法。 Maybe there are some less complicated ways to achieve that? 也许有一些不太复杂的方法可以实现这一目标? Storing ticks and converting them to DateTimeOffset is an overhead, hack with "SET TIME ZONE INTERVAL" before query execution didn't work in my case. 存储滴答并将其转换为DateTimeOffset是一项开销,在我的情况下,在执行查询之前无法使用“ SET TIME ZONE INTERVAL”进行设置。 Thanks in advance for any solutions. 在此先感谢您提供任何解决方案。

通过存储其他手动映射到DateTimeOffset的时间戳RTT字段来解决问题。

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

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