找回密码
 加入
搜索
查看: 1686|回复: 1

[AU3基础] 时间同步

[复制链接]
发表于 2010-9-14 16:30:06 | 显示全部楼层 |阅读模式
#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


麻烦大家帮我看看, 为什么使用运行无效。。。。

相关帖子

发表于 2010-9-15 22:28:40 | 显示全部楼层
整理下代码先、。。
您需要登录后才可以回帖 登录 | 加入

本版积分规则

QQ|手机版|小黑屋|AUTOIT CN ( 鲁ICP备19019924号-1 )谷歌 百度

GMT+8, 2024-11-15 02:28 , Processed in 0.070332 second(s), 23 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表