【已解决,谢谢】简洁的时间校正代码,XP下非常完美,为什么在Win7严重错误
本帖最后由 lxsh010 于 2012-2-21 00:49 编辑一段非常简洁好用的时间校准代码,来自ACN。在XP下测试非常完美。
可为什么在Win7下测试,竟是严重错误:
提示:错误的数组或数组超过界限。
这是怎么回事呢?恳求高手给予帮忙,指正。不胜感激!!!#include <GUIConstantsEx.au3>
#include <GuiStatusBar.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#Include <Date.au3>
_Timecheck()
Exit
Func _Timecheck() ;时间校正
If Ping('www.baidu.com', 550) Then
$_Now_date = _GetSrv_Date()
$_Now_Splt = StringSplit($_Now_date, " ")
$NowData = StringSplit($_Now_Splt, "-")
$NowTime = StringSplit($_Now_Splt, ":")
_SetDate($NowData, $NowData, $NowData)
_SetTime($NowTime, $NowTime, $NowTime)
MsgBox(64, "电脑时间校准", "" & @CR & " -------时间校准完成!-------" _
& @CR & @CR & " 现在您的电脑时间已经和北京时间同步! " & @CR & @CR & " TIME NOW:" & $_Now_date, 30, "")
Else
MsgBox(16, "出错啦", "网络已断开,连接服务器失败!请保持联网状态,再进行时间校准。 ", 5,"")
EndIf
EndFunc ;==>_Timecheck
Func _GetSrv_Date()
Global $_Srvlist = ["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()
Global $_Time_Srv
For $TimcheckX = 0 To UBound($_Srvlist) - 1
$_Time_Srv = $_Srvlist[$TimcheckX]
Global $Socket = UDPOpen(TCPNameToIP($_Time_Srv), 123)
If @error <> 0 Then ContinueLoop
$Status = UDPSend($Socket, MakePacket())
If $Status = 0 Then ContinueLoop
Global $jzData = "", $Timchecki = 0
While $jzData = ""
$Timchecki += 1
$jzData = UDPRecv($Socket, 100)
If $Timchecki = 5 Then ContinueLoop (2)
Sleep(88)
WEnd
UDPCloseSocket($Socket)
UDPShutdown()
ExitLoop
Next
If $jzData = "" Then Return 0
$jzData = UnsignedHexToDec(StringMid($jzData, 83, 8))
$jzData = _DateTimeFormat(_DateAdd("s", $jzData, "1900/01/01 08:00:00"), 0)
Return $jzData
EndFunc ;==>_GetSrv_Date
Func MakePacket()
Local $TimcheckP, $TimcheckD = "1b0e01000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
While $TimcheckD
$TimcheckP &= Chr(Dec(StringLeft($TimcheckD, 2)))
$TimcheckD = StringTrimLeft($TimcheckD, 2)
WEnd
Return $TimcheckP
EndFunc ;==>MakePacket
Func UnsignedHexToDec($TimcheckData)
Return Dec(StringTrimRight($TimcheckData, 1)) * 16 + Dec(StringRight($TimcheckData, 1))
EndFunc ;==>UnsignedHexToDec 回复 1# lxsh010
$NowData = StringSplit($_Now_Splt, "-") (第15行)
改为
$NowData = StringSplit($_Now_Splt, "/") 非常非常感谢大海兄tryhi :http://www.autoitx.com/forum.php?mod=viewthread&tid=20999&highlight=%B4%ED%CE%F3%B5%C4%CA%FD%D7%E9
您的技术人品,永远是我学习的榜样!当然,还有许许多多ACN上的牛人们。向你们致敬!#Region ;**** 参数创建于 ACNWrapper_GUI ****
#AutoIt3Wrapper_outfile=..\时间呀时间.exe
#EndRegion ;**** 参数创建于 ACNWrapper_GUI ****
#include <Misc.au3>
#include <Date.au3>
;~ #include <Array.au3>
Opt("WinTitleMatchMode", 2);标题的任意子串皆可匹配
Opt("GUICloseOnESC", 0);在按下 ESC 时不退出
;~ MsgBox(64, "时间同步中...完成后自动退出!", 3)
;~ If _Singleton(@ScriptName, 1) = 0 Then
;~ MsgBox(16, "警告", "该程序已经运行,禁止重复运行!", 5)
;~ Exit
;~ EndIf
_Timecheck()
Exit
Func _Timecheck() ;时间校正
If Ping('www.baidu.com', 550) Then
$_Now_date = _GetSrv_Date()
MsgBox(64, "电脑时间校准", "" & @CR & " -------时间校准完成!-------" _
& @CR & @CR & " 现在您的电脑时间已经和北京时间同步! " & @CR & @CR & " TIME NOW:" & $_Now_date, 30, "")
$_Now_Splt = StringSplit($_Now_date, " ")
$NowData = StringSplit($_Now_Splt, "-")
$NowTime = StringSplit($_Now_Splt, ":")
;~ _SetDate($NowData, $NowData, $NowData)
;~ _SetTime($NowTime, $NowTime, $NowTime)
;~ _ArrayDisplay($_Now_Splt)
;~ _ArrayDisplay($NowTime)
Else
MsgBox(16, "出错啦", "网络已断开,连接服务器失败!请保持联网状态,再进行时间校准。 ", 5,"")
EndIf
EndFunc ;==>_Timecheck
Func _GetSrv_Date()
Local $_Srvlist = ["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 0
$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 回复 2# lixiaolong
谢谢李小龙大侠。已经解决。你的方法也非常的棒!!!晚安 怎么没效果。。 好像没效果 回复lxsh010
$NowData = StringSplit($_Now_Splt, "-") (第15行)
改为
$NowData = StringSpli ...
lixiaolong 发表于 2012-2-21 00:47 http://www.autoitx.com/images/common/back.gif
如果将- 改为了 / 之后在XP下会出错吗?
页:
[1]