FreeBSD/contrib/ntp/libntp/ntp_calendar.c
/*
 *---------------------------------------------------------------------
 * Convert a timestamp in NTP scale to a 64bit seconds value in the UN*X
 * scale with proper epoch unfolding around a given pivot or the current
 * system time. This function happily accepts negative pivot values as
 * timestamps befor 1970-01-01, so be aware of possible trouble on
 * platforms with 32bit 'time_t'!
 *
 * This is also a periodic extension, but since the cycle is 2^32 and
 * the shift is 2^31, we can do some *very* fast math without explicit
 * divisions.
 *---------------------------------------------------------------------
 */
Convert a timestamp in NTP scale to a 64bit seconds value in the UN*X
scale with proper epoch unfolding around a given pivot or the current
system time. This function happily accepts negative pivot values as
timestamps befor 1970-01-01, so be aware of possible trouble on
platforms with 32bit 'time_t'!

This is also a periodic extension, but since the cycle is 2^32 and
the shift is 2^31, we can do some *very* fast math without explicit
divisions.
Convert a timestamp in NTP scale to a 64bit seconds value in the UN * X scale with proper epoch unfolding around a given pivot or the current system time . This function happily accepts negative pivot values as timestamps befor 1970 - 01 - 01 so be aware of possible trouble on platforms with 32bit ' time_t ' ! This is also a periodic extension but since the cycle is 2^32 and the shift is 2^31 we can do some * very * fast math without explicit divisions .
vint64
ntpcal_ntp_to_time(
	uint32_t	ntp,
	const time_t *	pivot
	)
{
	vint64 res;
#   if defined(HAVE_INT64)
	res.q_s = (pivot != NULL)
		      ? *pivot
		      : now();
	res.Q_s -= 0x80000000u;		
	ntp	-= (uint32_t)JAN_1970;	
	ntp	-= res.D_s.lo;		
	res.Q_s += (uint64_t)ntp;	
#   else 
	time_t tmp;
	tmp = (pivot != NULL)
		  ? *pivot
		  : now();
	res = time_to_vint64(&tmp);
	M_SUB(res.D_s.hi, res.D_s.lo, 0, 0x80000000u);
	ntp -= (uint32_t)JAN_1970;	
	ntp -= res.D_s.lo;		
	M_ADD(res.D_s.hi, res.D_s.lo, 0, ntp);
#   endif 
	return res;
}
vint64 ntpcal_ntp_to_time ( uint32_t ntp , const time_t * pivot ) { vint64 res ; #   if defined ( HAVE_INT64 ) res . q_s = ( pivot != NULL ) ? * pivot : now ( ) ; res . Q_s -= 0x80000000u ; ntp -= ( uint32_t ) JAN_1970 ; ntp -= res . D_s . lo ; res . Q_s += ( uint64_t ) ntp ; #   else time_t tmp ; tmp = ( pivot != NULL ) ? * pivot : now ( ) ; res = time_to_vint64 ( & tmp ) ; M_SUB ( res . D_s . hi , res . D_s . lo , 0 , 0x80000000u ) ; ntp -= ( uint32_t ) JAN_1970 ; ntp -= res . D_s . lo ; M_ADD ( res . D_s . hi , res . D_s . lo , 0 , ntp ) ; #   endif return res ; }
Convert a timestamp in NTP scale to a 64bit seconds value in the UN*X
 scale with proper epoch unfolding around a given pivot or the current
 system time.
Convert a timestamp in NTP scale to a 64bit seconds value in the UN * X scale with proper epoch unfolding around a given pivot or the current system time .
/* unshift of half range */ /* warp into UN*X domain */ /* cycle difference	 */ /* get expanded time	 */ /* no 64bit scalars */ /* warp into UN*X domain */ /* cycle difference	 */ /* no 64bit scalars */