FreeBSD/contrib/ntp/libntp/ntp_calendar.c
/*
 *---------------------------------------------------------------------
 * Convert a timestamp in NTP scale to a 64bit seconds value in the NTP
 * scale with proper epoch unfolding around a given pivot or the current
 * system time.
 *
 * Note: The pivot must be given in the UN*X time domain!
 *
 * 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 NTP
scale with proper epoch unfolding around a given pivot or the current
system time.

The pivot must be given in the UN*X time domain!

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 NTP scale with proper epoch unfolding around a given pivot or the current system time . The pivot must be given in the UN * X time domain! 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_ntp(
	uint32_t      ntp,
	const time_t *pivot
	)
{
	vint64 res;
#   if defined(HAVE_INT64)
	res.q_s = (pivot)
		      ? *pivot
		      : now();
	res.Q_s -= 0x80000000u;		
	res.Q_s += (uint32_t)JAN_1970;	
	ntp	-= res.D_s.lo;		
	res.Q_s += (uint64_t)ntp;	
#   else 
	time_t tmp;
	tmp = (pivot)
		  ? *pivot
		  : now();
	res = time_to_vint64(&tmp);
	M_SUB(res.D_s.hi, res.D_s.lo, 0, 0x80000000u);
	M_ADD(res.D_s.hi, res.D_s.lo, 0, (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_ntp ( uint32_t ntp , const time_t * pivot ) { vint64 res ; #   if defined ( HAVE_INT64 ) res . q_s = ( pivot ) ? * pivot : now ( ) ; res . Q_s -= 0x80000000u ; res . Q_s += ( uint32_t ) JAN_1970 ; ntp -= res . D_s . lo ; res . Q_s += ( uint64_t ) ntp ; #   else time_t tmp ; tmp = ( pivot ) ? * pivot : now ( ) ; res = time_to_vint64 ( & tmp ) ; M_SUB ( res . D_s . hi , res . D_s . lo , 0 , 0x80000000u ) ; M_ADD ( res . D_s . hi , res . D_s . lo , 0 , ( 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 NTP
 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 NTP scale with proper epoch unfolding around a given pivot or the current system time .
/* unshift of half range */ /* warp into NTP domain	 */ /* cycle difference	 */ /* get expanded time	 */ /* no 64bit scalars */ /*into NTP */ /* cycle difference	 */ /* no 64bit scalars */