154359Sroberto/*
254359Sroberto * clocktime - compute the NTP date from a day of year, hour, minute
354359Sroberto *	       and second.
454359Sroberto */
5280849Scy#include <config.h>
654359Sroberto#include "ntp_fp.h"
754359Sroberto#include "ntp_unixtime.h"
854359Sroberto#include "ntp_stdlib.h"
9280849Scy#include "ntp_calendar.h"
1054359Sroberto
1154359Sroberto/*
12280849Scy * We check that the time be within CLOSETIME seconds of the receive
13280849Scy * time stamp.	This is about 4 hours, which hopefully should be wide
14280849Scy * enough to collect most data, while close enough to keep things from
15280849Scy * getting confused.
1654359Sroberto */
17280849Scy#define	CLOSETIME	(4u*60u*60u)
1854359Sroberto
1954359Sroberto/*
20280849Scy * Since we try to match years, the result of a full search will not
21280849Scy * change when we are already less than a half year from the receive
22280849Scy * time stamp.	Since the length of a year is variable we use a
23280849Scy * slightly narrower limit; this might require a full evaluation near
24280849Scy * the edge, but will make sure we always get the correct result.
2554359Sroberto */
26280849Scy#define NEARTIME	(182u * SECSPERDAY)
2754359Sroberto
2854359Sroberto/*
29280849Scy * local calendar helpers
3054359Sroberto */
31280849Scystatic int32   ntp_to_year(u_int32);
32280849Scystatic u_int32 year_to_ntp(int32);
3354359Sroberto
34280849Scy/*
35280849Scy * Take a time spec given as day-of-year, hour, minute and second as
36280849Scy * well as a GMT offset in hours and convert it to a NTP time stamp in
37280849Scy * '*ts_ui'. The value will be in the range (rec_ui-0.5yrs) to
38280849Scy * (rec_ui+0.5yrs). A hint for the current start-of-year will be
39280849Scy * read from '*yearstart'.
40280849Scy *
41280849Scy * On return '*ts_ui' will always the best matching solution, and
42280849Scy * '*yearstart' will receive the associated start-of-year.
43280849Scy *
44280849Scy * The function will tell if the result in 'ts_ui' is in CLOSETIME
45280849Scy * (+/-4hrs) around the receive time by returning a non-zero value.
46280849Scy *
47280849Scy * Note: The function puts no constraints on the value ranges for the
48280849Scy * time specification, but evaluates the effective seconds in
49280849Scy * 32-bit arithmetic.
50280849Scy */
5154359Srobertoint
5254359Srobertoclocktime(
53280849Scy	int	yday	 ,	/* day-of-year */
54280849Scy	int	hour	 ,	/* hour of day */
55280849Scy	int	minute	 ,	/* minute of hour */
56280849Scy	int	second	 ,	/* second of minute */
57280849Scy	int	tzoff	 ,	/* hours west of GMT */
58280849Scy	u_int32 rec_ui	 ,	/* pivot value */
59280849Scy	u_long *yearstart,	/* cached start-of-year, should be fixed to u_int32 */
60280849Scy	u_int32 *ts_ui	 )	/* effective time stamp */
6154359Sroberto{
62280849Scy	u_int32 ystt[3];	/* year start */
63280849Scy	u_int32 test[3];	/* result time stamp */
64280849Scy	u_int32 diff[3];	/* abs difference to receive */
65280849Scy	int32 y, tmp, idx, min;
66280849Scy
6754359Sroberto	/*
68280849Scy	 * Compute the offset into the year in seconds.	 Note that
6954359Sroberto	 * this could come out to be a negative number.
7054359Sroberto	 */
71280849Scy	tmp = ((int32)second +
72280849Scy	       SECSPERMIN * ((int32)minute +
73280849Scy			     MINSPERHR * ((int32)hour + (int32)tzoff +
74280849Scy					  HRSPERDAY * ((int32)yday - 1))));
7554359Sroberto	/*
76280849Scy	 * Based on the cached year start, do a first attempt. Be
77280849Scy	 * happy and return if this gets us better than NEARTIME to
78280849Scy	 * the receive time stamp. Do this only if the cached year
79280849Scy	 * start is not zero, which will not happen after 1900 for the
80280849Scy	 * next few thousand years.
8154359Sroberto	 */
82280849Scy	if (*yearstart) {
83280849Scy		/* -- get time stamp of potential solution */
84280849Scy		test[0] = (u_int32)(*yearstart) + tmp;
85280849Scy		/* -- calc absolute difference to receive time */
86280849Scy		diff[0] = test[0] - rec_ui;
87280849Scy		if (diff[0] >= 0x80000000u)
88280849Scy			diff[0] = ~diff[0] + 1;
89280849Scy		/* -- can't get closer if diff < NEARTIME */
90280849Scy		if (diff[0] < NEARTIME) {
91280849Scy			*ts_ui = test[0];
92280849Scy			return diff[0] < CLOSETIME;
93280849Scy		}
9454359Sroberto	}
9554359Sroberto
9654359Sroberto	/*
97280849Scy	 * Now the dance begins. Based on the receive time stamp and
98280849Scy	 * the seconds offset in 'tmp', we make an educated guess
99280849Scy	 * about the year to start with. This takes us on the spot
100280849Scy	 * with a fuzz of +/-1 year.
101280849Scy	 *
102280849Scy	 * We calculate the effective timestamps for the three years
103280849Scy	 * around the guess and select the entry with the minimum
104280849Scy	 * absolute difference to the receive time stamp.
10554359Sroberto	 */
106280849Scy	y = ntp_to_year(rec_ui - tmp);
107280849Scy	for (idx = 0; idx < 3; idx++) {
108280849Scy		/* -- get year start of potential solution */
109280849Scy		ystt[idx] = year_to_ntp(y + idx - 1);
110280849Scy		/* -- get time stamp of potential solution */
111280849Scy		test[idx] = ystt[idx] + tmp;
112280849Scy		/* -- calc absolute difference to receive time */
113280849Scy		diff[idx] = test[idx] - rec_ui;
114280849Scy		if (diff[idx] >= 0x80000000u)
115280849Scy			diff[idx] = ~diff[idx] + 1;
11654359Sroberto	}
117280849Scy	/* -*- assume current year fits best, then search best fit */
118280849Scy	for (min = 1, idx = 0; idx < 3; idx++)
119280849Scy		if (diff[idx] < diff[min])
120280849Scy			min = idx;
121280849Scy	/* -*- store results and update year start */
122280849Scy	*ts_ui	   = test[min];
123280849Scy	*yearstart = ystt[min];
12454359Sroberto
125280849Scy	/* -*- tell if we could get into CLOSETIME*/
126280849Scy	return diff[min] < CLOSETIME;
127280849Scy}
12854359Sroberto
129280849Scystatic int32
130280849Scyntp_to_year(
131280849Scy	u_int32 ntp)
132280849Scy{
133280849Scy	vint64	     t;
134280849Scy	ntpcal_split s;
13554359Sroberto
136280849Scy	t = ntpcal_ntp_to_ntp(ntp, NULL);
137280849Scy	s = ntpcal_daysplit(&t);
138280849Scy	s = ntpcal_split_eradays(s.hi + DAY_NTP_STARTS - 1, NULL);
139280849Scy	return s.hi + 1;
140280849Scy}
14154359Sroberto
142280849Scystatic u_int32
143280849Scyyear_to_ntp(
144280849Scy	int32 year)
145280849Scy{
146280849Scy	u_int32 days;
147280849Scy	days = ntpcal_days_in_years(year-1) - DAY_NTP_STARTS + 1;
148280849Scy	return days * SECSPERDAY;
14954359Sroberto}
150