Jump to content

Update system date/time via HTTP (Firewall workaround)


Overkill
 Share

Recommended Posts

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
; ----------------------------------------------------------------------------------------------------------------------------------
; ----------------------------------------------------------------------------------------------------------------------------------
Link to comment
Share on other sites

  • 2 weeks later...

Somehow the code got mangled... I just tested this and it works really nicely, hope I can post it back in a more usable format... even though the indentation has been stripped... oh well!

Thanks to the original author!

Cheers.

#RequireAdmin
#include

; 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_MACHINESYSTEMCurrentControlSetControlTimeZoneInformation","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
; ----------------------------------------------------------------------------------------------------------------------------------
_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
; ----------------------------------------------------------------------------------------------------------------------------------
; ----------------------------------------------------------------------------------------------------------------------------------
Edited by kyo
Link to comment
Share on other sites

  • 3 months later...

Somehow the code got mangled... I just tested this and it works really nicely, hope I can post it back in a more usable format... even though the indentation has been stripped... oh well!

Thanks to the original author!

Cheers.

Global $tzo = RegRead("HKEY_LOCAL_MACHINESYSTEMCurrentControlSetControlTimeZoneInformation","ActiveTimeBias")

Should be

Global $tzo = RegRead("HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\TimeZoneInformation","ActiveTimeBias")
Edited by krawhitham
Link to comment
Share on other sites

  • 1 year later...

I know this is an old thread, but I reworked the code to place everything in a func which changed globals to locals, and it returns the date/time in an array.  so that way you can do 1 global variable array if one so desired.  The missing backslashes are there.

;usage
Global $currentdatetimearr = SetSysTime() ;or you could just do SetSysTime() without the global variable.

func SetsysTime()
; 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)

Local $tzo = RegRead("HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\TimeZoneInformation", "ActiveTimeBias"), _
$y = StringMid($date, 13, 4), _ ; The current year
$m = StringMid($date, 9, 3), _ ; The current month
$d = StringMid($date, 6, 2), _ ; The current day of month
$h = StringMid($date, 18, 2), _ ; The current hour
$n = StringMid($date, 21, 2), _ ; The current minute
$s = StringMid($date, 24, 2) ; The current second
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 $m
    Case 'JAN'; 31 days in month
        $m = 1
    Case 'FEB'; 28/29 days in month
        $m = 2
    Case 'MAR' ; 31 days in month
        $m = 3
    Case 'APR'; 30 days in month
        $m = 4
    Case 'MAY'; 31 days in month
        $m = 5
    Case 'JUN' ; 30 days in month
        $m = 6
    Case 'JUL' ; 31 days in month
        $m = 7
    Case 'AUG' ; 31 days in month
        $m = 8
    Case 'SEP' ; 30 days in month
        $m = 9
    Case 'OCT' ; 31 days in month
        $m = 10
    Case 'NOV' ; 30 days in month
        $m = 11
    Case 'DEC' ; 31 days in month
        $m = 12
EndSwitch

Local $epm = _EndOfMonth($m - 1), _ ; The last day of the previous month (28-31)
$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

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
; ----------------------------------------------------------------------------------------------------------------------------------
_SetDate($d, $m, $y)
_SetTime($h, $n, $s)

local $sysdatetime[6]
$sysdatetime[0] = $m
$sysdatetime[1] = $d
$sysdatetime[2] = $y
$sysdatetime[3] = $h
$sysdatetime[4] = $n
$sysdatetime[5] = $s

Return $sysdatetime


; ----------------------------------------------------------------------------------------------------------------------------------
; ----------------------------------------------------------------------------------------------------------------------------------
; ----------------------------------------------------------------------------------------------------------------------------------
; FUNCTION: _EndOfMonth ::: Determine the ending date of the month (28, 29, 30, or 31)
; ----------------------------------------------------------------------------------------------------------------------------------

EndFunc

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   ;==>_EndOfMonth
Link to comment
Share on other sites

  • 6 months later...

Hi,

it is a perfect solution. I will use it for a system with proxy-connection.

So i added a Line into the script an it works fantastic:

$site = "http://www.google.com/"
$oHTTP = ObjCreate("winhttp.winhttprequest.5.1")
$oHttp.SetProxy(2,"192.168.10.51:28910") ; this line is for using a proxy
$oHTTP.Open("GET", "http://" & $site & "/", False)
$oHTTP.Send()
$date = $oHTTP.GetResponseHeader("Date")

 

Link to comment
Share on other sites

BTW sync-ing thru http is silly, NTP was created just for doing that.

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

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...