我正在使用Xamarin.Forms和Azure應用程序服務(包括脫機同步)的應用程序。Azure應用服務 - 在同步期間更改日期時間
在客戶端上有這樣一段代碼:
appointment.StartDate = System.DateTime.Now;
我們假設appointment.StartDate現在2017年7月5日12:00:00。
用戶後同步數據對服務器,出現這種情況:
日期在SqlLite數據庫(客戶端):2017年7月5日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
更新2017年6月7日:
經過進一步的研究,我發現,這是一個已知的「問題」。
看看這裏:
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+
閱讀本我是能夠建立能解決問題的部分解決方案後。在客戶端,我現在正在做這樣的事情。
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的返回存儲在服務器數據庫中的客戶端的日期,天青轉換「UTC日期」到本地日期。以下是當前情況的示例:
客戶端日期:06.07。2017年14:30
- >客戶端推送日至服務器
服務器日期:2017年6月7日14:30
- >客戶端從服務器獲取日期
客戶日期:2017年6月7日16 :30
您可以在本地時間轉換爲UTC,並將其存儲!如果需要,我可以爲您提供代碼! – Sridharan
請查看我在第一篇文章中的更新 – OPunktSchmidt