
/*
The Time based on RFC 868

The time is the number of seconds since 00:00 (midnight) 1 January 1900
GMT, such that the time 1 is 12:00:01 am on 1 January 1900 GMT; this
base will serve until the year 2036.

For example:

   the time  2,208,988,800 corresponds to 00:00  1 Jan 1970 GMT,

             2,398,291,200 corresponds to 00:00  1 Jan 1976 GMT,

             2,524,521,600 corresponds to 00:00  1 Jan 1980 GMT,

             2,629,584,000 corresponds to 00:00  1 May 1983 GMT,

        and -1,297,728,000 corresponds to 00:00 17 Nov 1858 GMT.

*/

struct time_protocol {
	unsigned long secs;	
	unsigned int mSecs;	//while not part of the time protocol I have included it in this 
						//structure for convenience. On a time sync we will copy the secs
						//value from the appropriate CAN message and simply 0 the mSecs
						//value, in this manner we can have a mSec accurate clock time 
						//without the hassle of sending the exact mSec value to the device.
};





unsigned long funSystemTimeSync(bit mandatory_update, unsigned long syncseconds){

	if (mandatory_update == TRUE){
		synctime.secs = syncseconds;
		synctime.mSecs = 0;		//simply reset the mSecs
		return 0;				//indicates successful update
	}

	if (syncseconds >= synctime.secs){
		synctime.secs = syncseconds;
		synctime.mSecs = 0;		//simply reset the mSecs
		return 0;				//indicates successful update
	}
	else 
		synctime.mSecs = 0;		//simply reset the mSecs - there could be an error here where 
								//we reset our mSec and the message is queued or takes x-mSecs
								//to be processed by the other nodes (this is acceptable in our app)
		return synctime.secs;	//we are greater than the proposed time 
								//return our time to update rest of network

}
 