FreeBSD/contrib/ntp/ntpd/ntp_leapsec.c
/* [internal] Take a time stamp and create a leap second frame for
 * it. This will schedule a leap second for the beginning of the next
 * month, midnight UTC. The 'insert' argument tells if a leap second is
 * added (!=0) or removed (==0). We do not handle multiple inserts
 * (yet?)
 *
 * Returns 1 if the insert worked, 0 otherwise. (It's not possible to
 * insert a leap second into the current history -- only appending
 * towards the future is allowed!)
 */
[internal] Take a time stamp and create a leap second frame for
it. This will schedule a leap second for the beginning of the next
month, midnight UTC. The 'insert' argument tells if a leap second is
added (!=0) or removed (==0). We do not handle multiple inserts
(yet?)

Returns 1 if the insert worked, 0 otherwise. (It's not possible to
insert a leap second into the current history -- only appending
towards the future is allowed!)
[ internal ] Take a time stamp and create a leap second frame for it . This will schedule a leap second for the beginning of the next month midnight UTC . The ' insert ' argument tells if a leap second is added ( ! = 0 ) or removed ( == 0 ) . We do not handle multiple inserts ( yet? ) Returns 1 if the insert worked 0 otherwise . ( It ' s not possible to insert a leap second into the current history -- only appending towards the future is allowed! )
static int
leapsec_add(
	leap_table_t*  pt    ,
	const vint64 * now64 ,
	int            insert)
{
	vint64		ttime, starttime;
	struct calendar	fts;
	leap_info_t	li;
	if (   ucmpv64(now64, &pt->head.expire) < 0
	    || (pt->head.size && ucmpv64(now64, &pt->info[0].ttime) <= 0)) {
		errno = ERANGE;
		return FALSE;
	}
	ntpcal_ntp64_to_date(&fts, now64);
	if (fts.monthday == 1 && fts.hour == 0) {
		errno = EINVAL;
		return FALSE;
	}
	fts.monthday = 1;
	fts.hour     = 0;
	fts.minute   = 0;
	fts.second   = 0;
	starttime = ntpcal_date_to_ntp64(&fts);
	fts.month++;
	ttime = ntpcal_date_to_ntp64(&fts);
	li.ttime = ttime;
	li.stime = ttime.D_s.lo - starttime.D_s.lo;
	li.taiof = (pt->head.size ? pt->info[0].taiof : pt->head.base_tai)
	         + (insert ? 1 : -1);
	li.dynls = 1;
	return add_range(pt, &li);
}
static int leapsec_add ( leap_table_t * pt , const vint64 * now64 , int insert ) { vint64 ttime , starttime ; struct calendar fts ; leap_info_t li ; if ( ucmpv64 ( now64 , & pt -> head . expire ) < 0 || ( pt -> head . size && ucmpv64 ( now64 , & pt -> info [ 0 ] . ttime ) <= 0 ) ) { errno = ERANGE ; return FALSE ; } ntpcal_ntp64_to_date ( & fts , now64 ) ; if ( fts . monthday == 1 && fts . hour == 0 ) { errno = EINVAL ; return FALSE ; } fts . monthday = 1 ; fts . hour = 0 ; fts . minute = 0 ; fts . second = 0 ; starttime = ntpcal_date_to_ntp64 ( & fts ) ; fts . month ++ ; ttime = ntpcal_date_to_ntp64 ( & fts ) ; li . ttime = ttime ; li . stime = ttime . D_s . lo - starttime . D_s . lo ; li . taiof = ( pt -> head . size ? pt -> info [ 0 ] . taiof : pt -> head . base_tai ) + ( insert ? 1 : -1 ) ; li . dynls = 1 ; return add_range ( pt , & li ) ; }
[internal] Take a time stamp and create a leap second frame for
 it.
[ internal ] Take a time stamp and create a leap second frame for it .
/*BOOL*/ /* Check against the table expiration and the latest available
	 * leap entry. Do not permit inserts, only appends, and only if
	 * the extend the table beyond the expiration!
	 */ /* To guard against dangling leap flags: do not accept leap
	 * second request on the 1st hour of the 1st day of the month.
	 */ /* Ok, do the remaining calculations */