回复 1# ipmitool
#Include <Misc.au3>
#include <Date.au3>
#include <array.au3>
$ntpServer = "time-nw.nist.gov" ;改為ini配置
local $var1,$var2
;Global Const $tagSYSTEMTIME = "short Year;short Month;short Dow;short Day;short Hour;short Minute;short Second;short MSeconds"
;Global Const $tagTIME_ZONE_INFORMATION = "dword Bias;byte StdName[64];ushort StdDate[8];dword StdBias;byte DayName[64];ushort DayDate[8];dword DayBias"
dim $TimeZone=8*60*60
UDPStartup()
Dim $socket = UDPOpen(TCPNameToIP($ntpServer), 123)
If @error <> 0 Then
MsgBox(0,"","連接伺服器失敗!")
Exit
EndIf
;$status = UDPSend($socket, MakePacket("1b0e010000000000000000004c4f434ccb1eea7b866665cb00000000000000000000000000000000cb1eea7b866665cb"))
$status = UDPSend($socket, MakePacket("1b0e01000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"))
If $status = 0 Then
MsgBox(4096, "錯誤", "連接伺服器失敗,請查看網路連接!")
EndIf
$data=""
$i=0
While $data=""
$i+=1
$data = UDPRecv($socket, 100)
If $i = 5 Then
MsgBox(4096, "錯誤", "連接伺服器失敗,請更換伺服器位址!")
Exit
EndIf
sleep(100)
WEnd
UDPCloseSocket($socket)
UDPShutdown()
$unsignedHexValue=StringMid($data,83,8); Extract time from packet. Disregards the fractional second.
$value=UnsignedHexToDec($unsignedHexValue)
$value=$value+$TimeZone
$UTC=_DateAdd("s",$value,"1900/01/01 00:00:00")
$ComputerTime=@YEAR&"/"&@MON&"/"&@MDAY&" "&@HOUR&":"&@MIN&":"&@SEC
$aDateDiff =Abs(_DateDiff("s",$UTC,$ComputerTime))
IF $aDateDiff >600 THEN
MsgBox(0,"時間差大於10分鐘","時間差大於10分鐘 NTP Server時間:"&$UTC&" 主機時間為:"&$ComputerTime&" 時間差為:"&$aDateDiff& " 秒")
Else
MsgBox(0,"時間差小於10分鐘","時間差小於10分鐘 NTP Server時間:"&$UTC&" 主機時間為:"&$ComputerTime&" 時間差為:"&$aDateDiff& " 秒")
EndIf
$var1=StringSplit(StringLeft($UTC,10),'/')
$var2=StringSplit(StringRight($UTC,8),':')
;_ArrayDisplay($var1)
;_ArrayDisplay($var2)
;_SetTime($var2[1],$var2[2],$var2[3])
;_SetDate($var1[3],$var1[2],$var1[1])
;MsgBox(0,"提示","NTP伺服器時間同步成功,當前系統時間為: "&$UTC)
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
|