Jump to content

[Solved]How long is _Date_Time_SetSystemTimeAdjustment effective?


 Share

Recommended Posts

Hi everyone! I wish you all the best at this hard time.

As I said in my other post, I set system time adjustment when I start up my desktop in the morning. I would expect that this setting will last until I turn off the computer, but it does not. I don't know when, but _Date_Time_GetSystemTimeAdjustment during the day sometimes shows that the setting has reverted to default value. This seems to be happening randomly. Can you please tell me when and under what circumstance this can happen? I am certain that there was no Windows Time Update during my observation.

Edited by CYCho
Link to comment
Share on other sites

@faustf As I update system time every hour with my own code, I changed HKEY_LOCAL_MACHINE - System - Controlset001 - Services - W32Time - TimeProviders - NtpClient - SpecialPollInterval value to 864000000, thus virtually disabling automatic Windows Time Update. Below is my TimeSync.log file for yesterday and  today. The asterisk at the end of line means that _Date_Time_GetSystemTimeAdjustment detected the reversion to default (or whatever which is not my desired value) and set again. 2020/03/20 11:27:22.328 and  2020/03/21 06:04:44.712 are my system start up times.

2020/03/20 11:27:22.328 Adjustment: +0.048 second(s), NTP Response Time: 59.207 milsecs*
2020/03/20 11:55:02.985 Adjustment: +0.005 second(s), NTP Response Time: 63.402 milsecs
2020/03/20 12:55:01.718 Adjustment: +0.020 second(s), NTP Response Time: 08.095 milsecs*
2020/03/20 13:55:01.756 Adjustment: +0.006 second(s), NTP Response Time: 02.656 milsecs
2020/03/20 14:55:01.758 Adjustment: +0.005 second(s), NTP Response Time: 02.445 milsecs
2020/03/20 15:55:01.725 Adjustment: +0.005 second(s), NTP Response Time: 02.035 milsecs
2020/03/20 16:55:01.838 Adjustment: +0.012 second(s), NTP Response Time: 03.256 milsecs
2020/03/20 17:55:01.755 Adjustment: +0.004 second(s), NTP Response Time: 02.574 milsecs
2020/03/20 18:55:03.018 Adjustment: +0.015 second(s), NTP Response Time: 59.082 milsecs
2020/03/20 19:55:01.731 Adjustment: +0.003 second(s), NTP Response Time: 02.334 milsecs
2020/03/20 20:55:01.753 Adjustment: +0.007 second(s), NTP Response Time: 01.988 milsecs
2020/03/20 21:55:01.740 Adjustment: +0.008 second(s), NTP Response Time: 02.851 milsecs
2020/03/20 22:55:01.773 Adjustment: +0.010 second(s), NTP Response Time: 07.276 milsecs
2020/03/20 23:55:02.984 Adjustment: +0.009 second(s), NTP Response Time: 58.897 milsecs
2020/03/21 06:04:44.712 Adjustment: +0.141 second(s), NTP Response Time: 02.006 milsecs*
2020/03/21 06:55:01.694 Adjustment: +0.007 second(s), NTP Response Time: 07.442 milsecs
2020/03/21 07:55:01.717 Adjustment: +0.008 second(s), NTP Response Time: 07.024 milsecs
2020/03/21 08:55:01.604 Adjustment: +0.013 second(s), NTP Response Time: 01.660 milsecs
2020/03/21 09:55:01.824 Adjustment: +0.009 second(s), NTP Response Time: 02.200 milsecs
2020/03/21 10:55:02.139 Adjustment: +0.005 second(s), NTP Response Time: 01.972 milsecs
2020/03/21 11:55:03.392 Adjustment: +0.016 second(s), NTP Response Time: 59.931 milsecs
2020/03/21 12:55:02.136 Adjustment: +0.006 second(s), NTP Response Time: 03.243 milsecs
2020/03/21 13:55:02.132 Adjustment: +0.010 second(s), NTP Response Time: 01.841 milsecs
2020/03/21 14:55:02.185 Adjustment: +0.019 second(s), NTP Response Time: 03.440 milsecs*
2020/03/21 15:55:02.324 Adjustment: +0.014 second(s), NTP Response Time: 02.124 milsecs
2020/03/21 16:55:03.384 Adjustment: +0.014 second(s), NTP Response Time: 57.981 milsecs
2020/03/21 17:55:02.104 Adjustment: +0.005 second(s), NTP Response Time: 04.181 milsecs
2020/03/21 18:55:02.154 Adjustment: +0.014 second(s), NTP Response Time: 01.895 milsecs
2020/03/21 19:55:02.144 Adjustment: +0.005 second(s), NTP Response Time: 01.915 milsecs
 

#RequireAdmin
#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Res_Fileversion=6.0.0.0
#AutoIt3Wrapper_AU3Check_Parameters=-w 3 -w 4 -w 5 -w 6 -d
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
;~ Many thanks to @TheXman for his kind guidance.
;~ https://www.autoitscript.com/forum/topic/200643-update-system-time-based-on-ntp-server-time/page/2/?tab=comments#comment-1439629

