154359Sroberto/*
254359Sroberto * clocktime - compute the NTP date from a day of year, hour, minute
354359Sroberto *	       and second.
454359Sroberto */
5290000Sglebius#include <config.h>
654359Sroberto#include "ntp_fp.h"
754359Sroberto#include "ntp_unixtime.h"
854359Sroberto#include "ntp_stdlib.h"
9290000Sglebius#include "ntp_calendar.h"
1054359Sroberto
1154359Sroberto/*
12290000Sglebius * We check that the time be within CLOSETIME seconds of the receive
13290000Sglebius * time stamp.	This is about 4 hours, which hopefully should be wide
14290000Sglebius * enough to collect most data, while close enough to keep things from
15290000Sglebius * getting confused.
1654359Sroberto */
17290000Sglebius#define	CLOSETIME	(4u*60u*60u)
1854359Sroberto
1954359Sroberto/*
20290000Sglebius * Since we try to match years, the result of a full search will not
21290000Sglebius * change when we are already less than a half year from the receive
22290000Sglebius * time stamp.	Since the length of a year is variable we use a
23290000Sglebius * slightly narrower limit; this might require a full evaluation near
24290000Sglebius * the edge, but will make sure we always get the correct result.
2554359Sroberto */
26290000Sglebius#define NEARTIME	(182u * SECSPERDAY)
2754359Sroberto
2854359Sroberto/*
29290000Sglebius * local calendar helpers
3054359Sroberto */
31290000Sglebiusstatic int32   ntp_to_year(u_int32);
32290000Sglebiusstatic u_int32 year_to_ntp(int32);
3354359Sroberto
34290000Sglebius/*
35290000Sglebius * Take a time spec given as day-of-year, hour, minute and second as
36290000Sglebius * well as a GMT offset in hours and convert it to a NTP time stamp in
37290000Sglebius * '*ts_ui'. The value will be in the range (rec_ui-0.5yrs) to
38290000Sglebius * (rec_ui+0.5yrs). A hint for the current start-of-year will be
39290000Sglebius * read from '*yearstart'.
40290000Sglebius *
41290000Sglebius * On return '*ts_ui' will always the best matching solution, and
42290000Sglebius * '*yearstart' will receive the associated start-of-year.
43290000Sglebius *
44290000Sglebius * The function will tell if the result in 'ts_ui' is in CLOSETIME
45290000Sglebius * (+/-4hrs) around the receive time by returning a non-zero value.
46290000Sglebius *
47290000Sglebius * Note: The function puts no constraints on the value ranges for the
48290000Sglebius * time specification, but evaluates the effective seconds in
49290000Sglebius * 32-bit arithmetic.
50290000Sglebius */
5154359Srobertoint
5254359Srobertoclocktime(
53290000Sglebius	int	yday	 ,	/* day-of-year */
54290000Sglebius	int	hour	 ,	/* hour of day */
55290000Sglebius	int	minute	 ,	/* minute of hour */
56290000Sglebius	int	second	 ,	/* second of minute */
57290000Sglebius	int	tzoff	 ,	/* hours west of GMT */
58290000Sglebius	u_int32 rec_ui	 ,	/* pivot value */
59290000Sglebius	u_long *yearstart,	/* cached start-of-year, should be fixed to u_int32 */
60290000Sglebius	u_int32 *ts_ui	 )	/* effective time stamp */
6154359Sroberto{
62290000Sglebius	u_int32 ystt[3];	/* year start */
63290000Sglebius	u_int32 test[3];	/* result time stamp */
64290000Sglebius	u_int32 diff[3];	/* abs difference to receive */
65290000Sglebius	int32 y, tmp, idx, min;
66290000Sglebius
6754359Sroberto	/*
68290000Sglebius	 * Compute the offset into the year in seconds.	 Note that
6954359Sroberto	 * this could come out to be a negative number.
7054359Sroberto	 */
71290000Sglebius	tmp = ((int32)second +
72290000Sglebius	       SECSPERMIN * ((int32)minute +
73290000Sglebius			     MINSPERHR * ((int32)hour + (int32)tzoff +
74290000Sglebius					  HRSPERDAY * ((int32)yday - 1))));
7554359Sroberto	/*
76290000Sglebius	 * Based on the cached year start, do a first attempt. Be
77290000Sglebius	 * happy and return if this gets us better than NEARTIME to
78290000Sglebius	 * the receive time stamp. Do this only if the cached year
79290000Sglebius	 * start is not zero, which will not happen after 1900 for the
80290000Sglebius	 * next few thousand years.
8154359Sroberto	 */
82290000Sglebius	if (*yearstart) {
83290000Sglebius		/* -- get time stamp of potential solution */
84290000Sglebius		test[0] = (u_int32)(*yearstart) + tmp;
85290000Sglebius		/* -- calc absolute difference to receive time */
86290000Sglebius		diff[0] = test[0] - rec_ui;
87290000Sglebius		if (diff[0] >= 0x80000000u)
88290000Sglebius			diff[0] = ~diff[0] + 1;
89290000Sglebius		/* -- can't get closer if diff < NEARTIME */
90290000Sglebius		if (diff[0] < NEARTIME) {
91290000Sglebius			*ts_ui = test[0];
92290000Sglebius			return diff[0] < CLOSETIME;
93290000Sglebius		}
9454359Sroberto	}
9554359Sroberto
9654359Sroberto	/*
97290000Sglebius	 * Now the dance begins. Based on the receive time stamp and
98290000Sglebius	 * the seconds offset in 'tmp', we make an educated guess
99290000Sglebius	 * about the year to start with. This takes us on the spot
100290000Sglebius	 * with a fuzz of +/-1 year.
101290000Sglebius	 *
102290000Sglebius	 * We calculate the effective timestamps for the three years
103290000Sglebius	 * around the guess and select the entry with the minimum
104290000Sglebius	 * absolute difference to the receive time stamp.
10554359Sroberto	 */
106290000Sglebius	y = ntp_to_year(rec_ui - tmp);
107290000Sglebius	for (idx = 0; idx < 3; idx++) {
108290000Sglebius		/* -- get year start of potential solution */
109290000Sglebius		ystt[idx] = year_to_ntp(y + idx - 1);
110290000Sglebius		/* -- get time stamp of potential solution */
111290000Sglebius		test[idx] = ystt[idx] + tmp;
112290000Sglebius		/* -- calc absolute difference to receive time */
113290000Sglebius		diff[idx] = test[idx] - rec_ui;
114290000Sglebius		if (diff[idx] >= 0x80000000u)
115290000Sglebius			diff[idx] = ~diff[idx] + 1;
11654359Sroberto	}
117290000Sglebius	/* -*- assume current year fits best, then search best fit */
118290000Sglebius	for (min = 1, idx = 0; idx < 3; idx++)
119290000Sglebius		if (diff[idx] < diff[min])
120290000Sglebius			min = idx;
121290000Sglebius	/* -*- store results and update year start */
122290000Sglebius	*ts_ui	   = test[min];
123290000Sglebius	*yearstart = ystt[min];
12454359Sroberto
125290000Sglebius	/* -*- tell if we could get into CLOSETIME*/
126290000Sglebius	return diff[min] < CLOSETIME;
127290000Sglebius}
12854359Sroberto
129290000Sglebiusstatic int32
130290000Sglebiusntp_to_year(
131290000Sglebius	u_int32 ntp)
132290000Sglebius{
133290000Sglebius	vint64	     t;
134290000Sglebius	ntpcal_split s;
13554359Sroberto
136290000Sglebius	t = ntpcal_ntp_to_ntp(ntp, NULL);
137290000Sglebius	s = ntpcal_daysplit(&t);
138290000Sglebius	s = ntpcal_split_eradays(s.hi + DAY_NTP_STARTS - 1, NULL);
139290000Sglebius	return s.hi + 1;
140290000Sglebius}
14154359Sroberto
142290000Sglebiusstatic u_int32
143290000Sglebiusyear_to_ntp(
144290000Sglebius	int32 year)
145290000Sglebius{
146290000Sglebius	u_int32 days;
147290000Sglebius	days = ntpcal_days_in_years(year-1) - DAY_NTP_STARTS + 1;
148290000Sglebius	return days * SECSPERDAY;
14954359Sroberto}
150