Search the Community
Showing results for tags 'time date sync system update'.
-
At my work, most ports including the one used by the Internet Time sync are blocked by a firewall. Some systems (especially those with bad CMOS batteries) need to have the clock set before Windows Update works, so I wrote this for our software toolkit. Any changes, corrections, or criticisms welcome. I am always looking to improve my code! #RequireAdmin #include<Date.au3> ; For the ActiveTimeBias registry key: ; 4294967296 = 0 for UTC +X:YZ time zones; subtract value of key to get time zone offset. ; EG: 4294976296-4294976236 = 60 = UTC +1:00 ; ; UTC time zone uses 0 for the value ; ; UTC -A:BC time zones use the number of minutes of the offset ; EG: 480 = UTC -8:00 ; To-Do: ; 1. Add a timezone check to make sure the system is properly configured for its area ; EG: Time Zone is currently "Pacific Standard Time", is this correct? [Y/N] ; 2. Add error checking to _EndOfMonth() should the need arise [user editing of code] ; EG: -2 is not a valid month, you silly goose! ; ---------------------------------------------------------------------------------------------------------------------------------- ; Get time from HTTP server (Defaults to google.com ... pool.ntp.org is a good alternative) ; ---------------------------------------------------------------------------------------------------------------------------------- $site = "www.google.com" $oHTTP = ObjCreate("winhttp.winhttprequest.5.1") $oHTTP.Open("GET", "http://" & $site & "/", False) $oHTTP.Send() $date = $oHTTP.GetResponseHeader("Date") ; ---------------------------------------------------------------------------------------------------------------------------------- ; ---------------------------------------------------------------------------------------------------------------------------------- ; ---------------------------------------------------------------------------------------------------------------------------------- ; Set Global variables used throughout the script ; ---------------------------------------------------------------------------------------------------------------------------------- ; $tzo: TimeZoneOffset (the number of hours by which the local machine varies from UTC) Global $tzo = RegRead("HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\TimeZoneInformation","ActiveTimeBias") Global $y = StringMid($date,13,4) ; The current year Global $m = StringMid($date,9,3) ; The current month Global $d = StringMid($date,6,2) ; The current day of month Global $h = StringMid($date,18,2) ; The current hour Global $n = StringMid($date,21,2) ; The current minute Global $s = StringMid($date,24,2) ; The current second IF $M = "JAN" THEN $M = 1 ; 31 days in month IF $M = "FEB" THEN $M = 2 ; 28/29 days in month IF $M = "MAR" THEN $M = 3 ; 31 days in month IF $M = "APR" THEN $M = 4 ; 30 days in month IF $M = "MAY" THEN $M = 5 ; 31 days in month IF $M = "JUN" THEN $M = 6 ; 30 days in month IF $M = "JUL" THEN $M = 7 ; 31 days in month IF $M = "AUG" THEN $M = 8 ; 31 days in month IF $M = "SEP" THEN $M = 9 ; 30 days in month IF $M = "OCT" THEN $M = 10 ; 31 days in month IF $M = "NOV" THEN $M = 11 ; 30 days in month IF $M = "DEC" THEN $M = 12 ; 31 days in month Global $epm = _EndOfMonth($M-1) ; The last day of the previous month (28-31) Global $ecm = _EndOfMonth($M) ; The last day of the current month (28-31) ; Correct value for $tzo from the registry value to a +/- number of hours IF $tzo < 1500 Then $tzo /= -60 Else $tzo = (4294967296 - $tzo)/60 EndIf ; ---------------------------------------------------------------------------------------------------------------------------------- ; ---------------------------------------------------------------------------------------------------------------------------------- ; ---------------------------------------------------------------------------------------------------------------------------------- ; Calculate time zone changes (correct the hour/day/month/year based on timezone/hour/day/month changes ; ---------------------------------------------------------------------------------------------------------------------------------- $h += $tzo ; Set the local hour based on offset from UTC Select ; If the hour changes day forward/backward, correct appropriate values. Case $h >= 24 $d += 1 $h -= 24 Case $h < 0 $d -= 1 $h += 24 EndSelect Switch $d ; If the day changes month forward/backward, correct appropriate values. Case $ecm+1 $m += 1 $d = 1 Case 0 $m -= 1 $d = $epm EndSwitch Switch $m ; If the month changes year forward/backward, correct appropriate values. Case 0 $m = 12 $d = 31 $y -= 1 Case 13 $m = 1 $d = 1 $y += 1 EndSwitch ; ---------------------------------------------------------------------------------------------------------------------------------- ; ---------------------------------------------------------------------------------------------------------------------------------- ; ---------------------------------------------------------------------------------------------------------------------------------- ; Set system date/time using functions from #include<Date.au3> ; ---------------------------------------------------------------------------------------------------------------------------------- _SetDate($d,$m,$y) _SetTime($h,$n,$s) ; ---------------------------------------------------------------------------------------------------------------------------------- ; ---------------------------------------------------------------------------------------------------------------------------------- ; ---------------------------------------------------------------------------------------------------------------------------------- ; FUNCTION: _EndOfMonth ::: Determine the ending date of the month (28, 29, 30, or 31) ; ---------------------------------------------------------------------------------------------------------------------------------- Func _EndOfMonth($vMON) local $end Switch $vMON Case 0, 1, 3, 5, 7, 8, 10, 12, 13 $end = 31 Case 4, 6, 9, 11 $end = 30 Case 2 If IsInt(@YEAR/4) Then $end = 29 Else $end = 28 EndIf Case Else SetError(-1) Return(0) EndSwitch Return($end) EndFunc ; ---------------------------------------------------------------------------------------------------------------------------------- ; ----------------------------------------------------------------------------------------------------------------------------------