Azure 时区和 javascriptserializer 对象
我在 Windows Azure 上有一个基于预测的应用程序 ( http://ipredikt.com )。据我所知,Azure 的时钟已与 GMT 时区同步。这是我遇到的一个问题:
假设我有一个名为 CreateDate 的 DateTime 类型的数据库字段,并将其值设置为 2011 年 6 月 10 日凌晨 12:30。当创建新的预测时。如果我查看数据库表,日期设置正确。我不会以任何方式触及或改变这个值。但是,当我使用 API 读取该值、将其序列化并将其发送给客户端时,我得到的日期值为 2011 年 6 月 9 日下午 5:30。 (API dll 也存在于云中,并且可能与数据库并置。)
我的客户端浏览器在 PST(太平洋时区)运行,似乎 7 小时的差异是由于 PST 和 GMT 之间的差异造成的。用于序列化值的 API 代码类似于:
System.Web.Script.Serialization.JavaScriptSerializer serializer = new JavaScriptSerializer();
返回序列化器.Serialize(dataObject);
这是 JavaScriptSerializer 对象中的错误还是有修复此增量的技巧?基本上,我不希望 .NET 框架以任何方式干扰该值,我只希望 DB 字段按原样返回。
I have a predictions-based app that's up on Windows Azure ( http://ipredikt.com ). From what I can tell Azure's clock is synchronized to the GMT time zone. Here is a problem that I am encountering:
Let's say I have a DB field called CreateDate of type DateTime and I set its value to June 10, 2011, 12:30am. when a new prediction is created. If I peek inside the db table the date is correctly set. I don't touch or change this value in any way. However, when I read the value with our API, serialize it and send it to the client I get a date with the value of June 09, 2011, 5:30 pm. (The API dll also lives on the cloud and probably is collocated with the DB.)
My client browser is running in the PST (pacific time zone) and it seems that the 7 hour difference is due to the difference between PST and GMT. The API code used to serialize the value is similar to this:
System.Web.Script.Serialization.JavaScriptSerializer serializer = new JavaScriptSerializer();
return serializer.Serialize(dataObject);
Is this a bug in JavaScriptSerializer object or is there a trick to fix this delta? Basically, I don't want the .NET framework to interfere with this value in any way, I just want the DB field to just get returned as is.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当您将 DateTime 对象传递到 Azure 时,其 Kind 等于 Local。
(2011年6月10日,12:30am -7)
但是,当您将其保存到数据库时,区域信息会丢失。随后,当从数据库读取此字段时,它会创建带有 Utc 区域的 DateTime
(June 10, 2011, 12:30am 0)
最终,您的客户端错误地读取了日期时间。
有多种选择可以解决此问题。
1) 将方法参数和数据库中的 DateTime 转换为 DateTimeOffset。这将保证您的本地区域(即 PST)将保存在数据库中
2)使用 DateTime.SpecifyKind(dateTime, DateTimeKind.Unspecified) - 这样,日期时间的类型是未指定的,并且随后按原样保存在数据库中。
When you pass DateTime object to Azure its Kind equals to Local.
(June 10, 2011, 12:30am -7)
However, when you save it to the database the region information is lost. Subsequently, when reading this field from the database it creates DateTime with a Utc region
(June 10, 2011, 12:30am 0)
Eventually, your client reads the datetime incorrectly.
There are several options to resolve this issue.
1) Convert DateTime to DateTimeOffset in Method parameters as well as in database. This will guarantee that your Local region (ie PST) will be saved in db
2) Use DateTime.SpecifyKind(dateTime, DateTimeKind.Unspecified) - this way the kind of DateTime is unspecified and subsequently saved as is in the db.
序列化的 Json 响应中可能会出现格式为“自纪元以来的毫秒数”的日期,并且还应包含时区信息,浏览器在显示相对于本地时区的日期时会考虑这些信息。
所有这些都是正确的行为,因此不存在错误。看来您不希望在您的情况下出现这种行为。
.NET 日期有一个“Kind”属性。如果未指定,则假定为 UTC。序列化程序在序列化时应考虑“Kind”属性。尝试在序列化之前检查对象的此属性,并将其更改为 DateTimeKind.Local。
http://msdn.microsoft.com/en-us/library /system.datetime.kind.aspx
或者,您可以在浏览器端查看自定义反序列化,在反序列化之前,您可以从序列化日期中删除时区部分。
What presumably comes down in the serialized Json response is a date in the format "milliseconds since the epoch", and should also include timezone information, which the browser is then taking into account when displaying the date relative to the local timezone.
All of this is correct behavior, so there's no bug. It simply seems that you don't want this behavior in your case.
.NET dates have a "Kind" property. If this is not specified, UTC is assumed. The serializer should take the "Kind" property into account when serializing. Try inspecting this property on your object prior to serialization, and changing it to DateTimeKind.Local.
http://msdn.microsoft.com/en-us/library/system.datetime.kind.aspx
Alternatively, you could look at custom deserialization on the browser side, where you'd strip the timezone part off of the serialized date before it's deserialized.