MS JSON日期系列化和夏令时 [英] MS JSON date serialization and daylight saving time

查看:165
本文介绍了MS JSON日期系列化和夏令时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们在ASP.NET应用程序托管一个Ajax Web服务。此服务接受它,这个问题的目的,应该接受的DateTime相当于中/日(1359727200000-0200)日期时间参数/ ,这是一个MS-JSON字面为2013年2月1日的 9:00 AM 巴西时间。这个文字是浏览器中的Ajax请求发送内

We have an Ajax web service hosted in an ASP.NET application. This service accepts a DateTime parameter which, for the purposes of this question, should receive the DateTime equivalent of /Date(1359727200000-0200)/, which is a MS-JSON literal for Feb 1, 2013 9:00 AM in Brazil time. This literal is what the browser sends within the Ajax request.

但不是接收的日期和时间,传递给服务方法的参数是2013年2月1日的下午12:00 。更糟糕的是,争论的DateTimeKind是本地。这是可以理解的,如果它是 UTC ,但即使这是不正确的,因为2月1日是夏令时在东巴西,让<一href=\"http://www.timeanddate.com/worldclock/converted.html?iso=20130201T09&p1=233&p2=0&p3=&p4=&p5=&p6=&p7=&p8=&p9=&p10=&p11=&p12=\"相对=nofollow> 9:00 AM(本地)是前人的精力上午11:00(UTC)。这真的是一个烂摊子。顺便说一句,Web服务器下巴西东部时区运行。

But instead of receiving that date and time, the argument passed to the service method is Feb 1, 2013 12:00 PM. To make matters worse, the argument's DateTimeKind is Local. It would be understandable if it were Utc, but even that would be incorrect since February 1st is daylight saving time in Eastern Brazil, so that 9:00 AM (local) sould be 11:00 AM (UTC). It really is a mess. By the way, the web server runs under Eastern Brazilian time zone.

有没有ASP.NET配置,服务方法的注释,特定的客户端的指令,或任何框架资源来解决这个问题呢?

Is there any ASP.NET configuration, service method annotation, specific client instructions, or any framework resource to work around this problem?

推荐答案

许多调试后,我发现这个问题:我是无知的方式有关JSON序列作品的System.DateTime 类和日期文字时(反)序列日期来回服务器和浏览器之间。

After much debugging I found the problem: I was ignorant of the way the JSON serializer works regarding the System.DateTime kind and the Date literal when (de)serializing dates back and forth between the server and the browser.

在细节,这里发生了什么。假设一个服务有一个DateTime值返回JSON格式的客户端。在 DataContractJsonSerializer 将在(臭名昭著)的 /日期生成一个字符串(UTC蜱)/ 格式,其中客户端可以解析为JavaScript日期值。这种序列化到datetime的Kind属性相关的:

In detail, here's what happens. Suppose a service has to return a DateTime value to a client in JSON format. The DataContractJsonSerializer will produce a string in the (infamous) /Date(UTC ticks)/ format, which the client can parse into a JavaScript Date value. This serialization is sensitive to the DateTime's Kind property:


  • 当一种是 DateTimeKind.Utc ,该字符串包含/日期(UTC蜱)/,没有时区。

  • 当一种是 DateTimeKind.Local ,该字符串包含/日期(UTC蜱时区)/。

  • 如果种类为 DateTimeKind.Unspecified ,假设本地

  • When the Kind is DateTimeKind.Utc, the string literal will contain "/Date(UTC ticks)/" and no time zone.
  • When the Kind is DateTimeKind.Local, the string literal will contain "/Date(UTC ticks-time zone)/".
  • If the Kind is DateTimeKind.Unspecified, it is assumed Local.

相反,如果客户端发送一个日期值到服务,序列化过程也是对的厚道的字面JSON敏感的:

Conversely, if the client sends a date value to the service, the serialization process is also sensitive to the "kind" of the JSON literal:


  • 当文字的格式为/日期(UTC蜱)/,没有时区,所产生的
    日期时间将是 DateTimeKind.Utc 样的。

  • 当文字的格式为/日期(UTC蜱时区)/,所生产的日期时间将是 DateTimeKind.Local 样的。

  • When the literal is in the format "/Date(UTC ticks)/" with no time zone, the produced DateTime will be of DateTimeKind.Utc kind.
  • When the literal is in the format "/Date(UTC ticks-time zone)/", the produced DateTime will be of DateTimeKind.Local kind.

所有我必须做的就是确保客户端只发送UTC格式的日期文字服务。现在一切是伟大的。

All I had to do is make sure the client sends only UTC-formatted Date literals to the service. All is great now.

这篇关于MS JSON日期系列化和夏令时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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