Jump to content

yn0t

Members
  • Posts

    4
  • Joined

  • Last visited

yn0t's Achievements

Seeker

Seeker (1/7)

0

Reputation

  1. After all that I found a bug in my code that didn't update the Time Zone field properly or the offset on a Daylight Savings change. This should take care of it. #include <ButtonConstants.au3> #include <EditConstants.au3> #include <GUIConstantsEx.au3> #include <StaticConstants.au3> #include <WindowsConstants.au3> #include <Date.au3> $AppTitle = "NTP Client" $AppVersion = "1.0" $AppTitleVersion = $AppTitle & " v" & $AppVersion #Region ### START Koda GUI section ### $frmMain = GUICreate($AppTitleVersion, 498, 271, 239, 155) $txtNTPServer = GUICtrlCreateInput("pool.ntp.org", 72, 8, 417, 21) $Label1 = GUICtrlCreateLabel("NTP Server:", 8, 10, 63, 17) $cmdQuery = GUICtrlCreateButton("Query", 416, 240, 75, 25, $WS_GROUP) $grpNTPServer = GUICtrlCreateGroup("NTP Server", 8, 32, 481, 73) $Label2 = GUICtrlCreateLabel("NTP Server UTC:", 48, 52, 88, 17) $txtNTPServerUTC = GUICtrlCreateInput("", 136, 48, 345, 21, BitOR($ES_AUTOHSCROLL,$ES_READONLY)) $txtNTPServerLocalTime = GUICtrlCreateInput("", 136, 72, 345, 21, BitOR($ES_AUTOHSCROLL,$ES_READONLY)) $Label5 = GUICtrlCreateLabel("NTP Server Local Time:", 18, 76, 118, 17) GUICtrlCreateGroup("", -99, -99, 1, 1) $grpLocal = GUICtrlCreateGroup("Local Computer", 8, 112, 481, 121) $Label3 = GUICtrlCreateLabel("Time Zone:", 73, 134, 58, 17) $Label4 = GUICtrlCreateLabel("Offset (hours):", 61, 154, 70, 17) $Label6 = GUICtrlCreateLabel("UTC Time:", 76, 177, 55, 17) $txtTimeZone = GUICtrlCreateInput("", 136, 128, 345, 21, BitOR($ES_AUTOHSCROLL,$ES_READONLY)) $txtOffset = GUICtrlCreateInput("", 136, 152, 345, 21, BitOR($ES_AUTOHSCROLL,$ES_READONLY)) $txtLocalUTCTime = GUICtrlCreateInput("", 136, 176, 345, 21, BitOR($ES_AUTOHSCROLL,$ES_READONLY)) $txtLocalTime = GUICtrlCreateInput("", 136, 200, 345, 21, BitOR($ES_AUTOHSCROLL,$ES_READONLY)) $Label9 = GUICtrlCreateLabel("Local Time:", 72, 199, 59, 17) GUICtrlCreateGroup("", -99, -99, 1, 1) GUISetState(@SW_SHOW) #EndRegion ### END Koda GUI section ### ;************************************************************************************************** ;** Functions ************************************************************************************* ;************************************************************************************************** Func MakePacket($d) Local $p="" While $d $p&=Chr(Dec(StringLeft($d,2))) $d=StringTrimLeft($d,2) WEnd Return $p EndFunc Func UnsignedHexToDec($n) $ones=StringRight($n,1) $n=StringTrimRight($n,1) Return dec($n)*16+dec($ones) EndFunc Func QueryNTP($ntpServer) UDPStartup() Dim $socket = UDPOpen(TCPNameToIP($ntpServer), 123) If @error <> 0 Then MsgBox(0,"","Can't open connection!") Return 0 EndIf $status = UDPSend($socket, MakePacket("1b0e01000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000")) If $status = 0 Then MsgBox(0, "ERROR", "Error while sending UDP message: " & @error) Return 0 EndIf $data="" While $data="" $data = UDPRecv($socket, 100) sleep(100) WEnd UDPCloseSocket($socket) UDPShutdown() $unsignedHexValue=StringMid($data,83,8) ; Extract time from packet. Disregards the fractional second. $value=UnsignedHexToDec($unsignedHexValue) $TZinfo = _Date_Time_GetTimeZoneInformation() ;msgbox(4096, "", $TZinfo[0]) $UTC=_DateAdd("s",$value,"1900/01/01 00:00:00") Switch $TZinfo[0] Case 0 ;Daylight Savings not used in current time zone $TZoffset = 0 $TimeZone = $TZinfo[2] Case 1 ;Standard Time $TZoffset = ($TZinfo[1]) * -1 $TimeZone = $TZinfo[2] Case 2 ;Daylight Savings Time $TZoffset = ($TZinfo[1] + $TZinfo[7]) * -1 $TimeZone = $TZinfo[5] EndSwitch GUICtrlSetData($txtNTPServerUTC, $UTC) GUICtrlSetData($txtNTPServerLocalTime, _DateAdd("n",$TZoffset,$UTC)) GUICtrlSetData($txtTimeZone, $TimeZone) GUICtrlSetData($txtOffset, $TZoffset / 60) GUICtrlSetData($txtLocalUTCTime, _DateAdd("n",$TZoffset * -1,_NowCalc())) GUICtrlSetData($txtLocalTime, _NowCalc()) EndFunc Func CenterWindow($window_title) $position = WinGetPos($window_title) WinMove($window_title, "", (@desktopwidth / 2) - ($position[2] / 2), (@desktopheight / 2) - ($position[3] / 2)) EndFunc CenterWindow($AppTitleVersion) While 1 $nMsg = GUIGetMsg() Switch $nMsg Case $cmdQuery QueryNTP(GUICtrlRead($txtNTPServer)) Case $GUI_EVENT_CLOSE Exit EndSwitch WEnd
  2. Bug report submitted (#1180). Also, for those interested, I've taken the code above and created a "nice" GUI interface for it. I'm using it to test some time servers here at work with some simple NTP queries. It also fixes the Daylight Savings error I mentioned above and I've added a couple fields that could be helpful in troubleshooting the time servers. #include <ButtonConstants.au3> #include <EditConstants.au3> #include <GUIConstantsEx.au3> #include <StaticConstants.au3> #include <WindowsConstants.au3> #include <Date.au3> $AppTitle = "NTP Client" $AppVersion = "1.0" $AppTitleVersion = $AppTitle & " v" & $AppVersion #Region ### START Koda GUI section ### $frmMain = GUICreate($AppTitleVersion, 498, 271, 239, 155) $txtNTPServer = GUICtrlCreateInput("pool.ntp.org", 72, 8, 417, 21) $Label1 = GUICtrlCreateLabel("NTP Server:", 8, 10, 63, 17) $cmdQuery = GUICtrlCreateButton("Query", 416, 240, 75, 25, $WS_GROUP) $grpNTPServer = GUICtrlCreateGroup("NTP Server", 8, 32, 481, 73) $Label2 = GUICtrlCreateLabel("NTP Server UTC:", 48, 52, 88, 17) $txtNTPServerUTC = GUICtrlCreateInput("", 136, 48, 345, 21, BitOR($ES_AUTOHSCROLL,$ES_READONLY)) $txtNTPServerLocalTime = GUICtrlCreateInput("", 136, 72, 345, 21, BitOR($ES_AUTOHSCROLL,$ES_READONLY)) $Label5 = GUICtrlCreateLabel("NTP Server Local Time:", 18, 76, 118, 17) GUICtrlCreateGroup("", -99, -99, 1, 1) $grpLocal = GUICtrlCreateGroup("Local Computer", 8, 112, 481, 121) $Label3 = GUICtrlCreateLabel("Time Zone:", 73, 134, 58, 17) $Label4 = GUICtrlCreateLabel("Offset (hours):", 61, 154, 70, 17) $Label6 = GUICtrlCreateLabel("UTC Time:", 76, 177, 55, 17) $txtTimeZone = GUICtrlCreateInput("", 136, 128, 345, 21, BitOR($ES_AUTOHSCROLL,$ES_READONLY)) $txtOffset = GUICtrlCreateInput("", 136, 152, 345, 21, BitOR($ES_AUTOHSCROLL,$ES_READONLY)) $txtLocalUTCTime = GUICtrlCreateInput("", 136, 176, 345, 21, BitOR($ES_AUTOHSCROLL,$ES_READONLY)) $txtLocalTime = GUICtrlCreateInput("", 136, 200, 345, 21, BitOR($ES_AUTOHSCROLL,$ES_READONLY)) $Label9 = GUICtrlCreateLabel("Local Time:", 72, 199, 59, 17) GUICtrlCreateGroup("", -99, -99, 1, 1) GUISetState(@SW_SHOW) #EndRegion ### END Koda GUI section ### ;************************************************************************************************** ;** Functions ************************************************************************************* ;************************************************************************************************** Func MakePacket($d) Local $p="" While $d $p&=Chr(Dec(StringLeft($d,2))) $d=StringTrimLeft($d,2) WEnd Return $p EndFunc Func UnsignedHexToDec($n) $ones=StringRight($n,1) $n=StringTrimRight($n,1) Return dec($n)*16+dec($ones) EndFunc Func QueryNTP($ntpServer) UDPStartup() Dim $socket = UDPOpen(TCPNameToIP($ntpServer), 123) If @error <> 0 Then MsgBox(0,"","Can't open connection!") Return 0 EndIf $status = UDPSend($socket, MakePacket("1b0e01000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000")) If $status = 0 Then MsgBox(0, "ERROR", "Error while sending UDP message: " & @error) Return 0 EndIf $data="" While $data="" $data = UDPRecv($socket, 100) sleep(100) WEnd UDPCloseSocket($socket) UDPShutdown() $unsignedHexValue=StringMid($data,83,8) ; Extract time from packet. Disregards the fractional second. $value=UnsignedHexToDec($unsignedHexValue) $TZinfo = _Date_Time_GetTimeZoneInformation() $TZoffset= ($TZinfo[1] + $TZinfo[7]) * -1 $UTC=_DateAdd("s",$value,"1900/01/01 00:00:00") GUICtrlSetData($txtNTPServerUTC, $UTC) GUICtrlSetData($txtNTPServerLocalTime, _DateAdd("n",$TZoffset,$UTC)) GUICtrlSetData($txtTimeZone, $TZinfo[5]) GUICtrlSetData($txtOffset, $TZoffset / 60) GUICtrlSetData($txtLocalUTCTime, _DateAdd("n",$TZoffset * -1,_NowCalc())) GUICtrlSetData($txtLocalTime, _NowCalc()) EndFunc Func CenterWindow($window_title) $position = WinGetPos($window_title) WinMove($window_title, "", (@desktopwidth / 2) - ($position[2] / 2), (@desktopheight / 2) - ($position[3] / 2)) EndFunc CenterWindow($AppTitleVersion) While 1 $nMsg = GUIGetMsg() Switch $nMsg Case $cmdQuery QueryNTP(GUICtrlRead($txtNTPServer)) Case $GUI_EVENT_CLOSE Exit EndSwitch WEnd
  3. Thanks Manadar. I'll go ahead and open a ticket.
  4. Hey All, This is my first post to the forum but I've been around a long time and have written a couple AutoIT apps. I finally have something to contribute. Also, I realize that this is an old thread. Apologies in advanced. I was utilizing the code above to create a simple NTP client GUI that I could use to test some of the time servers here at work. As I was playing around with the code above I found that some of the time reports were inaccurate due to it not properly taking into account Daylight Savings Time. To correct the problem I've changed the $TZoffset=$TZinfo[1]*-1 line to: $TZoffset= ($TZinfo[1] + $TZinfo[7]) * -1 This seems to handle the time changes. I've moved my PC in and out of Daylight Savings and it appears to adjust properly. --- Also, on a side note. Is there a place I can help update some of the AutoIT documentation on the _Date_Time_GetTimeZoneInformation() function? I believe it is also inaccurate. It says that variables [3] and [6] give a date and time when the daylight savings starts/ends. I believe it is actually returning the month, and which Sunday the change occurs. For instance it reports that one of the changes occurs at 03/02/0000 00:02:00, which could be interpreted as March 2nd. It is actually indicating that it changes in March, on the 2nd Sunday (March 8th, 2009 this year).
×
×
  • Create New...