MS JSON date serialization and daylight saving tim

2020-07-23 05:32发布

问题:

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.

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.

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

回答1:

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.

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:

  • 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.

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

  • 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.

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