Jump to content

SOLVED - autoit function to pull from ntp?


kor
 Share

Recommended Posts

my rewritten code for anyone else.

#include <Date.au3>

_SetTimeFromNTP("pool.ntp.org")

Func _SetTimeFromNTP($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
    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
Link to comment
Share on other sites

  • 4 years later...

Hi,

I want to have exactly this function. But it didn't work for me because of o proxy. How can I modify the script above to pass through the proxy?

I tried it this way (this snippet only tries to get raw data):

#include <WinHttp.au3>
Global Const $sProxy = "http://prx01.xxx.de:8080"
Global Const $sUserName = "UserName", $sPassword = "PassWord"
$vOpen = _WinHttpOpen(Default, $WINHTTP_ACCESS_TYPE_NAMED_PROXY, $sProxy)
$vConnect = _WinHttpConnect($vOpen, "pool.ntp.org", 123) ; Port 123
$vRequest = _WinHttpSimpleSendRequest_ProxyAuth($vConnect)
Global $sSource = _WinHttpSimpleReadData($vRequest)
ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $sSource = ' & $sSource & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console
Exit

Func _WinHttpSimpleSendRequest_ProxyAuth($hConnect, $sType = Default, $sPath = Default, $sReferrer = Default, $sDta = Default, $sHeader = Default)
    Local $hRequest = _WinHttpSimpleSendRequest($hConnect, $sType, $sPath, $sReferrer, $sDta, $sHeader)
    If $hRequest Then
        Local $iStatusCode = _WinHttpQueryHeaders($hRequest, $WINHTTP_QUERY_STATUS_CODE)
        If $iStatusCode = $HTTP_STATUS_PROXY_AUTH_REQ Then
            Local $iSupportedSchemes, $iFirstScheme, $iAuthTarget ; Query Authorization scheme
            If _WinHttpQueryAuthSchemes($hRequest, $iSupportedSchemes, $iFirstScheme, $iAuthTarget) Then
                _WinHttpSetCredentials($hRequest, $iAuthTarget, $iFirstScheme, $sUserName, $sPassword) ; Set passed credentials
                _WinHttpSendRequest($hRequest)  ; Set passed credentials
                _WinHttpReceiveResponse($hRequest) ; And wait for the response again
            EndIf
        EndIf
    EndIf
    Return $hRequest
EndFunc

$sSource is "". So this does not work.

Regards, Conrad

SciTE4AutoIt = 3.7.3.0   AutoIt = 3.3.14.2   AutoItX64 = 0   OS = Win_10   Build = 19044   OSArch = X64   Language = 0407/german
H:\...\AutoIt3\SciTE     H:\...\AutoIt3      H:\...\AutoIt3\Include     (H:\ = Network Drive)

   88x31.png  Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind.

Link to comment
Share on other sites

You should open a new thread, this one is almost 5 years old, and kor hasn't been here in nearly 2 years.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

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