我正在开发一个使用 Xamarin.Forms 和 Azure 应用服务(包括离线同步)的应用程序。
在客户端有这样一段代码:
appointment.StartDate = System.DateTime.Now;
假设约会。开始日期是现在2017-07-05 12:00:00.
用户与服务器同步数据后,会发生以下情况:
Sqlite 数据库中的日期(在客户端):2017-07-05 12:00:00
服务器数据库中的日期:2017-07-05 10:00:00
因此,我假设 Azure 将我的日期更改为 UTC。这在技术上可能是正确的,因为您应该始终将 UTC 存储在数据库中并在客户端中处理时区转换。但不幸的是,服务器数据库很旧并且存储区域设置日期。
如何使 Azure 将客户端的本地日期而不是 UTC 存储在服务器数据库中?
我尝试将 Azure 中的 WEBSITE_TIME_ZONE 属性更改为我的本地时区,但这不起作用:http://www.louischarlesgagnon.com/post/azure-app-service-set-timezone-for-your-web-application http://www.louischarlesgagnon.com/post/azure-app-service-set-timezone-for-your-web-application
2017 年 7 月 6 日更新:
经过进一步的研究,我发现这是一个已知的“问题”。
看这里:
https://github.com/Azure/azure-mobile-apps-net-client/issues/131 https://github.com/Azure/azure-mobile-apps-net-client/issues/131
Azure 移动应用程序在 Sqlite 数据库中保存错误的日期时间 https://stackoverflow.com/questions/39890764/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+ 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
-> 客户端将日期推送到服务器
服务器日期:2017年7月6日 14:30
-> 客户端从服务器获取日期
客户日期:2017年7月6日 16:30