在SETSYSTEMTIME KERNEL32奇怪的行为奇怪、行为、SETSYSTEMTIME

由网友(不爱滚远点)分享简介:我有以下的code:Public Class SetSystemTimeStructure SYSTEMTIMEPublic wYear As ShortPublic wMonth As ShortPublic wDayOfWeek As ShortPublic wDay As ShortPublic wHour A...

我有以下的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.

阅读全文

相关推荐

最新文章