#Include <Date.au3>
_timesync()
Func _timesync() ;时间同步函数
$TimeZone = 8 * 60 * 60
UDPStartup()
$socket = UDPOpen(TCPNameToIP("210.72.145.8"), 123) ;TcpNameToIP把域名转换成IP.
$pac = "1b0e01000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
$status = UDPSend($socket, Chr(Dec(StringLeft($pac, 2))))
If @error <> 0 Then Return
If $status = 0 Then Return
$data = ""
$i = 0
While $data = ""
$i += 1
$data = UDPRecv($socket, 100)
If $i = 5 Then Return
Sleep(100)
WEnd
UDPCloseSocket($socket)
UDPShutdown()
$unsignedHexValue = StringMid($data, 83, 8); Extract time from packet. Disregards the fractional second.
$ones = StringRight($unsignedHexValue, 1)
$n = StringTrimRight($unsignedHexValue, 1)
$value = Dec($n) * 16 + Dec($ones)
$value = $value + $TimeZone
$UTC = _DateAdd("s", $value, "1900/01/01 00:00:00")
$localtime = @YEAR & "/" & @MON & "/" & @MDAY & " " & @HOUR & ":" & @MIN & ":" & @SEC
$iDateCalc = _DateDiff('s', $UTC, $localtime)
$var1 = StringSplit(StringLeft($UTC, 10), '/')
$var2 = StringSplit(StringRight($UTC, 8), ':')
_SetTime($var2[1], $var2[2], $var2[3])
_SetDate($var1[3], $var1[2], $var1[1])
If $iDateCalc > "60" Or $iDateCalc < "-" & "60" Then
MsgBox(64, "时间已同步", "本地时间和国际标准时间存在误差,已同步!" & @CRLF & "本机时间:" & $localtime & @CRLF & "标准时间:" & $UTC & @CRLF & "时间差距:" & $iDateCalc & "秒")
EndIf
EndFunc ;==>_timesync
麻烦大家帮我看看, 为什么使用运行无效。。。。 |