如何在Windows 10 IoT中设置系统时间?

rav*_*-ai 14 c# raspberry-pi windows-10-iot-core

有没有办法在我的应用程序中运行Windows 10 IoT核心内幕预览中的Raspberry Pi 2上的系统时间?

这不适用于缺少kernel32.dll

    [DllImport("kernel32.dll", EntryPoint = "SetSystemTime", SetLastError = true)]
    extern static bool Win32SetSystemTime(ref SystemTime sysTime);
Run Code Online (Sandbox Code Playgroud)

kir*_*ran 11

首先,使用PowerShell连接到Pi 2.

使用该命令set-date设置时间.例如,如果要将日期设置为2015年10月3日星期六,下午2:00,则可以键入set-date 10/3/2015 2:00PM.

该命令tzutil设置时区.tzutil /?使用类型

  • 日期应该在引号之间:`set-date"2015年10月3日下午2:00"` (3认同)

Kur*_*kay 6

1 -新通用项目
2 -添加引用>扩展>的Windows IOT扩展为UWP
3-放置一个按钮,日期选择器,并在您的MainPage.xaml中一个timepicer控制

private void buttonSetSystemDatetime_Click(object sender, RoutedEventArgs e)
        {
            DateTimeOffset dto = datePicker1.Date+ timePicker1.Time;
            Windows.System.DateTimeSettings.SetSystemDateTime(dto);
        }
Run Code Online (Sandbox Code Playgroud)

4-在项目设置中>设置目标版本和最低版本10.0;版本16299
5-双击appxmanifest>功能>选中“系统管理”

6-在物联网中运行应用程序,然后按按钮。而不是返回到默认应用。瞧!系统日期时间已更改。

注意:每次都要设置。我提供给您购买便宜的rtc(实时时钟)模块。(还需要额外的编码)


rav*_*-ai 3

到目前为止,似乎还没有办法实际编辑系统时间,但这是我想出的一种解决方法,至少可以在您的应用程序中获得正确的时间。我创建了一个TimeManager类,重要部分如下。

按照您想要的方式获取正确的时间(例如 NTP、其他网络时间、用户输入等)并输入到 UpdateOffset 方法中。

在应用程序的其余部分使用 TimeManager.Now 而不是 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. 
    }
Run Code Online (Sandbox Code Playgroud)

我还建议添加诸如跟踪上次更新时间之类的内容,以及指示时间是否已更新的标志,只是不想弄乱代码示例。