2 * clocktime - compute the NTP date from a day of year, hour, minute
6 #include "ntp_unixtime.h"
7 #include "ntp_stdlib.h"
10 * Hacks to avoid excercising the multiplier. I have no pride.
12 #define MULBY10(x) (((x)<<3) + ((x)<<1))
13 #define MULBY60(x) (((x)<<6) - ((x)<<2)) /* watch overflow */
14 #define MULBY24(x) (((x)<<4) + ((x)<<3))
17 * Two days, in seconds.
19 #define TWODAYS (2*24*60*60)
22 * We demand that the time be within CLOSETIME seconds of the receive
23 * time stamp. This is about 4 hours, which hopefully should be
24 * wide enough to collect most data, while close enough to keep things
25 * from getting confused.
27 #define CLOSETIME (4*60*60)
47 * Compute the offset into the year in seconds. Note that
48 * this could come out to be a negative number.
50 tmp = (long)(MULBY24((yday-1)) + hour + tzoff);
51 tmp = MULBY60(tmp) + (long)minute;
52 tmp = MULBY60(tmp) + (long)second;
55 * Initialize yearstart, if necessary.
59 yst = calyearstart(rec_ui);
64 * Now the fun begins. We demand that the received clock time
65 * be within CLOSETIME of the receive timestamp, but
66 * there is uncertainty about the year the timestamp is in.
67 * Use the current year start for the first check, this should
68 * work most of the time.
70 date = (u_long)(tmp + (long)yst);
71 if (date < (rec_ui + CLOSETIME) &&
72 date > (rec_ui - CLOSETIME)) {
78 * Trouble. Next check is to see if the year rolled over and, if
79 * so, try again with the new year's start.
81 yst = calyearstart(rec_ui);
82 if (yst != *yearstart) {
83 date = (u_long)((long)yst + tmp);
85 if (date < (rec_ui + CLOSETIME) &&
86 date > (rec_ui - CLOSETIME)) {
93 * Here we know the year start matches the current system
94 * time. One remaining possibility is that the time code
95 * is in the year previous to that of the system time. This
96 * is only worth checking if the receive timestamp is less
97 * than a couple of days into the new year.
99 if ((rec_ui - yst) < TWODAYS) {
100 yst = calyearstart(yst - TWODAYS);
101 if (yst != *yearstart) {
102 date = (u_long)(tmp + (long)yst);
103 if (date < (rec_ui + CLOSETIME) &&
104 date > (rec_ui - CLOSETIME)) {
113 * One last possibility is that the time stamp is in the year
114 * following the year the system is in. Try this one before
117 yst = calyearstart(rec_ui + TWODAYS);
118 if (yst != *yearstart) {
119 date = (u_long)((long)yst + tmp);
120 if (date < (rec_ui + CLOSETIME) &&
121 date > (rec_ui - CLOSETIME)) {