Azure的时区和对象的JavaScriptSerializer [英] Azure time zone and javascriptserializer object

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

问题描述

我有一个基于pdictions $ P $应用程序,最多的在Windows Azure上(的http://我predikt.com )。从我可以告诉Azure中的时钟同步到GMT时区。下面是我遇到一个问题:

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:

让我们说我有一个名为DateTime类型CREATEDATE一个数据库字段,我将其值设置为2011年6月10日,上午12:30。当创建一个新的prediction。如果我偷看db表内的日期设置正确。我不碰或以任何方式改变此值。然而,当我阅读我们的API值,序列化,并将其发送给客户端,我得到与2011年6月9日,下午5:30的值的日期。 (该API DLL还住在云中,可能并置与DB)。

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

我的客户端浏览器在运行PST(太平洋时区),似乎7个小时的时差是由PST和GMT之间的差异。用于序列化的价值API code是类似这样的:

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串=新的JavaScriptSerializer();

System.Web.Script.Serialization.JavaScriptSerializer serializer = new JavaScriptSerializer();

返回serializer.Serialize(dataObject时);

return serializer.Serialize(dataObject);

这是在的JavaScriptSerializer对象的错误,还是有窍门来解决此增量?基本上,我不希望.NET框架以任何方式与这个值干涉,我只是想一下就回到数据库字段是

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.

推荐答案

当你传递DateTime对象到Azure同类等于本地。结果
(2011年6月10日,上午12:30 -7)

When you pass DateTime object to Azure its Kind equals to Local.
(June 10, 2011, 12:30am -7)

但是,当您将它保存到数据库中的区域信息丢失。接着,从数据库中读出这个字段时,它与UTC区域创建日期时间
(2011年6月10日,上午12:30 0)

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)转换日期时间到的DateTimeOffset中的方法参数,以及在数据库中。这将保证您所在地区(如PST)将被保存在数据库

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)使用DateTime.SpecifyKind(日期时间,DateTimeKind.Unspecified) - 这样的那种日期时间是不确定的,随后保存在数据库中。

2) Use DateTime.SpecifyKind(dateTime, DateTimeKind.Unspecified) - this way the kind of DateTime is unspecified and subsequently saved as is in the db.

var timeNow = DateTime.SpecifyKind(DateTime.Now, DateTimeKind.Unspecified);
serviceClient.SaveTime(timeNow);
var dateTime = serviceClient.GetTime();

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

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