S2S_TimeSync.js


var counter=0;
var interval=7200; //retrive time every 2 hours

/*=====================================================================
A endless loop, call SyncTime() every 'interval' seconds.
=====================================================================*/
while(!Context.Service.GetStop())
{
	if(SyncTime())
	{
		counter=0;
	}
	else //if failed, try again after 1 minute
	{
		if(counter>=(interval-60))
		{
			counter=interval-60;
		}
	}
	
	while(!Context.Service.GetStop() && counter<interval)
	{
		Context.Sleep(500);
		counter+=0.5;
	}
}


/*=====================================================================
Function SyncTime():
Call GetTime() in 'GetNetTime.js' to get the internet time and adjusts
the computer clock with it.
=====================================================================*/

function SyncTime()
{
	
	var args=Context.Service.CreateFunctionArguments();
	args.Add(1000); //Tell GetTime() we can wait 1 second maxmum
	var tk0=Context.TickCount;
	var nt=Context.Service.CallFunction("GetNetTime.js", "GetTime", args);
	var tks=Context.TickCount-tk0;

	if(!nt || tks>1000)
	{
		return(false);
	}

	var st=Context.GetSystemTime();//the current time of the computer clock
	try{
		Context.SetSystemTime(nt);
		var log="System time updated from "+st.Format() + " to " + nt.Format() + "\n";
		Context.LogEvent(log, 0);
		return(true);
	}
	catch(e)
	{
		Context.LogEvent("Could not set system time.\n", 1);
		return(false);
	}
}