回复 4# 鉴仔
这个来自论坛的代码,具体是哪个帖子,忘了。对原作者说声:抱歉!
原来用StringSplit截取日期和时间,后来发现在WIN7 64位下无法工作,于是我改用了正则。
#include-once
#include <Date.au3>
_Timecheck()
Func _Timecheck() ;时间校正
If Ping('www.baidu.com', 550) Or Ping ('www.163.com') Then
Local $_Now_date, $year, $mon, $mday, $hour, $min, $sec
$_Now_date = _GetSrv_Date()
If @error Then Return SetError(2, '', 0)
$year = StringRegExpReplace($_Now_date, '/.+$', '')
$mon = StringRegExpReplace($_Now_date, '.+?/(\d{1,2})/.+', '\1')
$mday = StringRegExpReplace($_Now_date, '.+/(\d{1,2})\D.+', '\1')
$hour = StringRegExpReplace($_Now_date, '.+?(\d{1,2}):.+', '\1')
$min = StringRegExpReplace($_Now_date, '.+?:(\d{1,2}):.+', '\1')
$sec = StringRegExpReplace($_Now_date, '.+:', '')
_SetDate($mday, $mon, $year)
_SetTime($hour, $min, $sec)
Return 1
EndIf
Return SetError(1, '', 0)
EndFunc ;==>_Timecheck
Func _GetSrv_Date()
Local $_Srvlist[14] = ["time-nw.nist.gov", _
"time-a.nist.gov", _
"time-b.nist.gov", _
"time-a.timefreq.bldrdoc.gov", _
"time-b.timefreq.bldrdoc.gov", _
"time-c.timefreq.bldrdoc.gov", _
"utcnist.colorado.edu", _
"time.nist.gov", _
"nist1.datum.com", _
"nist1.dc.glassey.com", _
"nist1.ny.glassey.com", _
"nist1.sj.glassey.com", _
"nist1.aol-ca.truetime.com", _
"nist1.aol-va.truetime.com"]
UDPStartup()
Local $_Time_Srv
For $x = 0 To UBound($_Srvlist) - 1
$_Time_Srv = $_Srvlist[$x]
Local $Socket = UDPOpen(TCPNameToIP($_Time_Srv), 123)
If @error <> 0 Then ContinueLoop
$Status = UDPSend($Socket, MakePacket())
If $Status = 0 Then ContinueLoop
Local $Data = "", $i = 0
While $Data = ""
$i += 1
$Data = UDPRecv($Socket, 100)
If $i = 5 Then ContinueLoop (2)
Sleep(88)
WEnd
UDPCloseSocket($Socket)
UDPShutdown()
ExitLoop
Next
If $Data = "" Then Return SetError(1, '', '')
$Data = UnsignedHexToDec(StringMid($Data, 83, 8))
$Data = _DateTimeFormat(_DateAdd("s", $Data, "1900/01/01 08:00:00"), 0)
Return $Data
EndFunc ;==>_GetSrv_Date
Func MakePacket()
Local $P, $D = "1b0e01000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
While $D
$P &= Chr(Dec(StringLeft($D, 2)))
$D = StringTrimLeft($D, 2)
WEnd
Return $P
EndFunc ;==>MakePacket
Func UnsignedHexToDec($_Data)
Return Dec(StringTrimRight($_Data, 1)) * 16 + Dec(StringRight($_Data, 1))
EndFunc ;==>UnsignedHexToD
|