Loc Posted September 1, 2020 Share Posted September 1, 2020 This is the source code that takes the time on the computer to check and exit Global $Settimer = _DateDiff( 'd',"2020/9/1 00:00:00",_NowCalc()) If $Settimer > 0 Or $Settimer = 0 Then MsgBox(4096, "Error", "You have run out of time:" & $Settimer & " day") Exit Endif Link to comment Share on other sites More sharing options...
spudw2k Posted September 1, 2020 Share Posted September 1, 2020 Here's an example, tweaked from this source: https://www.autoitscript.com/forum/topic/144389-solved-autoit-function-to-pull-from-ntp/?do=findComment&comment=1018104 expandcollapse popup#include <Date.au3> _GetTimeFromNTP("pool.ntp.org") Func _GetTimeFromNTP($sNTPServer) Local $sData = "" TCPStartup() If TCPNameToIP($sNTPServer) = "" Then Return SetError(1) UDPStartup() $aSocket = UDPOpen(TCPNameToIP($sNTPServer), 123) $sStatus = UDPSend($aSocket, _MakePacket()) While $sData = "" $sData = UDPRecv($aSocket, 100) Sleep(250) WEnd UDPCloseSocket($aSocket) TCPShutdown() $sValue = _UnsignedHexToDecimal(StringMid($sData, 83, 8)) $aTimeZone = _Date_Time_GetTimeZoneInformation() $sUTC = _DateAdd("s", $sValue, "1900/01/01 00:00:00") If $aTimeZone[0] <> 2 Then $iTimeZoneOffset = ($aTimeZone[1]) * -1 Else $iTimeZoneOffset = ($aTimeZone[1] + $aTimeZone[7]) * -1 EndIf $sLocalTime = _DateAdd("n", $iTimeZoneOffset, $sUTC) ConsoleWrite($sLocalTime & @CRLF) EndFunc Func _MakePacket() Local $x, $sPacket = "1b0e01000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000" While $sPacket $x &= Chr(Dec(StringLeft($sPacket, 2))) $sPacket = StringTrimLeft($sPacket, 2) WEnd Return $x EndFunc Func _UnsignedHexToDecimal($sInput) $x = StringRight($sInput, 1) $sInput = StringTrimRight($sInput, 1) Return Dec($sInput) * 16 + Dec($x) EndFunc Spoiler Things I've Made: Always On Top Tool ◊ AU History ◊ Deck of Cards ◊ HideIt ◊ ICU ◊ Icon Freezer ◊ Ipod Ejector ◊ Junos Configuration Explorer ◊ Link Downloader ◊ MD5 Folder Enumerator ◊ PassGen ◊ Ping Tool ◊ Quick NIC ◊ Read OCR ◊ RemoteIT ◊ SchTasksGui ◊ SpyCam ◊ System Scan Report Tool ◊ System UpTime ◊ Transparency Machine ◊ VMWare ESX BuilderMisc Code Snippets: ADODB Example ◊ CheckHover ◊ Detect SafeMode ◊ DynEnumArray ◊ GetNetStatData ◊ HashArray ◊ IsBetweenDates ◊ Local Admins ◊ Make Choice ◊ Recursive File List ◊ Remove Sizebox Style ◊ Retrieve PNPDeviceID ◊ Retreive SysListView32 Contents ◊ Set IE Homepage ◊ Tickle Expired Password ◊ Transpose ArrayProjects: Drive Space Usage GUI ◊ LEDkIT ◊ Plasma_kIt ◊ Scan Engine Builder ◊ SpeeDBurner ◊ SubnetCalcCool Stuff: AutoItObject UDF ◊ Extract Icon From Proc ◊ GuiCtrlFontRotate ◊ Hex Edit Funcs ◊ Run binary ◊ Service_UDF Link to comment Share on other sites More sharing options...
Loc Posted September 1, 2020 Author Share Posted September 1, 2020 When I set $ set = _GetTimeFromNTP ("pool.ntp.org") MsgBox (0, 0, $ set) then it returns 0 Link to comment Share on other sites More sharing options...
Nine Posted September 1, 2020 Share Posted September 1, 2020 Here a simple way : #include <Constants.au3> Local $String = BinaryToString(InetRead ("http://worldtimeapi.org/api/timezone/America/Toronto.txt",1)) ;MsgBox (0,"",$String) ;Local $String = BinaryToString(InetRead ( "http://worldtimeapi.org/api/timezone/Europe/Amsterdam",1)) Local $Time = StringRegExp ($String,'datetime: (.+?)T(\d+:\d+:\d+)', $STR_REGEXPARRAYMATCH) MsgBox ($MB_SYSTEMMODAL,"",$Time[0] & " " & $Time[1]) “They did not know it was impossible, so they did it” ― Mark Twain Spoiler Block all input without UAC Save/Retrieve Images to/from Text Monitor Management (VCP commands) Tool to search in text (au3) files Date Range Picker Virtual Desktop Manager Sudoku Game 2020 Overlapped Named Pipe IPC HotString 2.0 - Hot keys with string x64 Bitwise Operations Multi-keyboards HotKeySet Recursive Array Display Fast and simple WCD IPC Multiple Folders Selector Printer Manager GIF Animation (cached) Screen Scraping Multi-Threading Made Easy Link to comment Share on other sites More sharing options...
Loc Posted September 1, 2020 Author Share Posted September 1, 2020 condition = then it can be greater than or equal to it wrong, is there the best way? If $Time[0] = '2020-09-01' Then MsgBox(0,0, 'out of date') Exit EndIf Link to comment Share on other sites More sharing options...
Nine Posted September 1, 2020 Share Posted September 1, 2020 (edited) 1 hour ago, MrRow said: it can be greater than or equal to You could use : If $Time[0] >= '2020-09-01' Then But it is not the most robust approach, I would prefer using something like : #include <Constants.au3> #include <Date.au3> Local $String = BinaryToString(InetRead ("http://worldtimeapi.org/api/timezone/America/Toronto.txt",1)) ;MsgBox (0,"",$String) ;Local $String = BinaryToString(InetRead ( "http://worldtimeapi.org/api/timezone/Europe/Amsterdam",1)) Local $Time = StringRegExp ($String,'datetime: (.+?)T(\d+:\d+:\d+)', $STR_REGEXPARRAYMATCH) MsgBox ($MB_SYSTEMMODAL,"",$Time[0] & " " & $Time[1]) Local $iDiff = _DateDiff ('D',"2020/09/01", StringReplace($time[0],"-","/")) If @error Then Exit MsgBox ($MB_SYSTEMMODAL,"Error","Invalid date format") If $iDiff >= 0 Then Exit MsgBox ($MB_SYSTEMMODAL,"Out of Date","Please renew your license") Edited September 1, 2020 by Nine Loc 1 “They did not know it was impossible, so they did it” ― Mark Twain Spoiler Block all input without UAC Save/Retrieve Images to/from Text Monitor Management (VCP commands) Tool to search in text (au3) files Date Range Picker Virtual Desktop Manager Sudoku Game 2020 Overlapped Named Pipe IPC HotString 2.0 - Hot keys with string x64 Bitwise Operations Multi-keyboards HotKeySet Recursive Array Display Fast and simple WCD IPC Multiple Folders Selector Printer Manager GIF Animation (cached) Screen Scraping Multi-Threading Made Easy Link to comment Share on other sites More sharing options...
Loc Posted September 1, 2020 Author Share Posted September 1, 2020 (edited) On 9/1/2020 at 6:57 PM, Nine said: Here a simple way : #include <Constants.au3> Local $String = BinaryToString(InetRead ("http://worldtimeapi.org/api/timezone/America/Toronto.txt",1)) ;MsgBox (0,"",$String) ;Local $String = BinaryToString(InetRead ( "http://worldtimeapi.org/api/timezone/Europe/Amsterdam",1)) Local $Time = StringRegExp ($String,'datetime: (.+?)T(\d+:\d+:\d+)', $STR_REGEXPARRAYMATCH) MsgBox ($MB_SYSTEMMODAL,"",$Time[0] & " " & $Time[1]) Edited September 7, 2020 by Loc Link to comment Share on other sites More sharing options...
Earthshine Posted September 1, 2020 Share Posted September 1, 2020 (edited) why not let os sync with time servers? then just read systemdate? see if you can use the site below to read the time somehow. edit: they do have an api service but it costs money. https://www.timeanddate.com/worldclock/vietnam/hanoi anyway, if you can get reliable GMT time, yours is Indochina time GMT+7 hours so add 7 hours to GMT that you get back via api calls Edited September 1, 2020 by Earthshine My resources are limited. You must ask the right questions Link to comment Share on other sites More sharing options...
FrancescoDiMuro Posted September 1, 2020 Share Posted September 1, 2020 @Earthshine Seems that both Indochina and Vietnam are not present in the list of all the timezones supported by the website. Perhaps, since Vietnam is GMT+7, he could use a city that has a GMT+7 time too, or use the public IP to determine the time zone Earthshine and Nine 2 Click here to see my signature: Spoiler ALWAYS GOOD TO READ: Forum Rules Forum Etiquette Link to comment Share on other sites More sharing options...
Nine Posted September 1, 2020 Share Posted September 1, 2020 @Loc ASIA/BANGKOK is +7. So use this city instead. FrancescoDiMuro and Earthshine 2 “They did not know it was impossible, so they did it” ― Mark Twain Spoiler Block all input without UAC Save/Retrieve Images to/from Text Monitor Management (VCP commands) Tool to search in text (au3) files Date Range Picker Virtual Desktop Manager Sudoku Game 2020 Overlapped Named Pipe IPC HotString 2.0 - Hot keys with string x64 Bitwise Operations Multi-keyboards HotKeySet Recursive Array Display Fast and simple WCD IPC Multiple Folders Selector Printer Manager GIF Animation (cached) Screen Scraping Multi-Threading Made Easy Link to comment Share on other sites More sharing options...
Loc Posted September 6, 2020 Author Share Posted September 6, 2020 I have written a clock tool and tried to turn off the network connection, the gui will hang and run your source code, it will error. Is there a way to fix this Link to comment Share on other sites More sharing options...
spudw2k Posted September 7, 2020 Share Posted September 7, 2020 I think we need to see more code to understand how best to address this. I imagine the time request from the internet will timeout, but how to deal with the failure in your script needs more information. I am sure some logic can be implemented to ignore the internet time request failure. Spoiler Things I've Made: Always On Top Tool ◊ AU History ◊ Deck of Cards ◊ HideIt ◊ ICU ◊ Icon Freezer ◊ Ipod Ejector ◊ Junos Configuration Explorer ◊ Link Downloader ◊ MD5 Folder Enumerator ◊ PassGen ◊ Ping Tool ◊ Quick NIC ◊ Read OCR ◊ RemoteIT ◊ SchTasksGui ◊ SpyCam ◊ System Scan Report Tool ◊ System UpTime ◊ Transparency Machine ◊ VMWare ESX BuilderMisc Code Snippets: ADODB Example ◊ CheckHover ◊ Detect SafeMode ◊ DynEnumArray ◊ GetNetStatData ◊ HashArray ◊ IsBetweenDates ◊ Local Admins ◊ Make Choice ◊ Recursive File List ◊ Remove Sizebox Style ◊ Retrieve PNPDeviceID ◊ Retreive SysListView32 Contents ◊ Set IE Homepage ◊ Tickle Expired Password ◊ Transpose ArrayProjects: Drive Space Usage GUI ◊ LEDkIT ◊ Plasma_kIt ◊ Scan Engine Builder ◊ SpeeDBurner ◊ SubnetCalcCool Stuff: AutoItObject UDF ◊ Extract Icon From Proc ◊ GuiCtrlFontRotate ◊ Hex Edit Funcs ◊ Run binary ◊ Service_UDF Link to comment Share on other sites More sharing options...
Loc Posted September 7, 2020 Author Share Posted September 7, 2020 here is my code #include <GUIConstantsEx.au3> #include <StaticConstants.au3> #include <WindowsConstants.au3> #include <Constants.au3> GUICreate("Timer", 224, 152, 495, 132, $WS_POPUPWINDOW) $Date = GUICtrlCreateLabel("", 50, 32, 120, 30) GUICtrlSetFont(-1, 15, 800, 0, "MS Sans Serif") $Timer = GUICtrlCreateLabel("", 60, 80, 100, 30) GUICtrlSetFont(-1, 15, 800, 0, "MS Sans Serif") GUISetState(@SW_SHOW) While 1 $Timer_Web = BinaryToString(InetRead ("http://worldtimeapi.org/api/timezone/ASIA/BANGKOK.txt",1)) $Timer_SetWeb = StringRegExp ($Timer_Web,'datetime: (.+?)T(\d+:\d+:\d+)', $STR_REGEXPARRAYMATCH) If GUICtrlRead($Date) <> $Timer_SetWeb[0] Then GUICtrlSetData($Date,$Timer_SetWeb[0]) If GUICtrlRead($Timer) <> $Timer_SetWeb[1] Then GUICtrlSetData($timer,$Timer_SetWeb[1]) $nMsg = GUIGetMsg() Switch $nMsg Case $GUI_EVENT_CLOSE Exit EndSwitch WEnd Link to comment Share on other sites More sharing options...
Trong Posted September 7, 2020 Share Posted September 7, 2020 expandcollapse popup#Region - Includes and Initilization #AutoIt3Wrapper_Change2CUI=Y #include <Date.au3> #EndRegion _SyncTimeFromInternet("time1.google.com") #Region - Command Line Parameter Execution/Logic If @Compiled Then If $cmdline[0] Then ;Check for CMD Lime Parameters If StringInstr($cmdlineraw,"?") Then ;Help ConsoleWrite(@CRLF & "Description:" & @CRLF & @TAB & "Displays system up time for local or remote machines." & @CRLF) ConsoleWrite(@CRLF & "Parameter List:" & @CRLF & @TAB & "/?" & @TAB & @TAB & @TAB & "Displays this help/usage." & @CRLF) ConsoleWrite(@CRLF & "Examples:" & @CRLF & @TAB & @ScriptName & @CRLF) ConsoleWrite(@TAB & @ScriptName & " hostname1 hostname2 hostname3" & @CRLF) Exit EndIf For $iX = 1 To $cmdline[0] ; ConsoleWrite(StringUpper($cmdline[$iX]) & " " & _Uptime($cmdline[$iX])&@CRLF) Next Else ConsoleWrite(_Uptime() & @CRLF) EndIf Else MsgBox(0,"Uptime:",_Uptime()&@CRLF) EndIf #EndRegion #Region - Uptime Function (Main) Func _Uptime($sComputer = @ComputerName) $iPing = Ping($sComputer,4000) If Not $iPing Then Return "Failed to Reach" $oWMIService = ObjGet("winmgmts:\\" & $sComputer & "\root\cimv2") If Not IsObj($oWMIService) Then Return "Failed to Connect" $oColOperatingSystems = $oWMIService.ExecQuery("Select LastBootUpTime from Win32_OperatingSystem") For $oOS in $oColOperatingSystems Local $sBootup = $oOS.LastBootUpTime Local $sLastBootupTime = __WMIDateStringToDate($sBootup) Next $oColOperatingSystems = 0 $oWMIService = 0 Local $sUpTime = __FormatUptime($sLastBootupTime) Return "Last Boot: " & $sLastBootupTime & @CRLF & "Uptime: " & $sUpTime EndFunc #EndRegion #Region - Internal Functions Func __FormatUptime($sLastBootupTime) Local $aDateDiffs=[[0,24," day"],[0,60," hour"],[0,60," minute"],[0,1," second"]] Local $aDateDiff = StringSplit("D|h|n|s","|") Local $sNow = _NowCalc() For $sDiff = 1 to $aDateDiff[0] $aDateDiffs[$sDiff-1][0] = _DateDiff($aDateDiff[$sDiff], $sLastBootUpTime, $sNow) Next Local $iUbound = UBound($aDateDiffs)-1 For $iX = $iUbound To 0 Step -1 If $iX > 0 Then If $aDateDiffs[$iX-1][0] > 0 Then $aDateDiffs[$iX][0] = ($aDateDiffs[$iX][0]-($aDateDiffs[$iX-1][0]*$aDateDiffs[$iX-1][1])) EndIf Next Local $sUptime = "" For $iX = 0 To $iUbound If $aDateDiffs[$iX][0] > 0 Then $sUptime &= $aDateDiffs[$iX][0] & $aDateDiffs[$iX][2] If $aDateDiffs[$iX][0] > 1 Then $sUptime &= "s" If $iX < $iUbound Then $sUptime &= ", " EndIf Next Return $sUptime EndFunc Func __WMIDateStringToDate($sBootup) Return StringLeft($sBootup,4) & "/" & StringMid($sBootup, 5, 2) & "/" & StringMid($sBootup, 7, 2) & " " & StringMid($sBootup, 9, 2) & ":" & StringMid($sBootup, 11, 2) & ":" & StringMid($sBootup, 13, 2) EndFunc #EndRegion Func _SyncTimeFromInternet($sNTPServer="time1.google.com") Local $sData = "" TCPStartup() If TCPNameToIP($sNTPServer) = "" Then Return SetError(1) UDPStartup() $aSocket = UDPOpen(TCPNameToIP($sNTPServer), 123) $sStatus = UDPSend($aSocket, _MakePacket()) While $sData = "" $sData = UDPRecv($aSocket, 100) Sleep(250) WEnd UDPCloseSocket($aSocket) TCPShutdown() $sValue = _UnsignedHexToDecimal(StringMid($sData, 83, 8)) $aTimeZone = _Date_Time_GetTimeZoneInformation() $sUTC = _DateAdd("s", $sValue, "1900/01/01 00:00:00") If $aTimeZone[0] <> 2 Then $iTimeZoneOffset = ($aTimeZone[1]) * -1 Else $iTimeZoneOffset = ($aTimeZone[1] + $aTimeZone[7]) * -1 EndIf Local $iMonth = StringMid($sUTC, 6, 2), $iDay = StringMid($sUTC, 9, 2), $iYear = StringMid($sUTC, 1, 4), _ $iHour = StringMid($sUTC, 12, 2), $iMinute = StringMid($sUTC, 15, 2), $iSecond = StringMid($sUTC, 18, 2) ConsoleWrite("Time = " & $iMonth & " " & $iDay & " " & $iYear & " " & $iHour & " " & $iMinute & " " & $iSecond & @CR) $sCurrentTime = _Date_Time_EncodeSystemTime($iMonth, $iDay, $iYear, $iHour, $iMinute, $iSecond) _Date_Time_SetSystemTime(DllStructGetPtr($sCurrentTime)) EndFunc Func _MakePacket() Local $x, $sPacket = "1b0e01000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000" While $sPacket $x &= Chr(Dec(StringLeft($sPacket, 2))) $sPacket = StringTrimLeft($sPacket, 2) WEnd Return $x EndFunc Func _UnsignedHexToDecimal($sInput) $x = StringRight($sInput, 1) $sInput = StringTrimRight($sInput, 1) Return Dec($sInput) * 16 + Dec($x) EndFunc Không biết có đúng ý bạn ko nữa Regards, Link to comment Share on other sites More sharing options...
Loc Posted September 7, 2020 Author Share Posted September 7, 2020 55 minutes ago, VIP said: expandcollapse popup#Region - Includes and Initilization #AutoIt3Wrapper_Change2CUI=Y #include <Date.au3> #EndRegion _SyncTimeFromInternet("time1.google.com") #Region - Command Line Parameter Execution/Logic If @Compiled Then If $cmdline[0] Then ;Check for CMD Lime Parameters If StringInstr($cmdlineraw,"?") Then ;Help ConsoleWrite(@CRLF & "Description:" & @CRLF & @TAB & "Displays system up time for local or remote machines." & @CRLF) ConsoleWrite(@CRLF & "Parameter List:" & @CRLF & @TAB & "/?" & @TAB & @TAB & @TAB & "Displays this help/usage." & @CRLF) ConsoleWrite(@CRLF & "Examples:" & @CRLF & @TAB & @ScriptName & @CRLF) ConsoleWrite(@TAB & @ScriptName & " hostname1 hostname2 hostname3" & @CRLF) Exit EndIf For $iX = 1 To $cmdline[0] ; ConsoleWrite(StringUpper($cmdline[$iX]) & " " & _Uptime($cmdline[$iX])&@CRLF) Next Else ConsoleWrite(_Uptime() & @CRLF) EndIf Else MsgBox(0,"Uptime:",_Uptime()&@CRLF) EndIf #EndRegion #Region - Uptime Function (Main) Func _Uptime($sComputer = @ComputerName) $iPing = Ping($sComputer,4000) If Not $iPing Then Return "Failed to Reach" $oWMIService = ObjGet("winmgmts:\\" & $sComputer & "\root\cimv2") If Not IsObj($oWMIService) Then Return "Failed to Connect" $oColOperatingSystems = $oWMIService.ExecQuery("Select LastBootUpTime from Win32_OperatingSystem") For $oOS in $oColOperatingSystems Local $sBootup = $oOS.LastBootUpTime Local $sLastBootupTime = __WMIDateStringToDate($sBootup) Next $oColOperatingSystems = 0 $oWMIService = 0 Local $sUpTime = __FormatUptime($sLastBootupTime) Return "Last Boot: " & $sLastBootupTime & @CRLF & "Uptime: " & $sUpTime EndFunc #EndRegion #Region - Internal Functions Func __FormatUptime($sLastBootupTime) Local $aDateDiffs=[[0,24," day"],[0,60," hour"],[0,60," minute"],[0,1," second"]] Local $aDateDiff = StringSplit("D|h|n|s","|") Local $sNow = _NowCalc() For $sDiff = 1 to $aDateDiff[0] $aDateDiffs[$sDiff-1][0] = _DateDiff($aDateDiff[$sDiff], $sLastBootUpTime, $sNow) Next Local $iUbound = UBound($aDateDiffs)-1 For $iX = $iUbound To 0 Step -1 If $iX > 0 Then If $aDateDiffs[$iX-1][0] > 0 Then $aDateDiffs[$iX][0] = ($aDateDiffs[$iX][0]-($aDateDiffs[$iX-1][0]*$aDateDiffs[$iX-1][1])) EndIf Next Local $sUptime = "" For $iX = 0 To $iUbound If $aDateDiffs[$iX][0] > 0 Then $sUptime &= $aDateDiffs[$iX][0] & $aDateDiffs[$iX][2] If $aDateDiffs[$iX][0] > 1 Then $sUptime &= "s" If $iX < $iUbound Then $sUptime &= ", " EndIf Next Return $sUptime EndFunc Func __WMIDateStringToDate($sBootup) Return StringLeft($sBootup,4) & "/" & StringMid($sBootup, 5, 2) & "/" & StringMid($sBootup, 7, 2) & " " & StringMid($sBootup, 9, 2) & ":" & StringMid($sBootup, 11, 2) & ":" & StringMid($sBootup, 13, 2) EndFunc #EndRegion Func _SyncTimeFromInternet($sNTPServer="time1.google.com") Local $sData = "" TCPStartup() If TCPNameToIP($sNTPServer) = "" Then Return SetError(1) UDPStartup() $aSocket = UDPOpen(TCPNameToIP($sNTPServer), 123) $sStatus = UDPSend($aSocket, _MakePacket()) While $sData = "" $sData = UDPRecv($aSocket, 100) Sleep(250) WEnd UDPCloseSocket($aSocket) TCPShutdown() $sValue = _UnsignedHexToDecimal(StringMid($sData, 83, 8)) $aTimeZone = _Date_Time_GetTimeZoneInformation() $sUTC = _DateAdd("s", $sValue, "1900/01/01 00:00:00") If $aTimeZone[0] <> 2 Then $iTimeZoneOffset = ($aTimeZone[1]) * -1 Else $iTimeZoneOffset = ($aTimeZone[1] + $aTimeZone[7]) * -1 EndIf Local $iMonth = StringMid($sUTC, 6, 2), $iDay = StringMid($sUTC, 9, 2), $iYear = StringMid($sUTC, 1, 4), _ $iHour = StringMid($sUTC, 12, 2), $iMinute = StringMid($sUTC, 15, 2), $iSecond = StringMid($sUTC, 18, 2) ConsoleWrite("Time = " & $iMonth & " " & $iDay & " " & $iYear & " " & $iHour & " " & $iMinute & " " & $iSecond & @CR) $sCurrentTime = _Date_Time_EncodeSystemTime($iMonth, $iDay, $iYear, $iHour, $iMinute, $iSecond) _Date_Time_SetSystemTime(DllStructGetPtr($sCurrentTime)) EndFunc Func _MakePacket() Local $x, $sPacket = "1b0e01000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000" While $sPacket $x &= Chr(Dec(StringLeft($sPacket, 2))) $sPacket = StringTrimLeft($sPacket, 2) WEnd Return $x EndFunc Func _UnsignedHexToDecimal($sInput) $x = StringRight($sInput, 1) $sInput = StringTrimRight($sInput, 1) Return Dec($sInput) * 16 + Dec($x) EndFunc Không biết có đúng ý bạn ko nữa thanks for helping me, i got the exact time from @Nine. Now I am looking to fix the no network connection error Link to comment Share on other sites More sharing options...
Trong Posted September 7, 2020 Share Posted September 7, 2020 ipconfig /release ipconfig /renew ipconfig /flushdns netsh winsock reset catalog nbtstat -R nbtstat -RR netsh int ip reset D:\resetlog.txt netsh winsock reset reg add HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\NlaSvc\Parameters\Internet /v EnableActiveProbing /t REG_DWORD /d 0 /f :::pause Chuyển đổi mã bat này sang AutoIT hoặc lưu lại dưới dạng file BAT và chạy với quyền Admin. Xong thì khởi động lại là đc :v Regards, Link to comment Share on other sites More sharing options...
CYCho Posted September 7, 2020 Share Posted September 7, 2020 (edited) I can say I am pretty much sensitive to the accuracy of my computer clock and I put the following program in the Windows task scheduler to run every hour. I am quite confident that my clock always keeps time that is not off by more than 20 millisecs from the NTP time. I turned off Windows default time sync function. expandcollapse popup;~ Many thanks to @TheXman for his kind assistance in getting the NTP time. ;~ https://www.autoitscript.com/forum/topic/200643-update-system-time-based-on-ntp-server-time/page/2/?tab=comments#comment-1439629 #RequireAdmin #Region ;**** Directives created by AutoIt3Wrapper_GUI **** #AutoIt3Wrapper_AU3Check_Parameters=-w 3 -w 4 -w 5 -w 6 -d #EndRegion ;**** Directives created by AutoIt3Wrapper_GUI **** #include <Constants.au3> #include <Date.au3> #include <Array.au3> #include <File.au3> #include <Misc.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 ; This may vary depending on your geolocation. For $i = 1 To 10 Main() If $success Then ExitLoop EndIf Sleep(1000) Next If $success = False Then $NTP_Server = "time.google.com" $NTP_MaxDelay = 150 ; This may vary depending on your geolocation. For $i = 1 To 10 Main() If $success Then ExitLoop EndIf Sleep(1000) Next EndIf #cs If your computer clock consistently runs faster or slower than the NTP time by more than 15 or so millisecs an hour, you may like to use _Date_Time_SetSystemTimeAdjustment() function in Date.au3 to make your clock run more precisely. In order to prevent $iAdjustment value from reverting to default, Windows default time update service must be disabled by one of the following 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" #ce $aInfo = _Date_Time_GetSystemTimeAdjustment() Global $iAdjustment = $aInfo[1] ; 156250 $iAdjustment += 0 ; 1 in $iAdustment value represents 23.04 milsecs in 1 hour, 0.55296 second a day ; You can set this value after you have understood how fast or slow your clock runs from the log file. If $aInfo[0] <> $iAdjustment Then _Date_Time_SetSystemTimeAdjustment($iAdjustment, False) EndIf If $success = False Then WriteLog(0, $sError) Exit EndIf System_SetTime() WriteLog($NTP_Time, "") Sleep(3000) 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 If StringInStr($hRoundTrip, ".") Then $hRoundTrip = StringMid(Round(100+$hRoundTrip, 3) & "000", 2, 6) Else $hRoundTrip = StringMid(Round(100+$hRoundTrip, 3) & ".000", 2, 6) EndIf $pTime = $NTP_Time & " Adjustment: " & $hAdjustment & " second(s), NTP Response Time: " & $hRoundTrip & " milsecs" _ArrayAdd($aLog, $pTime) _FileWriteFromArray(@ScriptDir & "\TimeSync.log", $aLog, 1) EndIf EndFunc ;==>WriteLog Edited October 7, 2020 by CYCho zPlayer - A Small Audio and Video Player Time Sync + SystemTimeAdjustment Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now