Azure应用服务-同步期间的DateTime更改 [英] Azure App Service - DateTime changes during Sync

查看:81
本文介绍了Azure应用服务-同步期间的DateTime更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个使用Xamarin.Forms和Azure App Service(包括脱机同步)的应用程序.

在客户端上有一段这样的代码:

appointment.StartDate = System.DateTime.Now;

我们假设约会.StartDate现在为 2017-07-05 12:00:00 .

用户将数据与服务器同步后,会发生以下情况:

SqlLite数据库中的日期(在客户端上):2017-07-05 12:00:00

服务器数据库中的日期:2017-07-05 10:00:00

因此,我假设Azure将我的日期更改为UTC.从技术上讲,这可能是正确的,因为您应该始终将UTC存储在数据库中,并在客户端中处理时区转换.但是不幸的是,服务器数据库很旧,并且存储了区域设置日期.

如何让Azure将客户端的本地日期存储在服务器数据库中而不是UTC?

我试图将Azure中的WEBSITE_TIME_ZONE属性更改为我的本地时区,但这不起作用:

看这里:

https://github.com/Azure/azure -mobile-apps-net-client/issues/131

Azure Mobile Apps将不正确的日期时间保存在Sqlite数据库中

https ://forums.asp.net/t/1808269.aspx?DateTime + issues + with + Azure + c + javascript + sql + so + confused +

阅读本文后,我能够构建解决部分问题的解决方案.在客户端,我现在正在做类似的事情.

 public System.DateTime? Start
    {
        get
        {
            System.DateTime? dateTime = GetValue<System.DateTime?>(_start);
            if (dateTime.HasValue)
                dateTime = System.DateTime.SpecifyKind(dateTime.Value, System.DateTimeKind.Utc);

            return dateTime;
        }
        set
        {
            System.DateTime? dateTime = value;
            if (dateTime.HasValue)
                dateTime = System.DateTime.SpecifyKind(dateTime.Value, System.DateTimeKind.Utc);

            SetValue<System.DateTime?>(_start, dateTime);
        }
    }

这告诉Azure日期已经是UTC,并且Azure不必转换.这适用于以下情况:在客户端创建的System.DateTime现在已成功存储在服务器数据库中,而无需进行转换.

但是现在还有另一个问题:

当Azure将存储在服务器数据库中的日期返回给客户端时,Azure会将"UTC日期"转换为本地日期.这是当前情况的一个示例:

客户日期:06.07.2017 14:30

->客户端将日期推送到服务器

服务器日期:06.07.2017 14:30

->客户端从服务器获取日期

客户日期:06.07.2017 16:30

解决方案

但是不幸的是,服务器数据库较旧并且存储了区域设置日期.如何使Azure将来自客户端的本地日期存储在服务器数据库中而不是UTC?

根据您的描述,建议您存储长类型值DateTime.UtcNow.Ticks,因为您的datetime不使用datetime类型.

此值不会在您的服务器数据库中更改,并且可以转换回UTC时间.

当您要使用时间时,可以将Ticks转换回日期时间.

更多详细信息,您可以参考以下代码:

    //get utc time ticks
    long i = DateTime.UtcNow.Ticks;
    //convert back to local time
    DateTime myDate = new DateTime(i).ToLocalTime();


我是正确的,我必须调整我的旧应用程序,以便它们可以处理UTC?

我认为,我建议您可以将UTC时间存储到数据库中.然后,您可以将UTC时间转换为本地时间.这是设计应用程序的正确方法.如果有不同的时区客户,那么如何将数据库的本地时间转换为客户时区的本地时间?

UTC是全世界普遍使用的时间标准.世界各地的计时中心已同意保持其时标紧密同步或协调,因此命名为协调世界时".

所以我建议您可以使用http://www.louischarlesgagnon.com/post/azure-app-service-set-timezone-for-your-web-application

Update 06.07.2017:

After some further research, I found out that this is a known "problem".

Look here:

https://github.com/Azure/azure-mobile-apps-net-client/issues/131

Azure Mobile Apps saves incorrect datetime in Sqlite database

https://forums.asp.net/t/1808269.aspx?DateTime+issues+with+Azure+c+javascript+sql+so+confused+

After reading this I was able to build a solution that resolves a part of the problem. On the client side, I'm now doing something like this.

 public System.DateTime? Start
    {
        get
        {
            System.DateTime? dateTime = GetValue<System.DateTime?>(_start);
            if (dateTime.HasValue)
                dateTime = System.DateTime.SpecifyKind(dateTime.Value, System.DateTimeKind.Utc);

            return dateTime;
        }
        set
        {
            System.DateTime? dateTime = value;
            if (dateTime.HasValue)
                dateTime = System.DateTime.SpecifyKind(dateTime.Value, System.DateTimeKind.Utc);

            SetValue<System.DateTime?>(_start, dateTime);
        }
    }

This tells Azure that the date is already in UTC and Azure does not have to convert. This works in the scenario that the System.DateTime, which was created on the client side, is now successfully stored in the server database without conversion.

But there is now an another problem:

When Azure returns the date stored in the server database to the client, Azure converts the "UTC date" to local date. Here is an example of the current situation:

Client Date: 06.07.2017 14:30

-> Client push date to server

Server date: 06.07.2017 14:30

-> Client gets date from server

Client date: 06.07.2017 16:30

解决方案

But unfortunately the server database is old and stores locale dates.How can I make Azure to store the local date from the client in the server database and not UTC?

According to your description, I suggest you could store long type value DateTime.UtcNow.Ticks as your datetime not use datetime type.

This value will not be changed in your server database and it could be converted back to UTC time.

When you want to use the time, you could convert Ticks back to the datetime.

More details, you could refer to these codes:

    //get utc time ticks
    long i = DateTime.UtcNow.Ticks;
    //convert back to local time
    DateTime myDate = new DateTime(i).ToLocalTime();


I'm correct that I have to adapt my legacy applications so that they can deal with UTC?

In my opinion, I suggest you could store UTC time to your database. Then you could convert the UTC time to local time. This is right way to design your application. If there are different timezone customer, how you convert the database's local time to customer timezone's local time?

UTC is the time standard commonly used across the world. The world's timing centers have agreed to keep their time scales closely synchronized - or coordinated - therefore the name Coordinated Universal Time.

So I suggest you could use tolocaltime method to convert the UTC time to local time in your apps.

这篇关于Azure应用服务-同步期间的DateTime更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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