C#JavaScriptSerializer和DateTime.MinValue跨越时区 [英] C# JavaScriptSerializer and DateTime.MinValue crossing timezones

查看:113
本文介绍了C#JavaScriptSerializer和DateTime.MinValue跨越时区的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

鉴于我有一个客户端和一个服务器,以及一个像这样的类:

Given that I have a client and a server, and a class like this:

客户端:(时区:UTC -8)

Client side: (TimeZone: UTC -8)

class Foo {
      public int a;
      public DateTime MyTime { get; set; }
    }

var serialized = new JavaScriptSerializer().Serialize(new Foo { a = 1 });
// Send serialized to server

服务器端:(TimeZone UTC-4)

Server side: (TimeZone UTC-4)

// Receive JSON
var deserialized = new JavaScriptSerializer().Deserialize<Foo>(serialized);
if (deserialized.MyTime == DateTime.MinValue) {
  // dostuff
}

我的问题是JavaScriptSerializer.Serialize(foo),是否执行MyTime.MinValue.ToUniversalTime(),这给了我 1.一月0001 08:00:00 .接下来,当我在服务器端反序列化时,它会在DateTime.MinValue上看到8个小时,并且if测试失败.我也不能对本地时间进行操作,因为服务器和客户端不在同一时区,所以本地时间会给我 1.一月0001 04:00:00 .

My problem here is that JavaScriptSerializer.Serialize(foo), does MyTime.MinValue.ToUniversalTime() which gives me 1. january 0001 08:00:00. Next when I deserialize on the server side, it sees 8 hours over DateTime.MinValue and the if test fails. I also cannot do to localtime, because the server and client are not in the same timezone, so localtime would give me 1. january 0001 04:00:00.

最后,我要提问:

In the end of this I have to questions:

  1. 鉴于这种情况,处理此问题的最佳方法是什么? JavaScriptSerializer()进行呼叫.ToUniversalTime().我的 当前的方法是只检查第0年左右的时间范围.
  2. 鉴于我们有DateTime.MinValue,应该不要期望这个值总是那样. DateTime.MinValue.ToUniversalTime()是否应等于DateTime.MinValue?基本上,我问的是是否应该在所有转换例程中明确查找DateTime.MinValue.
  1. What is the best way to handle this given that JavaScriptSerializer() does the call .ToUniversalTime(). My current approach is to just check a timerange around year 0.
  2. Given that we have DateTime.MinValue, should one not expect this value to always be exactly that. Should not the DateTime.MinValue.ToUniversalTime() be equal to DateTime.MinValue? Basically I am asking if there should be an explicit if check looking for DateTime.MinValue in all conversion routines.

谢谢.

JavaScriptSerializer: http://msdn.microsoft.com/en -us/library/system.web.script.serialization.javascriptserializer.aspx
约会时间: http://msdn.microsoft.com/en-us/library/system .datetime.aspx

JavaScriptSerializer: http://msdn.microsoft.com/en-us/library/system.web.script.serialization.javascriptserializer.aspx
DateTime: http://msdn.microsoft.com/en-us/library/system.datetime.aspx

推荐答案

使用DateTimeOffset代替DateTime.看起来序列化程序将结构序列化如下:

Use DateTimeOffset instead of DateTime. It looks like the serializer serializes the struct as follows:

{"a":1,"MyTime":"\/Date(-62135596800000)\/"}

这意味着可以通过服务器端的DateTimeDateTimeOffset字段反序列化此字符串.看起来这两个字符串都反序列化到午夜,所以我假设反序列化器将数字解串时会将其解释为+00:00偏移量.

This means that this string can be deserialized by either a DateTime and DateTimeOffset field on the server side. It looks like both of those deserialize the above string to midnight, so I'm assuming that the deserializer interprets the number as a +00:00 offset as it comes off the wire.

这篇关于C#JavaScriptSerializer和DateTime.MinValue跨越时区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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