NTP设置系统时钟

admin2021-10-28  2.0K+

//RUNAS//
import win.ui;
/*DSG{{*/
var winform = win.form(text="aardio form";right=295;bottom=245)
winform.add(
button={cls="button";text="写入系统时间";left=165;top=189;right=273;bottom=228;z=3};
button3={cls="button";text="NTP更新";left=29;top=189;right=137;bottom=228;z=4};
static={cls="static";left=115;top=61;right=228;bottom=87;notify=1;transparent=1;z=1};
static2={cls="static";left=115;top=112;right=228;bottom=139;notify=1;transparent=1;z=2};
static3={cls="static";text="系统时间";left=29;top=62;right=106;bottom=87;transparent=1;z=5};
static4={cls="static";text="NTP时间";left=29;top=114;right=106;bottom=139;transparent=1;z=6}
)
/*}}*/

import thread;
import time;
import console;
/*
var getNtpTime = function(){
    try {
    	var data, err = time.ntp.getData("time1.a2liyun.com", 123, )
    }
    catch(e) {
        console.log( e )
    }
    console.dump(t);
    data.time
}
getNtpTime();
*/

// ntp时间与系统时间的差值 ntp时间 - 系统时间
winform.diffSecond = 0;
winform.updateFlag = false;
// 用来显示系统时间的线程
thread.invoke(
        function(winform){
            while(true) {
                var tm = time.now();
            	tm.format = "%H:%M:%S";
            	tm.locale = "chs";
                //winform.edit.print( time.tick(),"线程ID:",thread.getId() );
                winform.static.text = tostring(tm);
                sleep(200);
            }
    },winform
)
// 用来显示ntp时间的线程
thread.invoke(
        function(winform){
            while(true) {
                var tm = time.now();
                tm.addsecond(winform.diffSecond)
            	tm.format = "%H:%M:%S";
            	tm.locale = "chs";
                //winform.edit.print( time.tick(),"线程ID:",thread.getId() );
                winform.static2.text = tostring(tm);
                sleep(200);
            }
    },winform
)

var getDiffSecond = function(winform){
    var data, err = nil;
    try {
        //获取ntp时间
		import time.ntp;
    	data, err = time.ntp.getData("time1.aliyun.com", 123, )
    }
    catch(e) {
        //console.log( e )
        winform.diffSecond = 0;
        winform.msgbox("更新失败");
        return;
    }
    //console.dump(type(data.time));
    if(data.time) {
        var tm = time.now();
        winform.diffSecond = tonumber(data.time) - tonumber(tm);
        winform.msgbox("更新成功");
        return;
    }
}

winform.button.oncommand = function(id,event){
	//console.log( winform.diffSecond );
	var tm = time.now();
    tm.addsecond(winform.diffSecond);
    //tm.locale = "chs";
	
	if(tm){
		console.log( winform.diffSecond );
		::Kernel32.SetSystemTime(tm.utc());
	}
	
}

winform.button3.oncommand = function(id,event){
	thread.invoke(getDiffSecond, winform);
}

thread.invoke(getDiffSecond, winform);

winform.show();
win.loopMessage();
转载请注明原文地址: https://aardio.net/read-253.html
最新回复(0)
aardio问答
aardio编程语言