How to set system time in Windows 10 IoT?

后端 未结 8 2452
星月不相逢
星月不相逢 2020-12-31 05:12

Is there a way to set system time from my app running on a Raspberry Pi 2 in Windows 10 IoT Core Insider Preview?

This doesn\'t work for lack of kernel32.dll

<
相关标签:
8条回答
  • 2020-12-31 06:05

    Seemingly as of now there appears to be no way to actually edit the system time, but here is a work around I came up with to get correct time in your app at least. I created a TimeManager class, the important parts are as follows.

    Get the correct time how ever you want (e.g. NTP, other network time, user input, etc) and input into the UpdateOffset method.

    In the rest of the App use TimeManager.Now instead of DateTime.Now

        static TimeSpan _offset = new TimeSpan(0,0,0);
        public static TimeSpan CurrentOffset //Doesn't have to be public, it is for me because I'm presenting it on the UI for my information
        {
            get { return _offset; }
            private set { _offset = value; }
        }
    
        public static DateTime Now
        {
            get
            {
                return DateTime.Now - CurrentOffset;
            }
        }
    
        static void UpdateOffset(DateTime currentCorrectTime) //May need to be public if you're getting the correct time outside of this class
        {
            CurrentOffset = DateTime.UtcNow - currentCorrectTime;
            //Note that I'm getting network time which is in UTC, if you're getting local time use DateTime.Now instead of DateTime.UtcNow. 
        }
    

    I also suggest adding things like tracking the last update time, and flag to indicate whether the time has ever been updated, just didn't want to clutter up the code sample.

    0 讨论(0)
  • 2020-12-31 06:06

    refer to Microsoft API Reference Docs and in the Windows.System namespace you can sets the system date and time with SetSystemDateTime method.

    but you must know it`s available in

    • Windows IoT Extension SDK (introduced v10.0.16225.0) and above

    you can use DateTimeSettings static class

    public static class DateTimeSettings
    

    then call SetSystemDateTime static method and send your object of DateTimeOffset type for setting date and time on Windows Iot.

    public static void SetSystemDateTime(DateTimeOffset utcDateTime)
    

    https://docs.microsoft.com/en-us/uwp/api/windows.system.datetimesettings

    0 讨论(0)
提交回复
热议问题