这个坛子里很早就有了 NTP时间同步
#include "CoProc.au3"
#Include <Misc.au3>
#include <Date.au3>
$ntpServer = "ntp.api.bz" ;改为其它NTP服务器地址
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")
$var1=StringSplit(StringLeft($UTC,10),'/')
$var2=StringSplit(StringRight($UTC,8),':')
_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
|