#include <Constants.au3>
#include <Date.au3>
#include <Array.au3>
#include <File.au3>

Global $hTimer = TimerInit(), $hPing

While 1
    $hPing = Ping("www.google.com", 200) ; Cloudflare.com
    If $hPing > 0 Then ExitLoop
    If TimerDiff($hTimer) > 120000 Then
        WriteLog("", "System has no internet connection")
        Exit
    EndIf
    Sleep(50)
WEnd

Global $tTime, $aTime, $Sys_Time, $aInfo
Global $NTP_Time, $iSeconds, $iFractions, $iMsecs, $hRoundTrip, $sError, $success

Global $tBuffer = DllStructCreate("byte[48]")
DllStructSetData($tBuffer, 1, 0x23) ; 00 100 011 = LI(0) / VN(4-NTPv4) / Mode(3-Client)

Global $xRequest = DllStructGetData($tBuffer, 1), $xResponse

UDPStartup()
OnAutoItExitRegister("udp_shutdown")

Global $NTP_Server = "pool.ntp.org"
Global $NTP_MaxDelay = 10
Main()
If $success = False Then
    Sleep(1000)
    Main()
EndIf

If $success = False Then
    $NTP_Server = "time.google.com"
    $NTP_MaxDelay = 80
    Main()
    If $success = False Then
        Sleep(1000)
        Main()
    EndIf
EndIf

$aInfo = _Date_Time_GetSystemTimeAdjustment()
Global $sAdjusted, $iAdjustment = $aInfo[1] ; 1 in $iAdustment value represents 23.04 milsecs in 1 hour
Switch @ComputerName
    Case "Desktop-Main"
        $iAdjustment += 3
    Case "88Ace"
        $iAdjustment -= 4
    Case "MSDN"
        $iAdjustment += 4
    Case "CYCho"
        $iAdjustment -= 5
EndSwitch

If $aInfo[0] <> $iAdjustment Then
    _Date_Time_SetSystemTimeAdjustment($iAdjustment, False)
    $sAdjusted = "*"
EndIf

If $success = True Then
    System_SetTime()
    WriteLog($NTP_Time, "")
Else
    WriteLog(0, $sError)
EndIf


Func Main()
    Local $NTP_IP = TCPNameToIP($NTP_Server)
    If $NTP_IP <> "" Then
        Local $aSocket = UDPOpen($NTP_IP, 123)
        If $aSocket[0] <> 0 Then
            $NTP_Time = ""
            NTP_GetTime($aSocket)
            If $hRoundTrip < $NTP_MaxDelay And $NTP_Time <> "" Then
                $sError = ""
                $success = True
            Else
                $sError = "Response delay too long"
            EndIf
        EndIf
        UDPCloseSocket($aSocket)
    EndIf
EndFunc

Func System_SetTime()
    ; 2019/10/28 23:09:52.522
    ; 12345678901234567890123
    Local $m = StringMid($NTP_Time, 6, 2)
    Local $d = StringMid($NTP_Time, 9, 2)
    Local $y = StringMid($NTP_Time, 1, 4)
    Local $h = StringMid($NTP_Time, 12, 2)
    Local $min = StringMid($NTP_Time, 15, 2)
    Local $s = StringMid($NTP_Time, 18, 2)
    Local $tCurr = _Date_Time_EncodeSystemTime($m, $d, $y, $h, $min, $s, $iMsecs)
    ; stores the current system time for calculation of $hAdjustment in WriteLog() function
    $tTime = _Date_Time_GetSystemTime()
    ; Sets the new current time to the computer
    _Date_Time_SetSystemTime(DllStructGetPtr($tCurr))
EndFunc ;==>System_SetTime

Func NTP_GetTime($aSocket)
    $hTimer = TimerInit()
    UDPSend($aSocket, $xRequest)
    If @error Then
        $sError = "UDPSend failed"
        Return
    EndIf
    Do
        $xResponse = UDPRecv($aSocket, DllStructGetSize($tBuffer), $UDP_DATA_BINARY)
        If @error Then
            $sError = "UDPRecv failed"
            Return
        EndIf
    Until $xResponse <> ""
    $hRoundTrip = TimerDiff($hTimer)
    $iSeconds = Dec(Hex(BinaryMid($xResponse, 41, 4)), $NUMBER_64BIT) ; seconds since 1900-01-01 00:00:00
    $iFractions = Dec(Hex(BinaryMid($xResponse, 45, 4)), $NUMBER_64BIT) ; the maximum value is 0xFFFFFFFF, which represents 1 second
    $iMsecs = Round($iFractions/(2^32)*1000 + $hRoundTrip/2 + 1.5)
    If $iMsecs >= 1000 Then
        $iSeconds += Int($iMsecs/1000)
        $iMsecs = Mod($iMsecs, 1000)
    EndIf
    $iMsecs = StringFormat("%03d", $iMsecs)

    $NTP_Time = _DateAdd("s", $iSeconds, "1900/01/01 00:00:00")
