Introser Posted September 16, 2018 Share Posted September 16, 2018 Hey Guys, I need the correct time from a ntp server, with millisecs. I found this script wich give me the time in seconds expandcollapse popup;************************************************************************************************** ; Ab hier beginnen die Funktionen, zum holen der Zeit aus dem Internet (Quelle: www.autoitscript.com/forum/) ;************************************************************************************************** Func _TimeSync() Local $ntpServer = 'de.pool.ntp.org' UDPStartup() Dim $socket = UDPOpen(TCPNameToIP($ntpServer), 123) If @error <> 0 Then Return SetError(1) ;$status = UDPSend($socket, MakePacket('1b0e010000000000000000004c4f434ccb1eea7b866665cb00000000000000000000000000000000cb1eea7b866665cb')) Local $status = UDPSend($socket, MakePacket('1b0e01000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000')) If $status = 0 Then Return SetError(1) Local $data = '', $a = TimerInit() ; Timer setzen, damit im Fehlerfall die Schleife abgebrochen wird While $data = '' $data = UDPRecv($socket, 250) sleep(100) If TimerDiff($a) > 1000 Then ExitLoop ; Wenn Timer > 1sek. (Fehler), dann Schleife verlassen WEnd If $data <> '' Then UDPShutdown() Local $unsignedHexValue = StringMid($data,83,8); Extract time from packet. Disregards the fractional second. Local $value = UnsignedHexToDec($unsignedHexValue) Local $TZinfo = _Date_Time_GetTimeZoneInformation() Local $TZoffset = -(UnsignedToLong($TZinfo[1]) + (UnsignedToLong($TZinfo[4])*($TZinfo[0]<2)) + (UnsignedToLong($TZinfo[7])*($TZinfo[0]=2))) Local $UTC = _DateAdd('s',$value,'1900/01/01 00:00:00') Return _DateAdd('n',$TZoffset,$UTC) EndIf SetError(1) EndFunc ;************************************************************************************************** Func MakePacket($d) Local $p = '' While $d $p &= Chr(Dec(StringLeft($d,2))) $d = StringTrimLeft($d,2) WEnd Return $p EndFunc ;************************************************************************************************** Func UnsignedHexToDec($n) Local $ones = StringRight($n,1) $n = StringTrimRight($n,1) Return dec($n)*16+dec($ones) EndFunc ;************************************************************************************************** ; http://support.microsoft.com/kb/189323 ; Gibt Die Zahl mit Vorzeichen zurück. ; Bei Fehler ( kleiner 0 oder größer MAXINT_4) wird der übergebene Wert zurückgegeben und @error auf 1 gesetzt. Func UnsignedToLong($Value) Local Const $OFFSET_4 = 4294967296 Local Const $MAXINT_4 = 2147483647 If $Value < 0 Or $Value >= $OFFSET_4 Then Return SetError(1,0,$Value) ;' Overflow If $Value <= $MAXINT_4 Then Return $Value Else Return $Value - $OFFSET_4 EndIf EndFunc ;************************************************************************************************** It says: Local $unsignedHexValue = StringMid($data,83,8); Extract time from packet. Disregards the fractional second. So I think there are the milli secs in it. But I couldnt extract it. I checked the NTP package doc and it says that there are 64bit with 32bit time and 32bit fraction seconds. I ectracted the bytes from 91 to 99, like in the example. But didnt know what to do with that It wasnt the millisecs. Anyone know how to extract and read the millisecs with that example? Link to comment Share on other sites More sharing options...
jchd Posted September 16, 2018 Share Posted September 16, 2018 Sync'ing down to ms range from WAN NTP is very hard and precision-wise unsafe due to extremely varying and often asymetric network lag times as well as your own PC multitasking. Just the Sleep(100) and other subsequent instructions in your code are eating a largely variable number of ms which will make your ms result essentially meaningless. If you really need serious precision timing you can sync to a local precision Xtal or Rubidium clock (short term), or better a GPS clock but the latter requires a fixed GPS antena which may not be compatible with your context. Depending on your requirements about short and long term precision and reliability the needed setup might reveal non-trivial. Some time ago I was running a (now dismanteled) local timebase using 1 precision Xtal and 2 Rb clocks all 3 being linked in Napoleon hat, backing up a Trimble GPS clock. This was used in my lab for high precision (<5ps) instrument time calibration purposes, a cheap but solid local alternative to expensive remote traceability to national standards. 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 hereRegExp tutorial: enough to get startedPCRE 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 More sharing options...
Introser Posted September 16, 2018 Author Share Posted September 16, 2018 16 minutes ago, jchd said: Sync'ing down to ms range from WAN NTP is very hard and precision-wise unsafe due to extremely varying and often asymetric network lag times as well as your own PC multitasking. Just the Sleep(100) and other subsequent instructions in your code are eating a largely variable number of ms which will make your ms result essentially meaningless. If you really need serious precision timing you can sync to a local precision Xtal or Rubidium clock (short term), or better a GPS clock but the latter requires a fixed GPS antena which may not be compatible with your context. Depending on your requirements about short and long term precision and reliability the needed setup might reveal non-trivial. Some time ago I was running a (now dismanteled) local timebase using 1 precision Xtal and 2 Rb clocks all 3 being linked in Napoleon hat, backing up a Trimble GPS clock. This was used in my lab for high precision (<5ps) instrument time calibration purposes, a cheap but solid local alternative to expensive remote traceability to national standards. Thansk for that answer. But I dont need it thaaat exactly. Its enough that its correctly in a range of 100ms. I know that the time depends on the ping and network too but I dont need it that correctly. But on a second basis its not enough Link to comment Share on other sites More sharing options...
CYCho Posted December 5, 2019 Share Posted December 5, 2019 (edited) If you are still in need of millisec information from NTP server, please take a look at this topic. According to my experience during the past few weeks, this code has a precision of plus or minus 10 millisecs. Please bear in mind that internet upload/download speed may affect the whole process and you may have to change some parts of the code if it doesn't work properly. Edited December 5, 2019 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