时间同步

许多实用脚本和封装函数, 可以让您编写脚本更加便捷高效

Moderators: tmplinshi, arcticir

tmplinshi
Posts: 1604
Joined: 01 Oct 2013, 14:57

时间同步

10 Jan 2014, 14:32

Image

Code: Select all

#NoEnv
#SingleInstance force
SetBatchLines, -1

; 托盘菜单
Menu, Tray, NoStandard
Menu, Tray, Add, 退出, GuiClose

; 命令行方式启动
if 1 = /NoGui
{
	Gosub, Button同步时间
	Sleep, 2000
	ExitApp
}

; 界面
Gui, Font, s12
Gui, Add, ActiveX, w320 h230 vWB Hidden Disabled, Shell.Explorer
Gui, Add, Text, xp wp hp Center 0x200 vPrompt cGreen, 载入中...
Gui, Add, Button, x70 w200 h40 Default Disabled vbtn, 同步时间
Gui, Show

; 载入网页
WB.Navigate("http://open.baidu.com/special/time/")
While, WB.readystate != 4 || WB.busy
   Sleep 10

; 修改 html
html := wb.document.DocumentElement.innerHTML
html := RegExReplace(html, "si)<div id=head>.*(<DIV id=bjtime class=main>)", "$1")
html := RegExReplace(html, "i)<div class=shouye>.*")
StringReplace, html, html, <p><span class=t4>时间校对</span></p>
html := RegExReplace(html, "si)<p>标准北京时间.*\+8小时</p>")
html := RegExReplace(html, ".*内容系百度根据您的指令自动搜索的.*")

WB.Document.Write(html)
WB.Document.GetElementsByTagName("body")[0].style.overflow := "hidden" ; 不显示滚动条

; 修改控件状态
GuiControl, Hide, Prompt
GuiControl, Show, WB
GuiControl, Enable, btn
Return

Button同步时间:
	GuiControl, Disable, btn
	GuiControl,, btn, 同步中...
	TrayTip, %A_ScriptName%, 时间同步中...

	While, !UrlDownloadToVar("http://open.baidu.com/special/time/", html)
		Sleep, 1000

	if !RegExMatch( html, "i)window\.baidu_time\(\K\d+", unixTimestamp )
	{
		MsgBox2("获取百度时间失败", "错误", 48)
		GuiControl, Enable, btn
		GuiControl,, btn, 同步时间
		Return
	}

	unixTimestamp := SubStr(unixTimestamp, 1, 10)
	unixTimestamp := unix2Human(unixTimestamp)
	FormatTime, _date, %unixTimestamp%, yyyy-MM-dd
	FormatTime, _time, %unixTimestamp%, HH:mm:ss

	RunWait, %ComSpec% /c date %_date% & time %_time%,, Hide

	GuiControl, Enable, btn
	GuiControl,, btn, 同步时间
	TrayTip, %A_ScriptName%, 同步完成!
	SetTimer, RemoveTrayTip, -5000
Return

RemoveTrayTip:
	TrayTip
Return

GuiClose:
ExitApp

; ==============================================================================
UrlDownloadToVar(Url, ByRef Result, Timeout = -1)
{
	ComObjError(0)
	static WinHttpReq := ComObjCreate("WinHttp.WinHttpRequest.5.1")
	WinHttpReq.Open("GET", Url, True)
	WinHttpReq.Send()
	ReturnCode := WinHttpReq.WaitForResponse(Timeout) ; Success = -1, Timeout = 0, No response = Empty String
	Result     := WinHttpReq.ResponseText
	Return ReturnCode
}

unix2Human(unixTimestamp)
{
	_date = 1970010108
	_date += unixTimestamp, s
	return _date
}

MsgBox2(Text, Title = "", Options = "")
{
	Gui, +OwnDialogs
	MsgBox, % Options, %Title%, %Text%
}
Last edited by tmplinshi on 10 Jan 2014, 20:57, edited 3 times in total.
User avatar
amnesiac
Posts: 186
Joined: 22 Nov 2013, 03:08
Location: Egret Island, China
Contact:

Re: 时间同步

10 Jan 2014, 19:58

实用,虽然没仔细看代码,我一直在使用一个类似的小工具(在一台每次冷启动都会复位时间的电脑上)。
建议加上同步后退出的选项。
tmplinshi
Posts: 1604
Joined: 01 Oct 2013, 14:57

Re: 时间同步

10 Jan 2014, 20:37

谢谢建议 :)
我增加了命令行方式启动——如果用 /NoGui 参数启动,则不显示界面,同步后就退出。
User avatar
amnesiac
Posts: 186
Joined: 22 Nov 2013, 03:08
Location: Egret Island, China
Contact:

Re: 时间同步

10 Jan 2014, 23:45

真快,下次需要时在那台电脑用用这个。
wzxiaodu
Posts: 12
Joined: 30 Nov 2013, 09:42

Re: 时间同步

12 Jan 2014, 08:12

太好了,学习了。。
不过,win7系统下
开头最好加
if not A_IsAdmin
{
Run *RunAs "%A_AhkPath%" "%A_ScriptFullPath%"
ExitApp
}
tmplinshi
Posts: 1604
Joined: 01 Oct 2013, 14:57

Re: 时间同步

12 Jan 2014, 13:19

wzxiaodu wrote:太好了,学习了。。
不过,win7系统下
开头最好加
if not A_IsAdmin
{
Run *RunAs "%A_AhkPath%" "%A_ScriptFullPath%"
ExitApp
}
谢谢建议。话说 WIN7 下非管理员权限真的运行不了这个脚本? :shock:
abs
Posts: 15
Joined: 06 Jan 2014, 01:28

Re: 时间同步

12 Jan 2014, 20:25

太棒了,原来RUN里面有HIDE这个参数,以前一直不知道命令行窗口可以隐藏。。。汗。。

另外URLDOWLOADTOVAR这个函数都被你搬过来了。。感谢!

Return to “脚本函数”

Who is online

Users browsing this forum: No registered users and 36 guests