获取网络时间的功能不可用了,在此求助!急用,谢谢。(已解决)
本帖最后由 txm888 于 2012-5-20 11:47 编辑我发现论坛里获取网络时间的功能不可用了,可能是时间服务器的网页改版了,什么都读取不了
#include <IE.au3>
$Form1 = GUICreate("", 468, 584, -1, -1)
$oIE = _IECreateEmbedded()
$GUIActiveX = GUICtrlCreateObj($oIE, 12, 65, 440, 468)
GUISetState(@SW_HIDE)
_IENavigate ($oIE,"http://www.timedate.cn/worldclock/ti.asp")
$oP = _IETagNameGetCollection($oIE, 'table', 0)
MsgBox(0, "北京时间", $oP.innertext)
和
#include <IE.au3>
$oIE = _IECreate ("http://www.timedate.cn/worldclock/ti.asp")
$sText = _IEBodyReadText ($oIE)
MsgBox(0, "Body Text", $sText)
都不行,求助,谢谢! #include <Date.au3>
MsgBox(0, "", _Time())
Func _Time()
$oHTTP = ObjCreate("MSXML2.XMLHTTP")
$oHTTP.Open("get", "http://open.baidu.com/special/time/", False)
$oHTTP.Send()
$a = BinaryToString($oHTTP.responsebody)
$aReturn = StringRegExp($a, 'window.baidu_time\((.*?)\);', 3)
If @error = 0 Then
$sec = $aReturn
Return _DateAdd("s", Round($sec / 1000), "1970/01/01 08:00:00")
EndIf
EndFunc ;==>_Time
用百度这个吧 稳定的很 回复 1# txm888
试了该链接, 有时获取到, 但更多时候确实获取不到.
换了下面链接, "现在"能百分百获取到:
#include <IE.au3>
Local $url = "http://www.timedate.cn/worldclock/city.asp?n=1"
$Form1 = GUICreate("", 468, 584, -1, -1)
$oIE = _IECreateEmbedded()
$GUIActiveX = GUICtrlCreateObj($oIE, 12, 65, 440, 468)
;GUISetState()
_IENavigate($oIE, $url, 0) ; 查看 北京时间 的链接
Sleep(1000)
$oP = _IETagNameGetCollection($oIE, 'table') ; 此链接包含多个 Table, 它们都没有作ID标志
; 所以下面只能一一枚举而获取北京时间了
Local $date, $bool = False
For $iop In $oP
;ConsoleWrite($iop.innertext & @CR)
$date = StringRegExp($iop.innertext, '当地时间\h*(\d{2,4}年\d{1,2}月\d{1,2}日.+)', 3) ; 正则获取北京时间
If Not @error Then
$bool = True
ExitLoop
EndIf
Next
If $bool Then
MsgBox(64, '操作成功', '当前北京时间: ' & $date)
Else
MsgBox(48, '操作失败', $url & @CRLF & '请查看此网页的新变动! ')
EndIf
;While GUIGetMsg() + 3
;WEnd #include <GUIConstantsEx.au3>
#include <IE.au3>
TraySetState (2 )
_IEErrorHandlerRegister ()
$oIE = _IECreateEmbedded ()
$Form1 =GUICreate("程序时间限制测试", 180, 50)
$GUIActiveX = GUICtrlCreateObj($oIE, 0, 1, 150, 50)
GUISetState()
_IENavigate ($oIE, "http://www.timedate.cn/worldclock/ti.asp")
$sText = _IEBodyReadText ($oIE)
While 1
Sleep(1000)
WEnd 回复 2# my788522
太感谢您了,哥,这的确稳定,又快,谢谢! 回复 3# user3000
也谢谢你,代码上比楼上的稍微多点儿,我暂就用楼上的方案,谢谢 回复 4# ooxxgod
谢谢 回复 6# txm888
呵呵, 那你接着你发出来的代码编的, 当作学习吧.
其实论坛上有相关的UDF, 非读取网页方式获取, 应该说是最可靠的!
#include-once
#include <Date.au3>
MsgBox(0, '', _GetSrv_Date())
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 回复 8# user3000
牛,请问下以这些网址来判断时间精确吗?什么样的原理小的还没明白,请讲下,谢谢大哥!
["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"] 它们都是网络上可用来校对时间的(应该都是外国的吧)时间服务器;
其实有一个能用的就够了, 但设多几个作'备用'就万无一失了.
然后用UDP协议建立连接, 成功后发送自己时区申请'标准时间' 回复 9# txm888
说到精确, 本来是很精确的, 但, 有网络延迟, 自然谈不上很精确了! 回复 10# user3000
又了解了些些,谢谢你,分享真是一种社会文明的体现... 谢谢分享。
页:
[1]