在SETSYSTEMTIME KERNEL32奇怪的行为 [英] Strange behaviour in SetSystemTime kernel32

查看:98
本文介绍了在SETSYSTEMTIME KERNEL32奇怪的行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下的code:

Public Class SetSystemTime
    Structure SYSTEMTIME
        Public wYear As Short
        Public wMonth As Short
        Public wDayOfWeek As Short
        Public wDay As Short
        Public wHour As Short
        Public wMinute As Short
        Public wSecond As Short
        Public wMilliseconds As Short
    End Structure

    Public Declare Function SetSystemTime Lib "kernel32" (ByRef lpSystemTime As SYSTEMTIME) As Boolean
End Class

Private Sub SetDateTime(dt As DateTime)
    Dim dateTimeStruct As SetSystemTime.SYSTEMTIME
    Dim incHour As Integer = DateDiff(DateInterval.Hour, Now, Date.UtcNow)

    With dateTimeStruct
        .wDay = dt.Day
        .wDayOfWeek = dt.DayOfWeek
        .wHour = dt.Hour + incHour
        .wMilliseconds = dt.Millisecond
        .wMinute = dt.Minute
        .wMonth = dt.Month
        .wSecond = dt.Second
        .wYear = dt.Year
    End With

    SetSystemTime.SetSystemTime(dateTimeStruct)
End Sub

我看到的行为是,在2014年8月4日4点15分07秒,该方法SetDateTime上面用dt的作为2014年4月8日4点15分07秒,以便有效地,该时间设定而在执行现实中,并没有改变。我的日志显示我的PC时钟跃升至2014年8月4日5时15分07秒作为变化的结果。 随后,在2014年8月4日13时00分28秒(14时00分28秒PC的时间),该方法被又一次打来电话,时钟设置回2014年8月4日13时00分28秒

The behaviour I have seen is that at 2014-08-04 04:15:07, the method SetDateTime above was executed with dt as 04/08/2014 04:15:07 so effectively, the time was set but in reality, hadn't changed. My logs show me that the PC clock jumped to 2014-08-04 05:15:07 as a result of the change. Subsequently, at 2014-08-04 13:00:28 (14:00:28 PC time), the method was called again and the clock was set back to 2014-08-04 13:00:28

什么可能导致此行为。时区设置为伦敦,我们目前正处于夏令时至十月。操作系统是Win7的嵌入式标准。

What could possibly cause this behaviour. Timezone is set to London and we're currently in daylight saving until October. The OS is Win7 Embedded Standard.

任何想法?

推荐答案

使用的 SetLocalTime 函数来代替,并请大家仔细记下它的文档中句话:

Use the SetLocalTime function instead, and please take careful note of the remark in its documentation:

本系统内部使用UTC。因此,当你调用SetLocalTime,系统将使用当前的时区信息进行转换,包括夏令时设置。需要注意的是该系统采用的当前时间夏令时设置,而不是新的时候要设置。因此,为了确保正确的结果,称SetLocalTime第二次,现在第一次调用更新了夏令时的设置。

The system uses UTC internally. Therefore, when you call SetLocalTime, the system uses the current time zone information to perform the conversion, including the daylight saving time setting. Note that the system uses the daylight saving time setting of the current time, not the new time you are setting. Therefore, to ensure the correct result, call SetLocalTime a second time, now that the first call has updated the daylight saving time setting.

这篇关于在SETSYSTEMTIME KERNEL32奇怪的行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