EndFunc   ;==>NTP_GetTime

Func udp_shutdown()
    UDPShutdown()
EndFunc   ;==>udp_shutdown

Func _DateDiffMS($sType, $sStartDate, $sEndDate)
    Local $iDiff
    If $sType = "ms" Or $sType = "s" Then
        Local $sStartMS, $sEndMS, $iDiffSec
        If StringInStr($sStartDate, ".") Then
            $sStartMS = StringMid($sStartDate, StringInStr($sStartDate, "."), 5)*1000
            $sStartDate = StringLeft($sStartDate, StringInStr($sStartDate, ".")-1)
        EndIf
        If StringInStr($sEndDate, ".") Then
            $sEndMS = StringMid($sEndDate, StringInStr($sEndDate, "."), 5)*1000
            $sEndDate = StringLeft($sEndDate, StringInStr($sEndDate, ".")-1)
        EndIf
        $iDiffSec = _DateDiff("s", $sStartDate, $sEndDate)
        If @error Then Return SetError(@error, 0, 0)
        $iDiff = $iDiffSec*1000+Round($sEndMS-$sStartMS)
        If $sType = "s" Then
            $iDiff = StringFormat("%.3f", $iDiff/1000)
        EndIf
    Else
        If StringInStr($sStartDate, ".") Then
            $sStartDate = StringLeft($sStartDate, StringInStr($sStartDate, ".")-1)
        EndIf
        If StringInStr($sEndDate, ".") Then
            $sEndDate = StringLeft($sEndDate, StringInStr($sEndDate, ".")-1)
        EndIf
        $iDiff = _DateDiff($sType, $sStartDate, $sEndDate)
    EndIf
    Return SetError(@error, 0, $iDiff)
EndFunc

Func WriteLog($pTime, $sMessage)
    If $pTime = "" Then
        Local $fn = FileOpen(@ScriptDir & "\TimeSync Failed.log", 2)
        $pTime = _NowCalc() & @CRLF
        FileWrite($fn, $pTime & $sMessage & @CRLF)
        FileClose($fn)
    Else
        Local $aLog[1]
        If FileExists(@ScriptDir & "\TimeSync.log") Then
            _FileReadToArray(@ScriptDir & "\TimeSync.log", $aLog, 1)
        ;   For $i = 1 To $aLog[0]-24
        ;       _ArrayDelete($aLog, 1)
        ;   Next
        EndIf

        FileDelete(@ScriptDir & "\TimeSync Failed.log")

        $aInfo = _Date_Time_GetTimeZoneInformation()
        Local $TZ_Offset = $aInfo[1]

        $aTime = _Date_Time_SystemTimeToArray($tTime)
        $Sys_Time = StringFormat("%04d/%02d/%02d %02d:%02d:%02d", $aTime[2], $aTime[0], $aTime[1], $aTime[3], $aTime[4], $aTime[5])
        $Sys_Time = _DateAdd("n", -$TZ_Offset, $Sys_Time) & StringFormat(".%03d", $aTime[6])

        $NTP_Time = _DateAdd("n", -$TZ_Offset, $NTP_Time) & "." & $iMsecs

        Local $hAdjustment = _DateDiffMS("s", $Sys_Time, $NTP_Time)
        If $hAdjustment >= 0 Then
            $hAdjustment = "+" & $hAdjustment
        EndIf

        $pTime = $NTP_Time & " Adjustment: " & $hAdjustment & " second(s), NTP Response Time: " & StringMid(Round(100+$hRoundTrip, 3) & "000", 2, 6) & " milsecs" & $sAdjusted
        _ArrayAdd($aLog, $pTime)
        _FileWriteFromArray(@ScriptDir & "\TimeSync.log", $aLog, 1)
    EndIf
EndFunc   ;==>WriteLog

 

Edited by CYCho
Link to comment
Share on other sites

On 3/20/2020 at 2:10 AM, CYCho said:

Can you please tell me when and under what circumstance this can happen?

Without more information related to your PC's specific configuration, it's hard to determine what the issue may be.  However, the Microsoft article below may be relevant to your issue.

https://support.microsoft.com/en-us/help/2638243/when-specialpollinterval-is-used-as-a-polling-interval-the-windows-tim

Link to comment
Share on other sites

Turning off automatic Windows Time Update service was the solution. It can be done by either of following 2 methods:

1. Turn off "Settings - Time and language - Date and time - Set time automatically"
2. Click off "Control panel - Date and time - Internet time - Synchronize with an internet time server"

Increasing the SpecialPollInterval value in the registry didn't work.

After turning off Windows Time Update, _Date_Time_SetSystemTimeAdjustment remained effective for a full day for the first time since I started to use this code.

Thank you, @faustf and @TheXman, for your hints.

Edited by CYCho
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...