在 PostgreSQL 中存储基于 NodaTime 的值的最佳方法是什么?
What is the best way to store NodaTime based values in PostgreSQL?
我们正在实施 PostgreSQL,并且还决定使用 NodaTime 而不是标准的 .NET DateTime。 PostgreSQL 有一个 "timestamp with time zone" 类型,NodaTime returns 一个日期和时间值,通过 SystemClock.Instance.Now 方法带有时区。但是,SystemClock.Instance.Now (2015-07-17T13:22:52Z) 返回的值不能直接写入 PostgreSQL 字段。是否有处理 NodaTime/PostgreSQL 实施的最佳实践?
据我了解,"timestamp with time zone" 无论如何都是用词不当 - 它只是 "you supply a time zone with a local value, PostgreSQL will store UTC"...换句话说,它实际上是一个额外的转换,而不是存储更多数据。
如果您只是想存储 Instant
,那么 timestamp
应该可以作为一种数据类型。在将值获取到数据库方面,您可能应该将其转换为 DateTimeOffset
,然后让驱动程序处理它。检索时需要使用 Instant.ToDateTimeOffset
when storing, and Instant.FromDateTimeOffset
。
我们正在实施 PostgreSQL,并且还决定使用 NodaTime 而不是标准的 .NET DateTime。 PostgreSQL 有一个 "timestamp with time zone" 类型,NodaTime returns 一个日期和时间值,通过 SystemClock.Instance.Now 方法带有时区。但是,SystemClock.Instance.Now (2015-07-17T13:22:52Z) 返回的值不能直接写入 PostgreSQL 字段。是否有处理 NodaTime/PostgreSQL 实施的最佳实践?
据我了解,"timestamp with time zone" 无论如何都是用词不当 - 它只是 "you supply a time zone with a local value, PostgreSQL will store UTC"...换句话说,它实际上是一个额外的转换,而不是存储更多数据。
如果您只是想存储 Instant
,那么 timestamp
应该可以作为一种数据类型。在将值获取到数据库方面,您可能应该将其转换为 DateTimeOffset
,然后让驱动程序处理它。检索时需要使用 Instant.ToDateTimeOffset
when storing, and Instant.FromDateTimeOffset
。