ntp_calendar.c revision 1.4.10.2 1 /* $NetBSD: ntp_calendar.c,v 1.4.10.2 2015/01/07 10:10:13 msaitoh Exp $ */
2
3 /*
4 * ntp_calendar.c - calendar and helper functions
5 *
6 * Written by Juergen Perlinger (perlinger (at) ntp.org) for the NTP project.
7 * The contents of 'html/copyright.html' apply.
8 */
9 #include <config.h>
10 #include <sys/types.h>
11
12 #include "ntp_types.h"
13 #include "ntp_calendar.h"
14 #include "ntp_stdlib.h"
15 #include "ntp_fp.h"
16 #include "ntp_unixtime.h"
17
18 /*
19 *---------------------------------------------------------------------
20 * replacing the 'time()' function
21 * --------------------------------------------------------------------
22 */
23
24 static systime_func_ptr systime_func = &time;
25 static inline time_t now(void);
26
27
28 systime_func_ptr
29 ntpcal_set_timefunc(
30 systime_func_ptr nfunc
31 )
32 {
33 systime_func_ptr res;
34
35 res = systime_func;
36 if (NULL == nfunc)
37 nfunc = &time;
38 systime_func = nfunc;
39
40 return res;
41 }
42
43
44 static inline time_t
45 now(void)
46 {
47 return (*systime_func)(NULL);
48 }
49
50 /*
51 *---------------------------------------------------------------------
52 * Convert between 'time_t' and 'vint64'
53 *---------------------------------------------------------------------
54 */
55 vint64
56 time_to_vint64(
57 const time_t * ptt
58 )
59 {
60 vint64 res;
61 time_t tt;
62
63 tt = *ptt;
64
65 #if SIZEOF_TIME_T <= 4
66
67 res.D_s.hi = 0;
68 if (tt < 0) {
69 res.D_s.lo = (uint32_t)-tt;
70 M_NEG(res.D_s.hi, res.D_s.lo);
71 } else {
72 res.D_s.lo = (uint32_t)tt;
73 }
74
75 #elif defined(HAVE_INT64)
76
77 res.q_s = tt;
78
79 #else
80 /*
81 * shifting negative signed quantities is compiler-dependent, so
82 * we better avoid it and do it all manually. And shifting more
83 * than the width of a quantity is undefined. Also a don't do!
84 */
85 if (tt < 0) {
86 tt = -tt;
87 res.D_s.lo = (uint32_t)tt;
88 res.D_s.hi = (uint32_t)(tt >> 32);
89 M_NEG(res.D_s.hi, res.D_s.lo);
90 } else {
91 res.D_s.lo = (uint32_t)tt;
92 res.D_s.hi = (uint32_t)(tt >> 32);
93 }
94
95 #endif
96
97 return res;
98 }
99
100
101 time_t
102 vint64_to_time(
103 const vint64 *tv
104 )
105 {
106 time_t res;
107
108 #if SIZEOF_TIME_T <= 4
109
110 res = (time_t)tv->D_s.lo;
111
112 #elif defined(HAVE_INT64)
113
114 res = (time_t)tv->q_s;
115
116 #else
117
118 res = ((time_t)tv->d_s.hi << 32) | tv->D_s.lo;
119
120 #endif
121
122 return res;
123 }
124
125 /*
126 *---------------------------------------------------------------------
127 * Get the build date & time
128 *---------------------------------------------------------------------
129 */
130 int
131 ntpcal_get_build_date(
132 struct calendar * jd
133 )
134 {
135 /* The C standard tells us the format of '__DATE__':
136 *
137 * __DATE__ The date of translation of the preprocessing
138 * translation unit: a character string literal of the form "Mmm
139 * dd yyyy", where the names of the months are the same as those
140 * generated by the asctime function, and the first character of
141 * dd is a space character if the value is less than 10. If the
142 * date of translation is not available, an
143 * implementation-defined valid date shall be supplied.
144 *
145 * __TIME__ The time of translation of the preprocessing
146 * translation unit: a character string literal of the form
147 * "hh:mm:ss" as in the time generated by the asctime
148 * function. If the time of translation is not available, an
149 * implementation-defined valid time shall be supplied.
150 *
151 * Note that MSVC declares DATE and TIME to be in the local time
152 * zone, while neither the C standard nor the GCC docs make any
153 * statement about this. As a result, we may be +/-12hrs off
154 * UTC. But for practical purposes, this should not be a
155 * problem.
156 *
157 */
158 #ifdef MKREPRO_DATE
159 static const char build[] = MKREPRO_TIME "/" MKREPRO_DATE;
160 #else
161 static const char build[] = __TIME__ "/" __DATE__;
162 #endif
163 static const char mlist[] = "JanFebMarAprMayJunJulAugSepOctNovDec";
164
165 char monstr[4];
166 const char * cp;
167 unsigned short hour, minute, second, day, year;
168 /* Note: The above quantities are used for sscanf 'hu' format,
169 * so using 'uint16_t' is contra-indicated!
170 */
171
172 #ifdef DEBUG
173 static int ignore = 0;
174 #endif
175
176 ZERO(*jd);
177 jd->year = 1970;
178 jd->month = 1;
179 jd->monthday = 1;
180
181 #ifdef DEBUG
182 /* check environment if build date should be ignored */
183 if (0 == ignore) {
184 const char * envstr;
185 envstr = getenv("NTPD_IGNORE_BUILD_DATE");
186 ignore = 1 + (envstr && (!*envstr || !strcasecmp(envstr, "yes")));
187 }
188 if (ignore > 1)
189 return FALSE;
190 #endif
191
192 if (6 == sscanf(build, "%hu:%hu:%hu/%3s %hu %hu",
193 &hour, &minute, &second, monstr, &day, &year)) {
194 cp = strstr(mlist, monstr);
195 if (NULL != cp) {
196 jd->year = year;
197 jd->month = (uint8_t)((cp - mlist) / 3 + 1);
198 jd->monthday = (uint8_t)day;
199 jd->hour = (uint8_t)hour;
200 jd->minute = (uint8_t)minute;
201 jd->second = (uint8_t)second;
202
203 return TRUE;
204 }
205 }
206
207 return FALSE;
208 }
209
210
211 /*
212 *---------------------------------------------------------------------
213 * basic calendar stuff
214 * --------------------------------------------------------------------
215 */
216
217 /* month table for a year starting with March,1st */
218 static const uint16_t shift_month_table[13] = {
219 0, 31, 61, 92, 122, 153, 184, 214, 245, 275, 306, 337, 366
220 };
221
222 /* month tables for years starting with January,1st; regular & leap */
223 static const uint16_t real_month_table[2][13] = {
224 /* -*- table for regular years -*- */
225 { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 },
226 /* -*- table for leap years -*- */
227 { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 }
228 };
229
230 /*
231 * Some notes on the terminology:
232 *
233 * We use the proleptic Gregorian calendar, which is the Gregorian
234 * calendar extended in both directions ad infinitum. This totally
235 * disregards the fact that this calendar was invented in 1582, and
236 * was adopted at various dates over the world; sometimes even after
237 * the start of the NTP epoch.
238 *
239 * Normally date parts are given as current cycles, while time parts
240 * are given as elapsed cycles:
241 *
242 * 1970-01-01/03:04:05 means 'IN the 1970st. year, IN the first month,
243 * ON the first day, with 3hrs, 4minutes and 5 seconds elapsed.
244 *
245 * The basic calculations for this calendar implementation deal with
246 * ELAPSED date units, which is the number of full years, full months
247 * and full days before a date: 1970-01-01 would be (1969, 0, 0) in
248 * that notation.
249 *
250 * To ease the numeric computations, month and day values outside the
251 * normal range are acceptable: 2001-03-00 will be treated as the day
252 * before 2001-03-01, 2000-13-32 will give the same result as
253 * 2001-02-01 and so on.
254 *
255 * 'rd' or 'RD' is used as an abbreviation for the latin 'rata die'
256 * (day number). This is the number of days elapsed since 0000-12-31
257 * in the proleptic Gregorian calendar. The begin of the Christian Era
258 * (0001-01-01) is RD(1).
259 *
260 *
261 * Some notes on the implementation:
262 *
263 * Calendar algorithms thrive on the division operation, which is one of
264 * the slowest numerical operations in any CPU. What saves us here from
265 * abysmal performance is the fact that all divisions are divisions by
266 * constant numbers, and most compilers can do this by a multiplication
267 * operation. But this might not work when using the div/ldiv/lldiv
268 * function family, because many compilers are not able to do inline
269 * expansion of the code with following optimisation for the
270 * constant-divider case.
271 *
272 * Also div/ldiv/lldiv are defined in terms of int/long/longlong, which
273 * are inherently target dependent. Nothing that could not be cured with
274 * autoconf, but still a mess...
275 *
276 * Furthermore, we need floor division while C demands truncation to
277 * zero, so additional steps are required to make sure the algorithms
278 * work.
279 *
280 * For all this, all divisions by constant are coded manually, even when
281 * there is a joined div/mod operation: The optimiser should sort that
282 * out, if possible.
283 *
284 * Finally, the functions do not check for overflow conditions. This
285 * is a sacrifice made for execution speed; since a 32-bit day counter
286 * covers +/- 5,879,610 years, this should not pose a problem here.
287 */
288
289
290 /*
291 * ==================================================================
292 *
293 * General algorithmic stuff
294 *
295 * ==================================================================
296 */
297
298 /*
299 *---------------------------------------------------------------------
300 * Do a periodic extension of 'value' around 'pivot' with a period of
301 * 'cycle'.
302 *
303 * The result 'res' is a number that holds to the following properties:
304 *
305 * 1) res MOD cycle == value MOD cycle
306 * 2) pivot <= res < pivot + cycle
307 * (replace </<= with >/>= for negative cycles)
308 *
309 * where 'MOD' denotes the modulo operator for FLOOR DIVISION, which
310 * is not the same as the '%' operator in C: C requires division to be
311 * a truncated division, where remainder and dividend have the same
312 * sign if the remainder is not zero, whereas floor division requires
313 * divider and modulus to have the same sign for a non-zero modulus.
314 *
315 * This function has some useful applications:
316 *
317 * + let Y be a calendar year and V a truncated 2-digit year: then
318 * periodic_extend(Y-50, V, 100)
319 * is the closest expansion of the truncated year with respect to
320 * the full year, that is a 4-digit year with a difference of less
321 * than 50 years to the year Y. ("century unfolding")
322 *
323 * + let T be a UN*X time stamp and V be seconds-of-day: then
324 * perodic_extend(T-43200, V, 86400)
325 * is a time stamp that has the same seconds-of-day as the input
326 * value, with an absolute difference to T of <= 12hrs. ("day
327 * unfolding")
328 *
329 * + Wherever you have a truncated periodic value and a non-truncated
330 * base value and you want to match them somehow...
331 *
332 * Basically, the function delivers 'pivot + (value - pivot) % cycle',
333 * but the implementation takes some pains to avoid internal signed
334 * integer overflows in the '(value - pivot) % cycle' part and adheres
335 * to the floor division convention.
336 *
337 * If 64bit scalars where available on all intended platforms, writing a
338 * version that uses 64 bit ops would be easy; writing a general
339 * division routine for 64bit ops on a platform that can only do
340 * 32/16bit divisions and is still performant is a bit more
341 * difficult. Since most usecases can be coded in a way that does only
342 * require the 32-bit version a 64bit version is NOT provided here.
343 * ---------------------------------------------------------------------
344 */
345 int32_t
346 ntpcal_periodic_extend(
347 int32_t pivot,
348 int32_t value,
349 int32_t cycle
350 )
351 {
352 uint32_t diff;
353 char cpl = 0; /* modulo complement flag */
354 char neg = 0; /* sign change flag */
355
356 /* make the cycle positive and adjust the flags */
357 if (cycle < 0) {
358 cycle = - cycle;
359 neg ^= 1;
360 cpl ^= 1;
361 }
362 /* guard against div by zero or one */
363 if (cycle > 1) {
364 /*
365 * Get absolute difference as unsigned quantity and
366 * the complement flag. This is done by always
367 * subtracting the smaller value from the bigger
368 * one. This implementation works only on a two's
369 * complement machine!
370 */
371 if (value >= pivot) {
372 diff = (uint32_t)value - (uint32_t)pivot;
373 } else {
374 diff = (uint32_t)pivot - (uint32_t)value;
375 cpl ^= 1;
376 }
377 diff %= (uint32_t)cycle;
378 if (diff) {
379 if (cpl)
380 diff = cycle - diff;
381 if (neg)
382 diff = ~diff + 1;
383 pivot += diff;
384 }
385 }
386 return pivot;
387 }
388
389 /*
390 *-------------------------------------------------------------------
391 * Convert a timestamp in NTP scale to a 64bit seconds value in the UN*X
392 * scale with proper epoch unfolding around a given pivot or the current
393 * system time. This function happily accepts negative pivot values as
394 * timestamps befor 1970-01-01, so be aware of possible trouble on
395 * platforms with 32bit 'time_t'!
396 *
397 * This is also a periodic extension, but since the cycle is 2^32 and
398 * the shift is 2^31, we can do some *very* fast math without explicit
399 * divisions.
400 *-------------------------------------------------------------------
401 */
402 vint64
403 ntpcal_ntp_to_time(
404 uint32_t ntp,
405 const time_t * pivot
406 )
407 {
408 vint64 res;
409
410 #ifdef HAVE_INT64
411
412 res.q_s = (pivot != NULL)
413 ? *pivot
414 : now();
415 res.Q_s -= 0x80000000; /* unshift of half range */
416 ntp -= (uint32_t)JAN_1970; /* warp into UN*X domain */
417 ntp -= res.D_s.lo; /* cycle difference */
418 res.Q_s += (uint64_t)ntp; /* get expanded time */
419
420 #else /* no 64bit scalars */
421
422 time_t tmp;
423
424 tmp = (pivot != NULL)
425 ? *pivot
426 : now();
427 res = time_to_vint64(&tmp);
428 M_SUB(res.D_s.hi, res.D_s.lo, 0, 0x80000000);
429 ntp -= (uint32_t)JAN_1970; /* warp into UN*X domain */
430 ntp -= res.D_s.lo; /* cycle difference */
431 M_ADD(res.D_s.hi, res.D_s.lo, 0, ntp);
432
433 #endif /* no 64bit scalars */
434
435 return res;
436 }
437
438 /*
439 *-------------------------------------------------------------------
440 * Convert a timestamp in NTP scale to a 64bit seconds value in the NTP
441 * scale with proper epoch unfolding around a given pivot or the current
442 * system time.
443 *
444 * Note: The pivot must be given in the UN*X time domain!
445 *
446 * This is also a periodic extension, but since the cycle is 2^32 and
447 * the shift is 2^31, we can do some *very* fast math without explicit
448 * divisions.
449 *-------------------------------------------------------------------
450 */
451 vint64
452 ntpcal_ntp_to_ntp(
453 uint32_t ntp,
454 const time_t *pivot
455 )
456 {
457 vint64 res;
458
459 #ifdef HAVE_INT64
460
461 res.q_s = (pivot)
462 ? *pivot
463 : now();
464 res.Q_s -= 0x80000000; /* unshift of half range */
465 res.Q_s += (uint32_t)JAN_1970; /* warp into NTP domain */
466 ntp -= res.D_s.lo; /* cycle difference */
467 res.Q_s += (uint64_t)ntp; /* get expanded time */
468
469 #else /* no 64bit scalars */
470
471 time_t tmp;
472
473 tmp = (pivot)
474 ? *pivot
475 : now();
476 res = time_to_vint64(&tmp);
477 M_SUB(res.D_s.hi, res.D_s.lo, 0, 0x80000000u);
478 M_ADD(res.D_s.hi, res.D_s.lo, 0, (uint32_t)JAN_1970);/*into NTP */
479 ntp -= res.D_s.lo; /* cycle difference */
480 M_ADD(res.D_s.hi, res.D_s.lo, 0, ntp);
481
482 #endif /* no 64bit scalars */
483
484 return res;
485 }
486
487
488 /*
489 * ==================================================================
490 *
491 * Splitting values to composite entities
492 *
493 * ==================================================================
494 */
495
496 /*
497 *-------------------------------------------------------------------
498 * Split a 64bit seconds value into elapsed days in 'res.hi' and
499 * elapsed seconds since midnight in 'res.lo' using explicit floor
500 * division. This function happily accepts negative time values as
501 * timestamps before the respective epoch start.
502 * -------------------------------------------------------------------
503 */
504 ntpcal_split
505 ntpcal_daysplit(
506 const vint64 *ts
507 )
508 {
509 ntpcal_split res;
510
511 #ifdef HAVE_INT64
512
513 /* manual floor division by SECSPERDAY */
514 res.hi = (int32_t)(ts->q_s / SECSPERDAY);
515 res.lo = (int32_t)(ts->q_s % SECSPERDAY);
516 if (res.lo < 0) {
517 res.hi -= 1;
518 res.lo += SECSPERDAY;
519 }
520
521 #else
522
523 /*
524 * since we do not have 64bit ops, we have to this by hand.
525 * Luckily SECSPERDAY is 86400 is 675*128, so we do the division
526 * using chained 32/16 bit divisions and shifts.
527 */
528 vint64 op;
529 uint32_t q, r, a;
530 int isneg;
531
532 memcpy(&op, ts, sizeof(op));
533 /* fix sign */
534 isneg = M_ISNEG(op.D_s.hi);
535 if (isneg)
536 M_NEG(op.D_s.hi, op.D_s.lo);
537
538 /* save remainder of DIV 128, shift for divide */
539 r = op.D_s.lo & 127; /* save remainder bits */
540 op.D_s.lo = (op.D_s.lo >> 7) | (op.D_s.hi << 25);
541 op.D_s.hi = (op.D_s.hi >> 7);
542
543 /* now do a mnual division, trying to remove as many ops as
544 * possible -- division is always slow! An since we do not have
545 * the advantage of a specific 64/32 bit or even a specific 32/16
546 * bit division op, but must use the general 32/32bit division
547 * even if we *know* the divider fits into unsigned 16 bits, the
548 * exra code pathes should pay off.
549 */
550 a = op.D_s.hi;
551 if (a > 675u)
552 a = a % 675u;
553 if (a) {
554 a = (a << 16) | op.W_s.lh;
555 q = a / 675u;
556 a = a % 675u;
557
558 a = (a << 16) | op.W_s.ll;
559 q = (q << 16) | (a / 675u);
560 } else {
561 a = op.D_s.lo;
562 q = a / 675u;
563 }
564 a = a % 675u;
565
566 /* assemble remainder */
567 r |= a << 7;
568
569 /* fix sign of result */
570 if (isneg) {
571 if (r) {
572 r = SECSPERDAY - r;
573 q = ~q;
574 } else
575 q = ~q + 1;
576 }
577
578 res.hi = q;
579 res.lo = r;
580
581 #endif
582 return res;
583 }
584
585 /*
586 *-------------------------------------------------------------------
587 * Split a 32bit seconds value into h/m/s and excessive days. This
588 * function happily accepts negative time values as timestamps before
589 * midnight.
590 * -------------------------------------------------------------------
591 */
592 static int32_t
593 priv_timesplit(
594 int32_t split[3],
595 int32_t ts
596 )
597 {
598 int32_t days = 0;
599
600 /* make sure we have a positive offset into a day */
601 if (ts < 0 || ts >= SECSPERDAY) {
602 days = ts / SECSPERDAY;
603 ts = ts % SECSPERDAY;
604 if (ts < 0) {
605 days -= 1;
606 ts += SECSPERDAY;
607 }
608 }
609
610 /* get secs, mins, hours */
611 split[2] = (uint8_t)(ts % SECSPERMIN);
612 ts /= SECSPERMIN;
613 split[1] = (uint8_t)(ts % MINSPERHR);
614 split[0] = (uint8_t)(ts / MINSPERHR);
615
616 return days;
617 }
618
619 /*
620 * ---------------------------------------------------------------------
621 * Given the number of elapsed days in the calendar era, split this
622 * number into the number of elapsed years in 'res.hi' and the number
623 * of elapsed days of that year in 'res.lo'.
624 *
625 * if 'isleapyear' is not NULL, it will receive an integer that is 0 for
626 * regular years and a non-zero value for leap years.
627 *---------------------------------------------------------------------
628 */
629 ntpcal_split
630 ntpcal_split_eradays(
631 int32_t days,
632 int *isleapyear
633 )
634 {
635 ntpcal_split res;
636 int32_t n400, n100, n004, n001, yday; /* calendar year cycles */
637
638 /*
639 * Split off calendar cycles, using floor division in the first
640 * step. After that first step, simple division does it because
641 * all operands are positive; alas, we have to be aware of the
642 * possibe cycle overflows for 100 years and 1 year, caused by
643 * the additional leap day.
644 */
645 n400 = days / GREGORIAN_CYCLE_DAYS;
646 yday = days % GREGORIAN_CYCLE_DAYS;
647 if (yday < 0) {
648 n400 -= 1;
649 yday += GREGORIAN_CYCLE_DAYS;
650 }
651 n100 = yday / GREGORIAN_NORMAL_CENTURY_DAYS;
652 yday = yday % GREGORIAN_NORMAL_CENTURY_DAYS;
653 n004 = yday / GREGORIAN_NORMAL_LEAP_CYCLE_DAYS;
654 yday = yday % GREGORIAN_NORMAL_LEAP_CYCLE_DAYS;
655 n001 = yday / DAYSPERYEAR;
656 yday = yday % DAYSPERYEAR;
657
658 /*
659 * check for leap cycle overflows and calculate the leap flag
660 * if needed
661 */
662 if ((n001 | n100) > 3) {
663 /* hit last day of leap year */
664 n001 -= 1;
665 yday += DAYSPERYEAR;
666 if (isleapyear)
667 *isleapyear = 1;
668 } else if (isleapyear)
669 *isleapyear = (n001 == 3) && ((n004 != 24) || (n100 == 3));
670
671 /* now merge the cycles to elapsed years, using horner scheme */
672 res.hi = ((4*n400 + n100)*25 + n004)*4 + n001;
673 res.lo = yday;
674
675 return res;
676 }
677
678 /*
679 *---------------------------------------------------------------------
680 * Given a number of elapsed days in a year and a leap year indicator,
681 * split the number of elapsed days into the number of elapsed months in
682 * 'res.hi' and the number of elapsed days of that month in 'res.lo'.
683 *
684 * This function will fail and return {-1,-1} if the number of elapsed
685 * days is not in the valid range!
686 *---------------------------------------------------------------------
687 */
688 ntpcal_split
689 ntpcal_split_yeardays(
690 int32_t eyd,
691 int isleapyear
692 )
693 {
694 ntpcal_split res;
695 const uint16_t *lt; /* month length table */
696
697 /* check leap year flag and select proper table */
698 lt = real_month_table[(isleapyear != 0)];
699 if (0 <= eyd && eyd < lt[12]) {
700 /* get zero-based month by approximation & correction step */
701 res.hi = eyd >> 5; /* approx month; might be 1 too low */
702 if (lt[res.hi + 1] <= eyd) /* fixup approximative month value */
703 res.hi += 1;
704 res.lo = eyd - lt[res.hi];
705 } else {
706 res.lo = res.hi = -1;
707 }
708
709 return res;
710 }
711
712 /*
713 *---------------------------------------------------------------------
714 * Convert a RD into the date part of a 'struct calendar'.
715 *---------------------------------------------------------------------
716 */
717 int
718 ntpcal_rd_to_date(
719 struct calendar *jd,
720 int32_t rd
721 )
722 {
723 ntpcal_split split;
724 int leaps;
725 int retv;
726
727 leaps = 0;
728 retv = 0;
729 /* get day-of-week first */
730 jd->weekday = rd % 7;
731 if (jd->weekday >= 7) /* unsigned! */
732 jd->weekday += 7;
733
734 split = ntpcal_split_eradays(rd - 1, &leaps);
735 retv = leaps;
736 /* get year and day-of-year */
737 jd->year = (uint16_t)split.hi + 1;
738 if (jd->year != split.hi + 1) {
739 jd->year = 0;
740 retv = -1; /* bletch. overflow trouble. */
741 }
742 jd->yearday = (uint16_t)split.lo + 1;
743
744 /* convert to month and mday */
745 split = ntpcal_split_yeardays(split.lo, leaps);
746 jd->month = (uint8_t)split.hi + 1;
747 jd->monthday = (uint8_t)split.lo + 1;
748
749 return retv ? retv : leaps;
750 }
751
752 /*
753 *---------------------------------------------------------------------
754 * Convert a RD into the date part of a 'struct tm'.
755 *---------------------------------------------------------------------
756 */
757 int
758 ntpcal_rd_to_tm(
759 struct tm *utm,
760 int32_t rd
761 )
762 {
763 ntpcal_split split;
764 int leaps;
765
766 leaps = 0;
767 /* get day-of-week first */
768 utm->tm_wday = rd % 7;
769 if (utm->tm_wday < 0)
770 utm->tm_wday += 7;
771
772 /* get year and day-of-year */
773 split = ntpcal_split_eradays(rd - 1, &leaps);
774 utm->tm_year = split.hi - 1899;
775 utm->tm_yday = split.lo; /* 0-based */
776
777 /* convert to month and mday */
778 split = ntpcal_split_yeardays(split.lo, leaps);
779 utm->tm_mon = split.hi; /* 0-based */
780 utm->tm_mday = split.lo + 1; /* 1-based */
781
782 return leaps;
783 }
784
785 /*
786 *---------------------------------------------------------------------
787 * Take a value of seconds since midnight and split it into hhmmss in a
788 * 'struct calendar'.
789 *---------------------------------------------------------------------
790 */
791 int32_t
792 ntpcal_daysec_to_date(
793 struct calendar *jd,
794 int32_t sec
795 )
796 {
797 int32_t days;
798 int ts[3];
799
800 days = priv_timesplit(ts, sec);
801 jd->hour = (uint8_t)ts[0];
802 jd->minute = (uint8_t)ts[1];
803 jd->second = (uint8_t)ts[2];
804
805 return days;
806 }
807
808 /*
809 *---------------------------------------------------------------------
810 * Take a value of seconds since midnight and split it into hhmmss in a
811 * 'struct tm'.
812 *---------------------------------------------------------------------
813 */
814 int32_t
815 ntpcal_daysec_to_tm(
816 struct tm *utm,
817 int32_t sec
818 )
819 {
820 int32_t days;
821 int32_t ts[3];
822
823 days = priv_timesplit(ts, sec);
824 utm->tm_hour = ts[0];
825 utm->tm_min = ts[1];
826 utm->tm_sec = ts[2];
827
828 return days;
829 }
830
831 /*
832 *---------------------------------------------------------------------
833 * take a split representation for day/second-of-day and day offset
834 * and convert it to a 'struct calendar'. The seconds will be normalised
835 * into the range of a day, and the day will be adjusted accordingly.
836 *
837 * returns >0 if the result is in a leap year, 0 if in a regular
838 * year and <0 if the result did not fit into the calendar struct.
839 *---------------------------------------------------------------------
840 */
841 int
842 ntpcal_daysplit_to_date(
843 struct calendar *jd,
844 const ntpcal_split *ds,
845 int32_t dof
846 )
847 {
848 dof += ntpcal_daysec_to_date(jd, ds->lo);
849 return ntpcal_rd_to_date(jd, ds->hi + dof);
850 }
851
852 /*
853 *---------------------------------------------------------------------
854 * take a split representation for day/second-of-day and day offset
855 * and convert it to a 'struct tm'. The seconds will be normalised
856 * into the range of a day, and the day will be adjusted accordingly.
857 *
858 * returns 1 if the result is in a leap year and zero if in a regular
859 * year.
860 *---------------------------------------------------------------------
861 */
862 int
863 ntpcal_daysplit_to_tm(
864 struct tm *utm,
865 const ntpcal_split *ds ,
866 int32_t dof
867 )
868 {
869 dof += ntpcal_daysec_to_tm(utm, ds->lo);
870
871 return ntpcal_rd_to_tm(utm, ds->hi + dof);
872 }
873
874 /*
875 *---------------------------------------------------------------------
876 * Take a UN*X time and convert to a calendar structure.
877 *---------------------------------------------------------------------
878 */
879 int
880 ntpcal_time_to_date(
881 struct calendar *jd,
882 const vint64 *ts
883 )
884 {
885 ntpcal_split ds;
886
887 ds = ntpcal_daysplit(ts);
888 ds.hi += ntpcal_daysec_to_date(jd, ds.lo);
889 ds.hi += DAY_UNIX_STARTS;
890
891 return ntpcal_rd_to_date(jd, ds.hi);
892 }
893
894
895 /*
896 * ==================================================================
897 *
898 * merging composite entities
899 *
900 * ==================================================================
901 */
902
903 /*
904 *---------------------------------------------------------------------
905 * Merge a number of days and a number of seconds into seconds,
906 * expressed in 64 bits to avoid overflow.
907 *---------------------------------------------------------------------
908 */
909 vint64
910 ntpcal_dayjoin(
911 int32_t days,
912 int32_t secs
913 )
914 {
915 vint64 res;
916
917 #ifdef HAVE_INT64
918
919 res.q_s = days;
920 res.q_s *= SECSPERDAY;
921 res.q_s += secs;
922
923 #else
924
925 uint32_t p1, p2;
926 int isneg;
927
928 /*
929 * res = days *86400 + secs, using manual 16/32 bit
930 * multiplications and shifts.
931 */
932 isneg = (days < 0);
933 if (isneg)
934 days = -days;
935
936 /* assemble days * 675 */
937 res.D_s.lo = (days & 0xFFFF) * 675u;
938 res.D_s.hi = 0;
939 p1 = (days >> 16) * 675u;
940 p2 = p1 >> 16;
941 p1 = p1 << 16;
942 M_ADD(res.D_s.hi, res.D_s.lo, p2, p1);
943
944 /* mul by 128, using shift */
945 res.D_s.hi = (res.D_s.hi << 7) | (res.D_s.lo >> 25);
946 res.D_s.lo = (res.D_s.lo << 7);
947
948 /* fix sign */
949 if (isneg)
950 M_NEG(res.D_s.hi, res.D_s.lo);
951
952 /* properly add seconds */
953 p2 = 0;
954 if (secs < 0) {
955 p1 = (uint32_t)-secs;
956 M_NEG(p2, p1);
957 } else {
958 p1 = (uint32_t)secs;
959 }
960 M_ADD(res.D_s.hi, res.D_s.lo, p2, p1);
961
962 #endif
963
964 return res;
965 }
966
967 /*
968 *---------------------------------------------------------------------
969 * Convert elapsed years in Era into elapsed days in Era.
970 *
971 * To accomodate for negative values of years, floor division would be
972 * required for all division operations. This can be eased by first
973 * splitting the years into full 400-year cycles and years in the
974 * cycle. Only this operation must be coded as a full floor division; as
975 * the years in the cycle is a non-negative number, all other divisions
976 * can be regular truncated divisions.
977 *---------------------------------------------------------------------
978 */
979 int32_t
980 ntpcal_days_in_years(
981 int32_t years
982 )
983 {
984 int32_t cycle; /* full gregorian cycle */
985
986 /* split off full calendar cycles, using floor division */
987 cycle = years / 400;
988 years = years % 400;
989 if (years < 0) {
990 cycle -= 1;
991 years += 400;
992 }
993
994 /*
995 * Calculate days in cycle. years now is a non-negative number,
996 * holding the number of years in the 400-year cycle.
997 */
998 return cycle * GREGORIAN_CYCLE_DAYS
999 + years * DAYSPERYEAR /* days inregular years */
1000 + years / 4 /* 4 year leap rule */
1001 - years / 100; /* 100 year leap rule */
1002 /* the 400-year rule does not apply due to full-cycle split-off */
1003 }
1004
1005 /*
1006 *---------------------------------------------------------------------
1007 * Convert a number of elapsed month in a year into elapsed days in year.
1008 *
1009 * The month will be normalized, and 'res.hi' will contain the
1010 * excessive years that must be considered when converting the years,
1011 * while 'res.lo' will contain the number of elapsed days since start
1012 * of the year.
1013 *
1014 * This code uses the shifted-month-approach to convert month to days,
1015 * because then there is no need to have explicit leap year
1016 * information. The slight disadvantage is that for most month values
1017 * the result is a negative value, and the year excess is one; the
1018 * conversion is then simply based on the start of the following year.
1019 *---------------------------------------------------------------------
1020 */
1021 ntpcal_split
1022 ntpcal_days_in_months(
1023 int32_t m
1024 )
1025 {
1026 ntpcal_split res;
1027
1028 /* normalize month into range */
1029 res.hi = 0;
1030 res.lo = m;
1031 if (res.lo < 0 || res.lo >= 12) {
1032 res.hi = res.lo / 12;
1033 res.lo = res.lo % 12;
1034 if (res.lo < 0) {
1035 res.hi -= 1;
1036 res.lo += 12;
1037 }
1038 }
1039
1040 /* add 10 month for year starting with march */
1041 if (res.lo < 2)
1042 res.lo += 10;
1043 else {
1044 res.hi += 1;
1045 res.lo -= 2;
1046 }
1047
1048 /* get cummulated days in year with unshift */
1049 res.lo = shift_month_table[res.lo] - 306;
1050
1051 return res;
1052 }
1053
1054 /*
1055 *---------------------------------------------------------------------
1056 * Convert ELAPSED years/months/days of gregorian calendar to elapsed
1057 * days in Gregorian epoch.
1058 *
1059 * If you want to convert years and days-of-year, just give a month of
1060 * zero.
1061 *---------------------------------------------------------------------
1062 */
1063 int32_t
1064 ntpcal_edate_to_eradays(
1065 int32_t years,
1066 int32_t mons,
1067 int32_t mdays
1068 )
1069 {
1070 ntpcal_split tmp;
1071 int32_t res;
1072
1073 if (mons) {
1074 tmp = ntpcal_days_in_months(mons);
1075 res = ntpcal_days_in_years(years + tmp.hi) + tmp.lo;
1076 } else
1077 res = ntpcal_days_in_years(years);
1078 res += mdays;
1079
1080 return res;
1081 }
1082
1083 /*
1084 *---------------------------------------------------------------------
1085 * Convert ELAPSED years/months/days of gregorian calendar to elapsed
1086 * days in year.
1087 *
1088 * Note: This will give the true difference to the start of the given year,
1089 * even if months & days are off-scale.
1090 *---------------------------------------------------------------------
1091 */
1092 int32_t
1093 ntpcal_edate_to_yeardays(
1094 int32_t years,
1095 int32_t mons,
1096 int32_t mdays
1097 )
1098 {
1099 ntpcal_split tmp;
1100
1101 if (0 <= mons && mons < 12) {
1102 years += 1;
1103 mdays += real_month_table[is_leapyear(years)][mons];
1104 } else {
1105 tmp = ntpcal_days_in_months(mons);
1106 mdays += tmp.lo
1107 + ntpcal_days_in_years(years + tmp.hi)
1108 - ntpcal_days_in_years(years);
1109 }
1110
1111 return mdays;
1112 }
1113
1114 /*
1115 *---------------------------------------------------------------------
1116 * Convert elapsed days and the hour/minute/second information into
1117 * total seconds.
1118 *
1119 * If 'isvalid' is not NULL, do a range check on the time specification
1120 * and tell if the time input is in the normal range, permitting for a
1121 * single leapsecond.
1122 *---------------------------------------------------------------------
1123 */
1124 int32_t
1125 ntpcal_etime_to_seconds(
1126 int32_t hours,
1127 int32_t minutes,
1128 int32_t seconds
1129 )
1130 {
1131 int32_t res;
1132
1133 res = (hours * MINSPERHR + minutes) * SECSPERMIN + seconds;
1134
1135 return res;
1136 }
1137
1138 /*
1139 *---------------------------------------------------------------------
1140 * Convert the date part of a 'struct tm' (that is, year, month,
1141 * day-of-month) into the RD of that day.
1142 *---------------------------------------------------------------------
1143 */
1144 int32_t
1145 ntpcal_tm_to_rd(
1146 const struct tm *utm
1147 )
1148 {
1149 return ntpcal_edate_to_eradays(utm->tm_year + 1899,
1150 utm->tm_mon,
1151 utm->tm_mday - 1) + 1;
1152 }
1153
1154 /*
1155 *---------------------------------------------------------------------
1156 * Convert the date part of a 'struct calendar' (that is, year, month,
1157 * day-of-month) into the RD of that day.
1158 *---------------------------------------------------------------------
1159 */
1160 int32_t
1161 ntpcal_date_to_rd(
1162 const struct calendar *jd
1163 )
1164 {
1165 return ntpcal_edate_to_eradays((int32_t)jd->year - 1,
1166 (int32_t)jd->month - 1,
1167 (int32_t)jd->monthday - 1) + 1;
1168 }
1169
1170 /*
1171 *---------------------------------------------------------------------
1172 * convert a year number to rata die of year start
1173 *---------------------------------------------------------------------
1174 */
1175 int32_t
1176 ntpcal_year_to_ystart(
1177 int32_t year
1178 )
1179 {
1180 return ntpcal_days_in_years(year - 1) + 1;
1181 }
1182
1183 /*
1184 *---------------------------------------------------------------------
1185 * For a given RD, get the RD of the associated year start,
1186 * that is, the RD of the last January,1st on or before that day.
1187 *---------------------------------------------------------------------
1188 */
1189 int32_t
1190 ntpcal_rd_to_ystart(
1191 int32_t rd
1192 )
1193 {
1194 /*
1195 * Rather simple exercise: split the day number into elapsed
1196 * years and elapsed days, then remove the elapsed days from the
1197 * input value. Nice'n sweet...
1198 */
1199 return rd - ntpcal_split_eradays(rd - 1, NULL).lo;
1200 }
1201
1202 /*
1203 *---------------------------------------------------------------------
1204 * For a given RD, get the RD of the associated month start.
1205 *---------------------------------------------------------------------
1206 */
1207 int32_t
1208 ntpcal_rd_to_mstart(
1209 int32_t rd
1210 )
1211 {
1212 ntpcal_split split;
1213 int leaps;
1214
1215 split = ntpcal_split_eradays(rd - 1, &leaps);
1216 split = ntpcal_split_yeardays(split.lo, leaps);
1217
1218 return rd - split.lo;
1219 }
1220
1221 /*
1222 *---------------------------------------------------------------------
1223 * take a 'struct calendar' and get the seconds-of-day from it.
1224 *---------------------------------------------------------------------
1225 */
1226 int32_t
1227 ntpcal_date_to_daysec(
1228 const struct calendar *jd
1229 )
1230 {
1231 return ntpcal_etime_to_seconds(jd->hour, jd->minute,
1232 jd->second);
1233 }
1234
1235 /*
1236 *---------------------------------------------------------------------
1237 * take a 'struct tm' and get the seconds-of-day from it.
1238 *---------------------------------------------------------------------
1239 */
1240 int32_t
1241 ntpcal_tm_to_daysec(
1242 const struct tm *utm
1243 )
1244 {
1245 return ntpcal_etime_to_seconds(utm->tm_hour, utm->tm_min,
1246 utm->tm_sec);
1247 }
1248
1249 /*
1250 *---------------------------------------------------------------------
1251 * take a 'struct calendar' and convert it to a 'time_t'
1252 *---------------------------------------------------------------------
1253 */
1254 time_t
1255 ntpcal_date_to_time(
1256 const struct calendar *jd
1257 )
1258 {
1259 vint64 join;
1260 int32_t days, secs;
1261
1262 days = ntpcal_date_to_rd(jd) - DAY_UNIX_STARTS;
1263 secs = ntpcal_date_to_daysec(jd);
1264 join = ntpcal_dayjoin(days, secs);
1265
1266 return vint64_to_time(&join);
1267 }
1268
1269
1270 /*
1271 * ==================================================================
1272 *
1273 * extended and unchecked variants of caljulian/caltontp
1274 *
1275 * ==================================================================
1276 */
1277 int
1278 ntpcal_ntp64_to_date(
1279 struct calendar *jd,
1280 const vint64 *ntp
1281 )
1282 {
1283 ntpcal_split ds;
1284
1285 ds = ntpcal_daysplit(ntp);
1286 ds.hi += ntpcal_daysec_to_date(jd, ds.lo);
1287
1288 return ntpcal_rd_to_date(jd, ds.hi + DAY_NTP_STARTS);
1289 }
1290
1291 int
1292 ntpcal_ntp_to_date(
1293 struct calendar *jd,
1294 uint32_t ntp,
1295 const time_t *piv
1296 )
1297 {
1298 vint64 ntp64;
1299
1300 /*
1301 * Unfold ntp time around current time into NTP domain. Split
1302 * into days and seconds, shift days into CE domain and
1303 * process the parts.
1304 */
1305 ntp64 = ntpcal_ntp_to_ntp(ntp, piv);
1306 return ntpcal_ntp64_to_date(jd, &ntp64);
1307 }
1308
1309
1310 vint64
1311 ntpcal_date_to_ntp64(
1312 const struct calendar *jd
1313 )
1314 {
1315 /*
1316 * Convert date to NTP. Ignore yearday, use d/m/y only.
1317 */
1318 return ntpcal_dayjoin(ntpcal_date_to_rd(jd) - DAY_NTP_STARTS,
1319 ntpcal_date_to_daysec(jd));
1320 }
1321
1322
1323 uint32_t
1324 ntpcal_date_to_ntp(
1325 const struct calendar *jd
1326 )
1327 {
1328 /*
1329 * Get lower half of 64-bit NTP timestamp from date/time.
1330 */
1331 return ntpcal_date_to_ntp64(jd).d_s.lo;
1332 }
1333
1334
1335
1336 /*
1337 * ==================================================================
1338 *
1339 * day-of-week calculations
1340 *
1341 * ==================================================================
1342 */
1343 /*
1344 * Given a RataDie and a day-of-week, calculate a RDN that is reater-than,
1345 * greater-or equal, closest, less-or-equal or less-than the given RDN
1346 * and denotes the given day-of-week
1347 */
1348 int32_t
1349 ntpcal_weekday_gt(
1350 int32_t rdn,
1351 int32_t dow
1352 )
1353 {
1354 return ntpcal_periodic_extend(rdn+1, dow, 7);
1355 }
1356
1357 int32_t
1358 ntpcal_weekday_ge(
1359 int32_t rdn,
1360 int32_t dow
1361 )
1362 {
1363 return ntpcal_periodic_extend(rdn, dow, 7);
1364 }
1365
1366 int32_t
1367 ntpcal_weekday_close(
1368 int32_t rdn,
1369 int32_t dow
1370 )
1371 {
1372 return ntpcal_periodic_extend(rdn-3, dow, 7);
1373 }
1374
1375 int32_t
1376 ntpcal_weekday_le(
1377 int32_t rdn,
1378 int32_t dow
1379 )
1380 {
1381 return ntpcal_periodic_extend(rdn, dow, -7);
1382 }
1383
1384 int32_t
1385 ntpcal_weekday_lt(
1386 int32_t rdn,
1387 int32_t dow
1388 )
1389 {
1390 return ntpcal_periodic_extend(rdn-1, dow, -7);
1391 }
1392
1393 /*
1394 * ==================================================================
1395 *
1396 * ISO week-calendar conversions
1397 *
1398 * The ISO8601 calendar defines a calendar of years, weeks and weekdays.
1399 * It is related to the Gregorian calendar, and a ISO year starts at the
1400 * Monday closest to Jan,1st of the corresponding Gregorian year. A ISO
1401 * calendar year has always 52 or 53 weeks, and like the Grogrian
1402 * calendar the ISO8601 calendar repeats itself every 400 years, or
1403 * 146097 days, or 20871 weeks.
1404 *
1405 * While it is possible to write ISO calendar functions based on the
1406 * Gregorian calendar functions, the following implementation takes a
1407 * different approach, based directly on years and weeks.
1408 *
1409 * Analysis of the tabulated data shows that it is not possible to
1410 * interpolate from years to weeks over a full 400 year range; cyclic
1411 * shifts over 400 years do not provide a solution here. But it *is*
1412 * possible to interpolate over every single century of the 400-year
1413 * cycle. (The centennial leap year rule seems to be the culprit here.)
1414 *
1415 * It can be shown that a conversion from years to weeks can be done
1416 * using a linear transformation of the form
1417 *
1418 * w = floor( y * a + b )
1419 *
1420 * where the slope a must hold to
1421 *
1422 * 52.1780821918 <= a < 52.1791044776
1423 *
1424 * and b must be chosen according to the selected slope and the number
1425 * of the century in a 400-year period.
1426 *
1427 * The inverse calculation can also be done in this way. Careful scaling
1428 * provides an unlimited set of integer coefficients a,k,b that enable
1429 * us to write the calulation in the form
1430 *
1431 * w = (y * a + b ) / k
1432 * y = (w * a' + b') / k'
1433 *
1434 * In this implementation the values of k and k' are chosen to be
1435 * smallest possible powers of two, so the division can be implemented
1436 * as shifts if the optimiser chooses to do so.
1437 *
1438 * ==================================================================
1439 */
1440
1441 /*
1442 * Given a number of elapsed (ISO-)years since the begin of the
1443 * christian era, return the number of elapsed weeks corresponding to
1444 * the number of years.
1445 */
1446 int32_t
1447 isocal_weeks_in_years(
1448 int32_t years
1449 )
1450 {
1451 /*
1452 * use: w = (y * 53431 + b[c]) / 1024 as interpolation
1453 */
1454 static const int32_t bctab[4] = { 449, 157, 889, 597 };
1455 int32_t cycle; /* full gregorian cycle */
1456 int32_t cents; /* full centuries */
1457 int32_t weeks; /* accumulated weeks */
1458
1459 /* split off full calendar cycles, using floor division */
1460 cycle = years / 400;
1461 years = years % 400;
1462 if (years < 0) {
1463 cycle -= 1;
1464 years += 400;
1465 }
1466
1467 /* split off full centuries */
1468 cents = years / 100;
1469 years = years % 100;
1470
1471 /*
1472 * calculate elapsed weeks, taking into account that the
1473 * first, third and fourth century have 5218 weeks but the
1474 * second century falls short by one week.
1475 */
1476 weeks = (years * 53431 + bctab[cents]) / 1024;
1477
1478 return cycle * GREGORIAN_CYCLE_WEEKS
1479 + cents * 5218 - (cents > 1)
1480 + weeks;
1481 }
1482
1483 /*
1484 * Given a number of elapsed weeks since the begin of the christian
1485 * era, split this number into the number of elapsed years in res.hi
1486 * and the excessive number of weeks in res.lo. (That is, res.lo is
1487 * the number of elapsed weeks in the remaining partial year.)
1488 */
1489 ntpcal_split
1490 isocal_split_eraweeks(
1491 int32_t weeks
1492 )
1493 {
1494 /*
1495 * use: y = (w * 157 + b[c]) / 8192 as interpolation
1496 */
1497 static const int32_t bctab[4] = { 85, 131, 17, 62 };
1498 ntpcal_split res;
1499 int32_t cents;
1500
1501 /*
1502 * split off 400-year cycles, using the fact that a 400-year
1503 * cycle has 146097 days, which is exactly 20871 weeks.
1504 */
1505 res.hi = weeks / GREGORIAN_CYCLE_WEEKS;
1506 res.lo = weeks % GREGORIAN_CYCLE_WEEKS;
1507 if (res.lo < 0) {
1508 res.hi -= 1;
1509 res.lo += GREGORIAN_CYCLE_WEEKS;
1510 }
1511 res.hi *= 400;
1512
1513 /*
1514 * split off centuries, taking into account that the first,
1515 * third and fourth century have 5218 weeks but that the
1516 * second century falls short by one week.
1517 */
1518 res.lo += (res.lo >= 10435);
1519 cents = res.lo / 5218;
1520 res.lo %= 5218; /* res.lo is weeks in century now */
1521
1522 /* convert elapsed weeks in century to elapsed years and weeks */
1523 res.lo = res.lo * 157 + bctab[cents];
1524 res.hi += cents * 100 + res.lo / 8192;
1525 res.lo = (res.lo % 8192) / 157;
1526
1527 return res;
1528 }
1529
1530 /*
1531 * Given a second in the NTP time scale and a pivot, expand the NTP
1532 * time stamp around the pivot and convert into an ISO calendar time
1533 * stamp.
1534 */
1535 int
1536 isocal_ntp64_to_date(
1537 struct isodate *id,
1538 const vint64 *ntp
1539 )
1540 {
1541 ntpcal_split ds;
1542 int32_t ts[3];
1543
1544 /*
1545 * Split NTP time into days and seconds, shift days into CE
1546 * domain and process the parts.
1547 */
1548 ds = ntpcal_daysplit(ntp);
1549
1550 /* split time part */
1551 ds.hi += priv_timesplit(ts, ds.lo);
1552 id->hour = (uint8_t)ts[0];
1553 id->minute = (uint8_t)ts[1];
1554 id->second = (uint8_t)ts[2];
1555
1556 /* split date part */
1557 ds.lo = ds.hi + DAY_NTP_STARTS - 1; /* elapsed era days */
1558 ds.hi = ds.lo / 7; /* elapsed era weeks */
1559 ds.lo = ds.lo % 7; /* elapsed week days */
1560 if (ds.lo < 0) { /* floor division! */
1561 ds.hi -= 1;
1562 ds.lo += 7;
1563 }
1564 id->weekday = (uint8_t)ds.lo + 1; /* weekday result */
1565
1566 ds = isocal_split_eraweeks(ds.hi); /* elapsed years&week*/
1567 id->year = (uint16_t)ds.hi + 1; /* shift to current */
1568 id->week = (uint8_t )ds.lo + 1;
1569
1570 return (ds.hi >= 0 && ds.hi < 0x0000FFFF);
1571 }
1572
1573 int
1574 isocal_ntp_to_date(
1575 struct isodate *id,
1576 uint32_t ntp,
1577 const time_t *piv
1578 )
1579 {
1580 vint64 ntp64;
1581
1582 /*
1583 * Unfold ntp time around current time into NTP domain, then
1584 * convert the full time stamp.
1585 */
1586 ntp64 = ntpcal_ntp_to_ntp(ntp, piv);
1587 return isocal_ntp64_to_date(id, &ntp64);
1588 }
1589
1590 /*
1591 * Convert a ISO date spec into a second in the NTP time scale,
1592 * properly truncated to 32 bit.
1593 */
1594 vint64
1595 isocal_date_to_ntp64(
1596 const struct isodate *id
1597 )
1598 {
1599 int32_t weeks, days, secs;
1600
1601 weeks = isocal_weeks_in_years((int32_t)id->year - 1)
1602 + (int32_t)id->week - 1;
1603 days = weeks * 7 + (int32_t)id->weekday;
1604 /* days is RDN of ISO date now */
1605 secs = ntpcal_etime_to_seconds(id->hour, id->minute, id->second);
1606
1607 return ntpcal_dayjoin(days - DAY_NTP_STARTS, secs);
1608 }
1609
1610 uint32_t
1611 isocal_date_to_ntp(
1612 const struct isodate *id
1613 )
1614 {
1615 /*
1616 * Get lower half of 64-bit NTP timestamp from date/time.
1617 */
1618 return isocal_date_to_ntp64(id).d_s.lo;
1619 }
1620
1621 /* -*-EOF-*- */
1622