JSON.NET将+00:00时区解析为本地时间,但将Z解析为UTC [英] JSON.NET Parses +00:00 timezone as local times, but Z as UTC

查看:377
本文介绍了JSON.NET将+00:00时区解析为本地时间,但将Z解析为UTC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了Web API错误地解析日期时间的问题,而我跟踪到JSON.NET.

I've been having an issue with Web API parsing datetimes incorrectly which I have tracked down to JSON.NET.

问题在于,如果我发送此日期时间:

The issue is that if I send this datetime:

2015-07-28T19:06:01.000+00:00

在JSON PUT请求中,在我的模型中解析的DateTime将被转换为本地服务器时区中的时间,并且使用C#datetime类型的本地时间,而不是UTC.

in a JSON PUT request, the DateTime parsed in my model will be converted to a time in the local server timezone, with the C# datetime kind of local, not UTC.

如果我发送此日期时间:

If I send this datetime:

2015-07-28T19:06:01.000Z

它将正确地保留为UTC,而我想要的是C#datetime类型的UTC.

It will correctly keep it as UTC, with the C# datetime kind of UTC which is what I want.

我可以通过这样设置DateTimeZoneHandling来解决此问题:

I can fix this, by setting the DateTimeZoneHandling like this:

SerializerSettings.DateTimeZoneHandling = DateTimeZoneHandling.Utc;

但是我不明白为什么它首先要这样做.根据ISO8601,Z和+00:00应该表示相同的意思,对吗?我担心全局将DateTimeZomeHandling设置为UTC,因为我可能不希望端点上的每个日期都被视为UTC.

But I don't understand why it does this in the first place. According to ISO8601, Z and +00:00 should mean the same thing right? I'm worried about setting the DateTimeZomeHandling to UTC globally as I might not want every single date on my endpoint to be considered UTC.

还有其他设置可以让我考虑+00:00表示UTC吗?

Is there another setting where I can tell it to consider +00:00 to mean UTC?

推荐答案

DateTime仅区分UTCLocal(以及Unknown).考虑到两个不同的Local时间当然可以存在于两个不同的区域,这会引起各种问题,如果Local时间位于使用UTC的时区(例如冰岛),则它的确可能是UTC时间.一整年,或整个冬季都在爱尔兰).因此,DateTime可能是Local,并且与UTC的时差为零.

DateTime only distinguishes between UTC and Local (and Unknown). This causes all manner of problems considering that two different Local times could of course be local to two different zones, and a Local time could indeed be a UTC time if it's in a time zone that uses UTC (e.g. Iceland all the year long, or Ireland during the Winter). Because of this a DateTime could be Local and have a time difference to UTC of zero.

另一方面,ISO 8601(更明智地)要么包含时区信息,要么包含日期和/或时间,或者不包含.

ISO 8601 on the otherhand (much more sensibly) either includes timezone information along with the date and/or time, or it doesn't.

在两者之间没有完美的往返方法,因此考虑将Z表示为UTC,将+00:00表示为Local(但与UTC相同的本地类型)对此并不完美.情况下,通过提供DateTimeZoneHandling可以减少不完美的情况,以便人们可以从这种妥协中进行调整.

There's no perfect way to roundtrip between the two, so considering Z to mean UTC and +00:00 to mean Local (but the sort of local that is identical to UTC) is an imperfect compromise to this situation, made a bit less imperfect by providing a DateTimeZoneHandling so that people can adjust from that compromise.

使用DateTimeOffset代替DateTime是处理DateTime与日期,时间和偏移量的组合之间不匹配的另一种方法.

Using DateTimeOffset instead of DateTime is another way of dealing with the mismatch between DateTime and a combination of date, time and offset.

这篇关于JSON.NET将+00:00时区解析为本地时间,但将Z解析为UTC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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