Change the default for ntpd back to -s, the bug which triggered this
[dragonfly.git] / contrib / ntp / libntp / calleapwhen.c
1 /*
2  * calleapwhen - determine the number of seconds to the next possible
3  *               leap occurance.
4  */
5 #include <sys/types.h>
6
7 #include "ntp_types.h"
8 #include "ntp_calendar.h"
9 #include "ntp_stdlib.h"
10
11 /*
12  * calleaptab - leaps occur at the end of December and June
13  */
14 long calleaptab[10] = {
15         -(JAN+FEBLEAP)*SECSPERDAY,      /* leap previous to cycle */
16         (MAR+APR+MAY+JUN)*SECSPERDAY,   /* end of June */
17         (MAR+APR+MAY+JUN+JUL+AUG+SEP+OCT+NOV+DEC)*SECSPERDAY, /* end of Dec */
18         (MAR+APR+MAY+JUN)*SECSPERDAY + SECSPERYEAR,
19         (MAR+APR+MAY+JUN+JUL+AUG+SEP+OCT+NOV+DEC)*SECSPERDAY + SECSPERYEAR,
20         (MAR+APR+MAY+JUN)*SECSPERDAY + 2*SECSPERYEAR,
21         (MAR+APR+MAY+JUN+JUL+AUG+SEP+OCT+NOV+DEC)*SECSPERDAY + 2*SECSPERYEAR,
22         (MAR+APR+MAY+JUN)*SECSPERDAY + 3*SECSPERYEAR,
23         (MAR+APR+MAY+JUN+JUL+AUG+SEP+OCT+NOV+DEC)*SECSPERDAY + 3*SECSPERYEAR,
24         (MAR+APR+MAY+JUN+JUL+AUG+SEP+OCT+NOV+DEC+JAN+FEBLEAP+MAR+APR+MAY+JUN)
25         *SECSPERDAY + 3*SECSPERYEAR,    /* next after current cycle */
26 };
27
28 u_long
29 calleapwhen(
30         u_long ntpdate
31         )
32 {
33         register u_long dateincycle;
34         register int i;
35
36         /*
37          * Find the offset from the start of the cycle
38          */
39         dateincycle = ntpdate;
40         if (dateincycle >= MAR1988)
41             dateincycle -= MAR1988;
42         else
43             dateincycle -= MAR1900;
44
45         while (dateincycle >= SECSPERCYCLE)
46             dateincycle -= SECSPERCYCLE;
47
48         /*
49          * Find where we are with respect to the leap events.
50          */
51         for (i = 1; i < 9; i++)
52             if (dateincycle < (u_long)calleaptab[i])
53                 break;
54         
55         /*
56          * i points at the next leap.  Compute the last and the next.
57          */
58         return (u_long)(calleaptab[i] - (long)dateincycle);
59 }