使用System.Text.Json序列化DateTimeOffset时如何影响时区? [英] How to affect timezone when serializing DateTimeOffset with System.Text.Json?

查看:206
本文介绍了使用System.Text.Json序列化DateTimeOffset时如何影响时区?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

要解决的任务:

给出 DateTimeOffset ,其值有一个特定的偏移量。我想使用它所表示的确切形式对其进行序列化,使之具有确切的日期和时间值以及完全相同的偏移值

Given a DateTimeOffset, its value has a particular Offset. I would like to serialize it with the exact form as it was represented, with that exact date and time value and exact same Offset value

问题

当前在序列化矩时是正确的,但是格式化的时间点序列化版本将转换为偏移量,该偏移量是根据当前时区。 (这样虽然时间点仍然正确,但是原始偏移信息丢失了)

Currently when serializing the moment is correct, but the formatted serialized version of the point in time is converted to the offset which is according to the current timezone. (This way although the point in time remains correct, but the original offset information lost)

示例

给出一个 DateTimeOffset ,日期:2019-07-26,时间:00:00:00,偏移量:-05:00
在一个系统,当当前时区为结果提供该特定日期和时间偏移+01:00时,DateTimeOffset将被序列化为:

Given a DateTimeOffset with date: 2019-07-26 and time: 00:00:00 and offset: -05:00 In a system when the current timezone gives the result the this particular date and time has offset +01:00, the DateTimeOffset will be serialized as:


2019-07-26T00:06:00 + 01:00

2019-07-26T00:06:00+01:00

我想将其序列化为


2019-07-26T00:00:00-05:00

2019-07-26T00:00:00-05:00



<我知道两者是相等的时刻。我也确实知道时区和偏移量之间的区别,并且当措辞为时要小心,当当前时区给出结果时,该特定日期和时间的偏移量为+01:00

在我们的持久性和业务记录中,不仅确切的时间点很重要,而且偏移量本身也具有意义。

In our persistence and business records not only the exact point in time matters, but the offset itself also has meaning.

我要做什么已经尝试过:

我看到在 Utf8JsonWriter 它不是为自定义或继承而设计的。我尝试实现一个自定义转换器,但是必须在其中注入自定义转换器……

As I see the automatic equality conversion to the current timezone's offset is done in Utf8JsonWriter It is not designed to customization or inheritance. I've tried to implement a custom converter, but it is pretty much higher level where the customization must be injected...

public class DateTimeConverterUsingDateTimeParse : JsonConverter<DateTimeOffset>
{
    public override void Write(Utf8JsonWriter writer, DateTimeOffset value, JsonSerializerOptions options)
    {
        // Based on the experience I've described in the OP, it is useless
        // to anything with the `DateTimeOffset value` because regardless its Offset, Utf8JsonWriter will write it with the current timezone's offset for the date.
    }
}


推荐答案

System.Text.Json 支持本地时区或UTC的序列化。

System.Text.Json supports serialization in local timezone or UTC.

您似乎确实知道这一点。

you definitely seem aware of this.

解决问题的其他方法是在时区之间转换。
看到此链接: https://docs.microsoft.com/zh-cn/dotnet/standard/datetime/converting-between-time-zones#converting-between-any-two-time-zones

Other way solving your problem is converting between timezones. see this link: https://docs.microsoft.com/en-us/dotnet/standard/datetime/converting-between-time-zones#converting-between-any-two-time-zones

这篇关于使用System.Text.Json序列化DateTimeOffset时如何影响时区?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