Azure Mobile Apps saves incorrect datetime in Sqlite database

女生的网名这么多〃 提交于 2020-01-25 13:08:14

问题


I am trying to save an Entity with an ID and an DateTimeOffset in a AzureMobileServicesClient SQLite database in a Xamarin Forms application.

This work fine as long as the time stamp is non ambiguous. When we go from daylight saving to standard time there is a period a time where a time stamp can be ambiguous depending on the timezone. In 2016 in Denmark it is 2:00-3:00 AM October 30th.

I save the Entities DateTimeOffset as UTC, but it saves it as a local timestamp.

When I save an ambiguous time stamp as 2016-10-30 12:30 AM + 0:00 it comes back as 2016-10-30 11:30 AM +0:00. I have tested this for other time zones and it only happens with the ambiguous time during the shift from daylight saving to standard time in that particular time zone.

I have seen the issue fixed for a normal SQLite data base here: http://www.thomaslevesque.com/2015/06/28/how-to-retrieve-dates-as-utc-in-sqlite/
But because i am using AzureMobileSerives the solution don't work in this case.

In this code i initialize the AzureMobileService, save an ambiguous time stamp and then retreive it again, and write out the two times to the screen to see the different output

public static class SaveTimeStamp
{
    public static async Task Save()
    {
        //Init mobile service Client 
        var mobileService = new MobileServiceClient(Configuration.MobileAppsUrl);

        //init MobileService Database
        var dataStore = DependencyService.Get<IDataStore>();
        var path = dataStore.GetPath("store.db");
        dataStore.CreateFile(path);
        var store = new MobileServiceSQLiteStore(path);

        store.DefineTable<Entity>();
        await mobileService.SyncContext.InitializeAsync(store, StoreTrackingOptions.NotifyLocalAndServerOperations).ConfigureAwait(false);
        var entityTable = mobileService.GetSyncTable<Entity>();

        //Save entity with ambiguous timestamp
        var ambiguousTimestamp = new DateTimeOffset(2016, 10, 30, 0, 30, 0, TimeSpan.Zero); //UTC: 30th of October 12:30 AM + 0:00 => DK time:30th of october 2:30 AM + 2:00

        var entity = new Entity
        {
            Id = Guid.NewGuid().ToString(),
            Timestamp = ambiguousTimestamp
        };

        Debug.WriteLine("Saving datetime UTC: " + entity.Timestamp.UtcDateTime);

        await entityTable.InsertAsync(entity).ConfigureAwait(false);

        //Fetch saved entity
        var refecthedEntities = await entityTable.Where(e => e.Id == entity.Id).ToListAsync();
        var refecthedEntity = refecthedEntities.FirstOrDefault();

        if (refecthedEntity.Timestamp != ambiguousTimestamp)
        {
            Debug.WriteLine("Refecthed datetime UTC do not match: " + refecthedEntity.Timestamp.UtcDateTime);
        }
        else
        {
            Debug.WriteLine("Refecthed datetime UTC: " + refecthedEntity.Timestamp.UtcDateTime);
        }

        //output 
        //[0:]Saving datetime UTC: 10/30/2016 12:30:00 AM
        //[0:]Refecthed datetime UTC do not match: 10/29/2016 11:30:00 PM
    }
}

class Entity
{
    public string Id { get; set; }
    public DateTimeOffset Timestamp { get; set; }
}

Becuase this is a Xamarin application i also have some code in the xamarin.ios proejct to initlize the database.

//IN Xamarin.IOS  
[Register("AppDelegate")]
public partial class AppDelegate : global::Xamarin.Forms.Platform.iOS.FormsApplicationDelegate
{

    public override bool FinishedLaunching(UIApplication app, NSDictionary options)
    {

        global::Xamarin.Forms.Forms.Init();
        Microsoft.WindowsAzure.MobileServices.CurrentPlatform.Init(); //Init Azure mobileservies on IOS Current  IOS 9.3
        SQLitePCL.CurrentPlatform.Init();

        LoadApplication(new App());

        App.ScreenWidth = (int)UIScreen.MainScreen.Bounds.Width;
        App.ScreenHeight = (int)UIScreen.MainScreen.Bounds.Height;

        return base.FinishedLaunching(app, options);
    }

    public override void WillEnterForeground(UIApplication uiApplication)
    {
        base.WillEnterForeground(uiApplication);
        uiApplication.IdleTimerDisabled = true;
    }

    public override void DidEnterBackground(UIApplication uiApplication)
    {
        base.DidEnterBackground(uiApplication);
        uiApplication.IdleTimerDisabled = false;
    }
}

I have tried to use a DateTime instead of DateTimeOffset on the entity and only use DateTimeKind=UTC, but I got the same result.


回答1:


This looks like an already reported bug: https://github.com/Azure/azure-mobile-apps-net-client/issues/131

Feel free to follow the bug.



来源:https://stackoverflow.com/questions/39890764/azure-mobile-apps-saves-incorrect-datetime-in-sqlite-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!