2 * ntp_calendar.c - calendar and helper functions
4 * Written by Juergen Perlinger (perlinger@ntp.org) for the NTP project.
5 * The contents of 'html/copyright.html' apply.
10 #include "ntp_types.h"
11 #include "ntp_calendar.h"
12 #include "ntp_stdlib.h"
14 #include "ntp_unixtime.h"
17 *---------------------------------------------------------------------
18 * replacing the 'time()' function
19 * --------------------------------------------------------------------
22 static systime_func_ptr systime_func = &time;
23 static inline time_t now(void);
28 systime_func_ptr nfunc
45 return (*systime_func)(NULL);
49 *---------------------------------------------------------------------
50 * Convert between 'time_t' and 'vint64'
51 *---------------------------------------------------------------------
63 #if SIZEOF_TIME_T <= 4
67 res.D_s.lo = (uint32_t)-tt;
68 M_NEG(res.D_s.hi, res.D_s.lo);
70 res.D_s.lo = (uint32_t)tt;
73 #elif defined(HAVE_INT64)
79 * shifting negative signed quantities is compiler-dependent, so
80 * we better avoid it and do it all manually. And shifting more
81 * than the width of a quantity is undefined. Also a don't do!
85 res.D_s.lo = (uint32_t)tt;
86 res.D_s.hi = (uint32_t)(tt >> 32);
87 M_NEG(res.D_s.hi, res.D_s.lo);
89 res.D_s.lo = (uint32_t)tt;
90 res.D_s.hi = (uint32_t)(tt >> 32);
106 #if SIZEOF_TIME_T <= 4
108 res = (time_t)tv->D_s.lo;
110 #elif defined(HAVE_INT64)
112 res = (time_t)tv->q_s;
116 res = ((time_t)tv->d_s.hi << 32) | tv->D_s.lo;
124 *---------------------------------------------------------------------
125 * Get the build date & time
126 *---------------------------------------------------------------------
129 ntpcal_get_build_date(
133 /* The C standard tells us the format of '__DATE__':
135 * __DATE__ The date of translation of the preprocessing
136 * translation unit: a character string literal of the form "Mmm
137 * dd yyyy", where the names of the months are the same as those
138 * generated by the asctime function, and the first character of
139 * dd is a space character if the value is less than 10. If the
140 * date of translation is not available, an
141 * implementation-defined valid date shall be supplied.
143 * __TIME__ The time of translation of the preprocessing
144 * translation unit: a character string literal of the form
145 * "hh:mm:ss" as in the time generated by the asctime
146 * function. If the time of translation is not available, an
147 * implementation-defined valid time shall be supplied.
149 * Note that MSVC declares DATE and TIME to be in the local time
150 * zone, while neither the C standard nor the GCC docs make any
151 * statement about this. As a result, we may be +/-12hrs off
152 * UTC. But for practical purposes, this should not be a
157 static const char build[] = MKREPRO_TIME "/" MKREPRO_DATE;
159 static const char build[] = __TIME__ "/" __DATE__;
161 static const char mlist[] = "JanFebMarAprMayJunJulAugSepOctNovDec";
165 unsigned short hour, minute, second, day, year;
166 /* Note: The above quantities are used for sscanf 'hu' format,
167 * so using 'uint16_t' is contra-indicated!
171 static int ignore = 0;
180 /* check environment if build date should be ignored */
183 envstr = getenv("NTPD_IGNORE_BUILD_DATE");
184 ignore = 1 + (envstr && (!*envstr || !strcasecmp(envstr, "yes")));
190 if (6 == sscanf(build, "%hu:%hu:%hu/%3s %hu %hu",
191 &hour, &minute, &second, monstr, &day, &year)) {
192 cp = strstr(mlist, monstr);
195 jd->month = (uint8_t)((cp - mlist) / 3 + 1);
196 jd->monthday = (uint8_t)day;
197 jd->hour = (uint8_t)hour;
198 jd->minute = (uint8_t)minute;
199 jd->second = (uint8_t)second;
210 *---------------------------------------------------------------------
211 * basic calendar stuff
212 * --------------------------------------------------------------------
215 /* month table for a year starting with March,1st */
216 static const uint16_t shift_month_table[13] = {
217 0, 31, 61, 92, 122, 153, 184, 214, 245, 275, 306, 337, 366
220 /* month tables for years starting with January,1st; regular & leap */
221 static const uint16_t real_month_table[2][13] = {
222 /* -*- table for regular years -*- */
223 { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 },
224 /* -*- table for leap years -*- */
225 { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 }
229 * Some notes on the terminology:
231 * We use the proleptic Gregorian calendar, which is the Gregorian
232 * calendar extended in both directions ad infinitum. This totally
233 * disregards the fact that this calendar was invented in 1582, and
234 * was adopted at various dates over the world; sometimes even after
235 * the start of the NTP epoch.
237 * Normally date parts are given as current cycles, while time parts
238 * are given as elapsed cycles:
240 * 1970-01-01/03:04:05 means 'IN the 1970st. year, IN the first month,
241 * ON the first day, with 3hrs, 4minutes and 5 seconds elapsed.
243 * The basic calculations for this calendar implementation deal with
244 * ELAPSED date units, which is the number of full years, full months
245 * and full days before a date: 1970-01-01 would be (1969, 0, 0) in
248 * To ease the numeric computations, month and day values outside the
249 * normal range are acceptable: 2001-03-00 will be treated as the day
250 * before 2001-03-01, 2000-13-32 will give the same result as
251 * 2001-02-01 and so on.
253 * 'rd' or 'RD' is used as an abbreviation for the latin 'rata die'
254 * (day number). This is the number of days elapsed since 0000-12-31
255 * in the proleptic Gregorian calendar. The begin of the Christian Era
256 * (0001-01-01) is RD(1).
259 * Some notes on the implementation:
261 * Calendar algorithms thrive on the division operation, which is one of
262 * the slowest numerical operations in any CPU. What saves us here from
263 * abysmal performance is the fact that all divisions are divisions by
264 * constant numbers, and most compilers can do this by a multiplication
265 * operation. But this might not work when using the div/ldiv/lldiv
266 * function family, because many compilers are not able to do inline
267 * expansion of the code with following optimisation for the
268 * constant-divider case.
270 * Also div/ldiv/lldiv are defined in terms of int/long/longlong, which
271 * are inherently target dependent. Nothing that could not be cured with
272 * autoconf, but still a mess...
274 * Furthermore, we need floor division while C demands truncation to
275 * zero, so additional steps are required to make sure the algorithms
278 * For all this, all divisions by constant are coded manually, even when
279 * there is a joined div/mod operation: The optimiser should sort that
282 * Finally, the functions do not check for overflow conditions. This
283 * is a sacrifice made for execution speed; since a 32-bit day counter
284 * covers +/- 5,879,610 years, this should not pose a problem here.
289 * ==================================================================
291 * General algorithmic stuff
293 * ==================================================================
297 *---------------------------------------------------------------------
298 * Do a periodic extension of 'value' around 'pivot' with a period of
301 * The result 'res' is a number that holds to the following properties:
303 * 1) res MOD cycle == value MOD cycle
304 * 2) pivot <= res < pivot + cycle
305 * (replace </<= with >/>= for negative cycles)
307 * where 'MOD' denotes the modulo operator for FLOOR DIVISION, which
308 * is not the same as the '%' operator in C: C requires division to be
309 * a truncated division, where remainder and dividend have the same
310 * sign if the remainder is not zero, whereas floor division requires
311 * divider and modulus to have the same sign for a non-zero modulus.
313 * This function has some useful applications:
315 * + let Y be a calendar year and V a truncated 2-digit year: then
316 * periodic_extend(Y-50, V, 100)
317 * is the closest expansion of the truncated year with respect to
318 * the full year, that is a 4-digit year with a difference of less
319 * than 50 years to the year Y. ("century unfolding")
321 * + let T be a UN*X time stamp and V be seconds-of-day: then
322 * perodic_extend(T-43200, V, 86400)
323 * is a time stamp that has the same seconds-of-day as the input
324 * value, with an absolute difference to T of <= 12hrs. ("day
327 * + Wherever you have a truncated periodic value and a non-truncated
328 * base value and you want to match them somehow...
330 * Basically, the function delivers 'pivot + (value - pivot) % cycle',
331 * but the implementation takes some pains to avoid internal signed
332 * integer overflows in the '(value - pivot) % cycle' part and adheres
333 * to the floor division convention.
335 * If 64bit scalars where available on all intended platforms, writing a
336 * version that uses 64 bit ops would be easy; writing a general
337 * division routine for 64bit ops on a platform that can only do
338 * 32/16bit divisions and is still performant is a bit more
339 * difficult. Since most usecases can be coded in a way that does only
340 * require the 32-bit version a 64bit version is NOT provided here.
341 * ---------------------------------------------------------------------
344 ntpcal_periodic_extend(
351 char cpl = 0; /* modulo complement flag */
352 char neg = 0; /* sign change flag */
354 /* make the cycle positive and adjust the flags */
360 /* guard against div by zero or one */
363 * Get absolute difference as unsigned quantity and
364 * the complement flag. This is done by always
365 * subtracting the smaller value from the bigger
366 * one. This implementation works only on a two's
367 * complement machine!
369 if (value >= pivot) {
370 diff = (uint32_t)value - (uint32_t)pivot;
372 diff = (uint32_t)pivot - (uint32_t)value;
375 diff %= (uint32_t)cycle;
388 *-------------------------------------------------------------------
389 * Convert a timestamp in NTP scale to a 64bit seconds value in the UN*X
390 * scale with proper epoch unfolding around a given pivot or the current
391 * system time. This function happily accepts negative pivot values as
392 * timestamps befor 1970-01-01, so be aware of possible trouble on
393 * platforms with 32bit 'time_t'!
395 * This is also a periodic extension, but since the cycle is 2^32 and
396 * the shift is 2^31, we can do some *very* fast math without explicit
398 *-------------------------------------------------------------------
410 res.q_s = (pivot != NULL)
413 res.Q_s -= 0x80000000; /* unshift of half range */
414 ntp -= (uint32_t)JAN_1970; /* warp into UN*X domain */
415 ntp -= res.D_s.lo; /* cycle difference */
416 res.Q_s += (uint64_t)ntp; /* get expanded time */
418 #else /* no 64bit scalars */
422 tmp = (pivot != NULL)
425 res = time_to_vint64(&tmp);
426 M_SUB(res.D_s.hi, res.D_s.lo, 0, 0x80000000);
427 ntp -= (uint32_t)JAN_1970; /* warp into UN*X domain */
428 ntp -= res.D_s.lo; /* cycle difference */
429 M_ADD(res.D_s.hi, res.D_s.lo, 0, ntp);
431 #endif /* no 64bit scalars */
437 *-------------------------------------------------------------------
438 * Convert a timestamp in NTP scale to a 64bit seconds value in the NTP
439 * scale with proper epoch unfolding around a given pivot or the current
442 * Note: The pivot must be given in the UN*X time domain!
444 * This is also a periodic extension, but since the cycle is 2^32 and
445 * the shift is 2^31, we can do some *very* fast math without explicit
447 *-------------------------------------------------------------------
462 res.Q_s -= 0x80000000; /* unshift of half range */
463 res.Q_s += (uint32_t)JAN_1970; /* warp into NTP domain */
464 ntp -= res.D_s.lo; /* cycle difference */
465 res.Q_s += (uint64_t)ntp; /* get expanded time */
467 #else /* no 64bit scalars */
474 res = time_to_vint64(&tmp);
475 M_SUB(res.D_s.hi, res.D_s.lo, 0, 0x80000000u);
476 M_ADD(res.D_s.hi, res.D_s.lo, 0, (uint32_t)JAN_1970);/*into NTP */
477 ntp -= res.D_s.lo; /* cycle difference */
478 M_ADD(res.D_s.hi, res.D_s.lo, 0, ntp);
480 #endif /* no 64bit scalars */
487 * ==================================================================
489 * Splitting values to composite entities
491 * ==================================================================
495 *-------------------------------------------------------------------
496 * Split a 64bit seconds value into elapsed days in 'res.hi' and
497 * elapsed seconds since midnight in 'res.lo' using explicit floor
498 * division. This function happily accepts negative time values as
499 * timestamps before the respective epoch start.
500 * -------------------------------------------------------------------
511 /* manual floor division by SECSPERDAY */
512 res.hi = (int32_t)(ts->q_s / SECSPERDAY);
513 res.lo = (int32_t)(ts->q_s % SECSPERDAY);
516 res.lo += SECSPERDAY;
522 * since we do not have 64bit ops, we have to this by hand.
523 * Luckily SECSPERDAY is 86400 is 675*128, so we do the division
524 * using chained 32/16 bit divisions and shifts.
530 memcpy(&op, ts, sizeof(op));
532 isneg = M_ISNEG(op.D_s.hi);
534 M_NEG(op.D_s.hi, op.D_s.lo);
536 /* save remainder of DIV 128, shift for divide */
537 r = op.D_s.lo & 127; /* save remainder bits */
538 op.D_s.lo = (op.D_s.lo >> 7) | (op.D_s.hi << 25);
539 op.D_s.hi = (op.D_s.hi >> 7);
541 /* now do a mnual division, trying to remove as many ops as
542 * possible -- division is always slow! An since we do not have
543 * the advantage of a specific 64/32 bit or even a specific 32/16
544 * bit division op, but must use the general 32/32bit division
545 * even if we *know* the divider fits into unsigned 16 bits, the
546 * exra code pathes should pay off.
552 a = (a << 16) | op.W_s.lh;
556 a = (a << 16) | op.W_s.ll;
557 q = (q << 16) | (a / 675u);
564 /* assemble remainder */
567 /* fix sign of result */
584 *-------------------------------------------------------------------
585 * Split a 32bit seconds value into h/m/s and excessive days. This
586 * function happily accepts negative time values as timestamps before
588 * -------------------------------------------------------------------
598 /* make sure we have a positive offset into a day */
599 if (ts < 0 || ts >= SECSPERDAY) {
600 days = ts / SECSPERDAY;
601 ts = ts % SECSPERDAY;
608 /* get secs, mins, hours */
609 split[2] = (uint8_t)(ts % SECSPERMIN);
611 split[1] = (uint8_t)(ts % MINSPERHR);
612 split[0] = (uint8_t)(ts / MINSPERHR);
618 * ---------------------------------------------------------------------
619 * Given the number of elapsed days in the calendar era, split this
620 * number into the number of elapsed years in 'res.hi' and the number
621 * of elapsed days of that year in 'res.lo'.
623 * if 'isleapyear' is not NULL, it will receive an integer that is 0 for
624 * regular years and a non-zero value for leap years.
625 *---------------------------------------------------------------------
628 ntpcal_split_eradays(
634 int32_t n400, n100, n004, n001, yday; /* calendar year cycles */
637 * Split off calendar cycles, using floor division in the first
638 * step. After that first step, simple division does it because
639 * all operands are positive; alas, we have to be aware of the
640 * possibe cycle overflows for 100 years and 1 year, caused by
641 * the additional leap day.
643 n400 = days / GREGORIAN_CYCLE_DAYS;
644 yday = days % GREGORIAN_CYCLE_DAYS;
647 yday += GREGORIAN_CYCLE_DAYS;
649 n100 = yday / GREGORIAN_NORMAL_CENTURY_DAYS;
650 yday = yday % GREGORIAN_NORMAL_CENTURY_DAYS;
651 n004 = yday / GREGORIAN_NORMAL_LEAP_CYCLE_DAYS;
652 yday = yday % GREGORIAN_NORMAL_LEAP_CYCLE_DAYS;
653 n001 = yday / DAYSPERYEAR;
654 yday = yday % DAYSPERYEAR;
657 * check for leap cycle overflows and calculate the leap flag
660 if ((n001 | n100) > 3) {
661 /* hit last day of leap year */
666 } else if (isleapyear)
667 *isleapyear = (n001 == 3) && ((n004 != 24) || (n100 == 3));
669 /* now merge the cycles to elapsed years, using horner scheme */
670 res.hi = ((4*n400 + n100)*25 + n004)*4 + n001;
677 *---------------------------------------------------------------------
678 * Given a number of elapsed days in a year and a leap year indicator,
679 * split the number of elapsed days into the number of elapsed months in
680 * 'res.hi' and the number of elapsed days of that month in 'res.lo'.
682 * This function will fail and return {-1,-1} if the number of elapsed
683 * days is not in the valid range!
684 *---------------------------------------------------------------------
687 ntpcal_split_yeardays(
693 const uint16_t *lt; /* month length table */
695 /* check leap year flag and select proper table */
696 lt = real_month_table[(isleapyear != 0)];
697 if (0 <= eyd && eyd < lt[12]) {
698 /* get zero-based month by approximation & correction step */
699 res.hi = eyd >> 5; /* approx month; might be 1 too low */
700 if (lt[res.hi + 1] <= eyd) /* fixup approximative month value */
702 res.lo = eyd - lt[res.hi];
704 res.lo = res.hi = -1;
711 *---------------------------------------------------------------------
712 * Convert a RD into the date part of a 'struct calendar'.
713 *---------------------------------------------------------------------
727 /* get day-of-week first */
728 jd->weekday = rd % 7;
729 if (jd->weekday >= 7) /* unsigned! */
732 split = ntpcal_split_eradays(rd - 1, &leaps);
734 /* get year and day-of-year */
735 jd->year = (uint16_t)split.hi + 1;
736 if (jd->year != split.hi + 1) {
738 retv = -1; /* bletch. overflow trouble. */
740 jd->yearday = (uint16_t)split.lo + 1;
742 /* convert to month and mday */
743 split = ntpcal_split_yeardays(split.lo, leaps);
744 jd->month = (uint8_t)split.hi + 1;
745 jd->monthday = (uint8_t)split.lo + 1;
747 return retv ? retv : leaps;
751 *---------------------------------------------------------------------
752 * Convert a RD into the date part of a 'struct tm'.
753 *---------------------------------------------------------------------
765 /* get day-of-week first */
766 utm->tm_wday = rd % 7;
767 if (utm->tm_wday < 0)
770 /* get year and day-of-year */
771 split = ntpcal_split_eradays(rd - 1, &leaps);
772 utm->tm_year = split.hi - 1899;
773 utm->tm_yday = split.lo; /* 0-based */
775 /* convert to month and mday */
776 split = ntpcal_split_yeardays(split.lo, leaps);
777 utm->tm_mon = split.hi; /* 0-based */
778 utm->tm_mday = split.lo + 1; /* 1-based */
784 *---------------------------------------------------------------------
785 * Take a value of seconds since midnight and split it into hhmmss in a
787 *---------------------------------------------------------------------
790 ntpcal_daysec_to_date(
798 days = priv_timesplit(ts, sec);
799 jd->hour = (uint8_t)ts[0];
800 jd->minute = (uint8_t)ts[1];
801 jd->second = (uint8_t)ts[2];
807 *---------------------------------------------------------------------
808 * Take a value of seconds since midnight and split it into hhmmss in a
810 *---------------------------------------------------------------------
821 days = priv_timesplit(ts, sec);
822 utm->tm_hour = ts[0];
830 *---------------------------------------------------------------------
831 * take a split representation for day/second-of-day and day offset
832 * and convert it to a 'struct calendar'. The seconds will be normalised
833 * into the range of a day, and the day will be adjusted accordingly.
835 * returns >0 if the result is in a leap year, 0 if in a regular
836 * year and <0 if the result did not fit into the calendar struct.
837 *---------------------------------------------------------------------
840 ntpcal_daysplit_to_date(
842 const ntpcal_split *ds,
846 dof += ntpcal_daysec_to_date(jd, ds->lo);
847 return ntpcal_rd_to_date(jd, ds->hi + dof);
851 *---------------------------------------------------------------------
852 * take a split representation for day/second-of-day and day offset
853 * and convert it to a 'struct tm'. The seconds will be normalised
854 * into the range of a day, and the day will be adjusted accordingly.
856 * returns 1 if the result is in a leap year and zero if in a regular
858 *---------------------------------------------------------------------
861 ntpcal_daysplit_to_tm(
863 const ntpcal_split *ds ,
867 dof += ntpcal_daysec_to_tm(utm, ds->lo);
869 return ntpcal_rd_to_tm(utm, ds->hi + dof);
873 *---------------------------------------------------------------------
874 * Take a UN*X time and convert to a calendar structure.
875 *---------------------------------------------------------------------
885 ds = ntpcal_daysplit(ts);
886 ds.hi += ntpcal_daysec_to_date(jd, ds.lo);
887 ds.hi += DAY_UNIX_STARTS;
889 return ntpcal_rd_to_date(jd, ds.hi);
894 * ==================================================================
896 * merging composite entities
898 * ==================================================================
902 *---------------------------------------------------------------------
903 * Merge a number of days and a number of seconds into seconds,
904 * expressed in 64 bits to avoid overflow.
905 *---------------------------------------------------------------------
918 res.q_s *= SECSPERDAY;
927 * res = days *86400 + secs, using manual 16/32 bit
928 * multiplications and shifts.
934 /* assemble days * 675 */
935 res.D_s.lo = (days & 0xFFFF) * 675u;
937 p1 = (days >> 16) * 675u;
940 M_ADD(res.D_s.hi, res.D_s.lo, p2, p1);
942 /* mul by 128, using shift */
943 res.D_s.hi = (res.D_s.hi << 7) | (res.D_s.lo >> 25);
944 res.D_s.lo = (res.D_s.lo << 7);
948 M_NEG(res.D_s.hi, res.D_s.lo);
950 /* properly add seconds */
953 p1 = (uint32_t)-secs;
958 M_ADD(res.D_s.hi, res.D_s.lo, p2, p1);
966 *---------------------------------------------------------------------
967 * Convert elapsed years in Era into elapsed days in Era.
969 * To accomodate for negative values of years, floor division would be
970 * required for all division operations. This can be eased by first
971 * splitting the years into full 400-year cycles and years in the
972 * cycle. Only this operation must be coded as a full floor division; as
973 * the years in the cycle is a non-negative number, all other divisions
974 * can be regular truncated divisions.
975 *---------------------------------------------------------------------
978 ntpcal_days_in_years(
982 int32_t cycle; /* full gregorian cycle */
984 /* split off full calendar cycles, using floor division */
993 * Calculate days in cycle. years now is a non-negative number,
994 * holding the number of years in the 400-year cycle.
996 return cycle * GREGORIAN_CYCLE_DAYS
997 + years * DAYSPERYEAR /* days inregular years */
998 + years / 4 /* 4 year leap rule */
999 - years / 100; /* 100 year leap rule */
1000 /* the 400-year rule does not apply due to full-cycle split-off */
1004 *---------------------------------------------------------------------
1005 * Convert a number of elapsed month in a year into elapsed days in year.
1007 * The month will be normalized, and 'res.hi' will contain the
1008 * excessive years that must be considered when converting the years,
1009 * while 'res.lo' will contain the number of elapsed days since start
1012 * This code uses the shifted-month-approach to convert month to days,
1013 * because then there is no need to have explicit leap year
1014 * information. The slight disadvantage is that for most month values
1015 * the result is a negative value, and the year excess is one; the
1016 * conversion is then simply based on the start of the following year.
1017 *---------------------------------------------------------------------
1020 ntpcal_days_in_months(
1026 /* normalize month into range */
1029 if (res.lo < 0 || res.lo >= 12) {
1030 res.hi = res.lo / 12;
1031 res.lo = res.lo % 12;
1038 /* add 10 month for year starting with march */
1046 /* get cummulated days in year with unshift */
1047 res.lo = shift_month_table[res.lo] - 306;
1053 *---------------------------------------------------------------------
1054 * Convert ELAPSED years/months/days of gregorian calendar to elapsed
1055 * days in Gregorian epoch.
1057 * If you want to convert years and days-of-year, just give a month of
1059 *---------------------------------------------------------------------
1062 ntpcal_edate_to_eradays(
1072 tmp = ntpcal_days_in_months(mons);
1073 res = ntpcal_days_in_years(years + tmp.hi) + tmp.lo;
1075 res = ntpcal_days_in_years(years);
1082 *---------------------------------------------------------------------
1083 * Convert ELAPSED years/months/days of gregorian calendar to elapsed
1086 * Note: This will give the true difference to the start of the given year,
1087 * even if months & days are off-scale.
1088 *---------------------------------------------------------------------
1091 ntpcal_edate_to_yeardays(
1099 if (0 <= mons && mons < 12) {
1101 mdays += real_month_table[is_leapyear(years)][mons];
1103 tmp = ntpcal_days_in_months(mons);
1105 + ntpcal_days_in_years(years + tmp.hi)
1106 - ntpcal_days_in_years(years);
1113 *---------------------------------------------------------------------
1114 * Convert elapsed days and the hour/minute/second information into
1117 * If 'isvalid' is not NULL, do a range check on the time specification
1118 * and tell if the time input is in the normal range, permitting for a
1119 * single leapsecond.
1120 *---------------------------------------------------------------------
1123 ntpcal_etime_to_seconds(
1131 res = (hours * MINSPERHR + minutes) * SECSPERMIN + seconds;
1137 *---------------------------------------------------------------------
1138 * Convert the date part of a 'struct tm' (that is, year, month,
1139 * day-of-month) into the RD of that day.
1140 *---------------------------------------------------------------------
1144 const struct tm *utm
1147 return ntpcal_edate_to_eradays(utm->tm_year + 1899,
1149 utm->tm_mday - 1) + 1;
1153 *---------------------------------------------------------------------
1154 * Convert the date part of a 'struct calendar' (that is, year, month,
1155 * day-of-month) into the RD of that day.
1156 *---------------------------------------------------------------------
1160 const struct calendar *jd
1163 return ntpcal_edate_to_eradays((int32_t)jd->year - 1,
1164 (int32_t)jd->month - 1,
1165 (int32_t)jd->monthday - 1) + 1;
1169 *---------------------------------------------------------------------
1170 * convert a year number to rata die of year start
1171 *---------------------------------------------------------------------
1174 ntpcal_year_to_ystart(
1178 return ntpcal_days_in_years(year - 1) + 1;
1182 *---------------------------------------------------------------------
1183 * For a given RD, get the RD of the associated year start,
1184 * that is, the RD of the last January,1st on or before that day.
1185 *---------------------------------------------------------------------
1188 ntpcal_rd_to_ystart(
1193 * Rather simple exercise: split the day number into elapsed
1194 * years and elapsed days, then remove the elapsed days from the
1195 * input value. Nice'n sweet...
1197 return rd - ntpcal_split_eradays(rd - 1, NULL).lo;
1201 *---------------------------------------------------------------------
1202 * For a given RD, get the RD of the associated month start.
1203 *---------------------------------------------------------------------
1206 ntpcal_rd_to_mstart(
1213 split = ntpcal_split_eradays(rd - 1, &leaps);
1214 split = ntpcal_split_yeardays(split.lo, leaps);
1216 return rd - split.lo;
1220 *---------------------------------------------------------------------
1221 * take a 'struct calendar' and get the seconds-of-day from it.
1222 *---------------------------------------------------------------------
1225 ntpcal_date_to_daysec(
1226 const struct calendar *jd
1229 return ntpcal_etime_to_seconds(jd->hour, jd->minute,
1234 *---------------------------------------------------------------------
1235 * take a 'struct tm' and get the seconds-of-day from it.
1236 *---------------------------------------------------------------------
1239 ntpcal_tm_to_daysec(
1240 const struct tm *utm
1243 return ntpcal_etime_to_seconds(utm->tm_hour, utm->tm_min,
1248 *---------------------------------------------------------------------
1249 * take a 'struct calendar' and convert it to a 'time_t'
1250 *---------------------------------------------------------------------
1253 ntpcal_date_to_time(
1254 const struct calendar *jd
1260 days = ntpcal_date_to_rd(jd) - DAY_UNIX_STARTS;
1261 secs = ntpcal_date_to_daysec(jd);
1262 join = ntpcal_dayjoin(days, secs);
1264 return vint64_to_time(&join);
1269 * ==================================================================
1271 * extended and unchecked variants of caljulian/caltontp
1273 * ==================================================================
1276 ntpcal_ntp64_to_date(
1277 struct calendar *jd,
1283 ds = ntpcal_daysplit(ntp);
1284 ds.hi += ntpcal_daysec_to_date(jd, ds.lo);
1286 return ntpcal_rd_to_date(jd, ds.hi + DAY_NTP_STARTS);
1291 struct calendar *jd,
1299 * Unfold ntp time around current time into NTP domain. Split
1300 * into days and seconds, shift days into CE domain and
1301 * process the parts.
1303 ntp64 = ntpcal_ntp_to_ntp(ntp, piv);
1304 return ntpcal_ntp64_to_date(jd, &ntp64);
1309 ntpcal_date_to_ntp64(
1310 const struct calendar *jd
1314 * Convert date to NTP. Ignore yearday, use d/m/y only.
1316 return ntpcal_dayjoin(ntpcal_date_to_rd(jd) - DAY_NTP_STARTS,
1317 ntpcal_date_to_daysec(jd));
1323 const struct calendar *jd
1327 * Get lower half of 64-bit NTP timestamp from date/time.
1329 return ntpcal_date_to_ntp64(jd).d_s.lo;
1335 * ==================================================================
1337 * day-of-week calculations
1339 * ==================================================================
1342 * Given a RataDie and a day-of-week, calculate a RDN that is reater-than,
1343 * greater-or equal, closest, less-or-equal or less-than the given RDN
1344 * and denotes the given day-of-week
1352 return ntpcal_periodic_extend(rdn+1, dow, 7);
1361 return ntpcal_periodic_extend(rdn, dow, 7);
1365 ntpcal_weekday_close(
1370 return ntpcal_periodic_extend(rdn-3, dow, 7);
1379 return ntpcal_periodic_extend(rdn, dow, -7);
1388 return ntpcal_periodic_extend(rdn-1, dow, -7);
1392 * ==================================================================
1394 * ISO week-calendar conversions
1396 * The ISO8601 calendar defines a calendar of years, weeks and weekdays.
1397 * It is related to the Gregorian calendar, and a ISO year starts at the
1398 * Monday closest to Jan,1st of the corresponding Gregorian year. A ISO
1399 * calendar year has always 52 or 53 weeks, and like the Grogrian
1400 * calendar the ISO8601 calendar repeats itself every 400 years, or
1401 * 146097 days, or 20871 weeks.
1403 * While it is possible to write ISO calendar functions based on the
1404 * Gregorian calendar functions, the following implementation takes a
1405 * different approach, based directly on years and weeks.
1407 * Analysis of the tabulated data shows that it is not possible to
1408 * interpolate from years to weeks over a full 400 year range; cyclic
1409 * shifts over 400 years do not provide a solution here. But it *is*
1410 * possible to interpolate over every single century of the 400-year
1411 * cycle. (The centennial leap year rule seems to be the culprit here.)
1413 * It can be shown that a conversion from years to weeks can be done
1414 * using a linear transformation of the form
1416 * w = floor( y * a + b )
1418 * where the slope a must hold to
1420 * 52.1780821918 <= a < 52.1791044776
1422 * and b must be chosen according to the selected slope and the number
1423 * of the century in a 400-year period.
1425 * The inverse calculation can also be done in this way. Careful scaling
1426 * provides an unlimited set of integer coefficients a,k,b that enable
1427 * us to write the calulation in the form
1429 * w = (y * a + b ) / k
1430 * y = (w * a' + b') / k'
1432 * In this implementation the values of k and k' are chosen to be
1433 * smallest possible powers of two, so the division can be implemented
1434 * as shifts if the optimiser chooses to do so.
1436 * ==================================================================
1440 * Given a number of elapsed (ISO-)years since the begin of the
1441 * christian era, return the number of elapsed weeks corresponding to
1442 * the number of years.
1445 isocal_weeks_in_years(
1450 * use: w = (y * 53431 + b[c]) / 1024 as interpolation
1452 static const int32_t bctab[4] = { 449, 157, 889, 597 };
1453 int32_t cycle; /* full gregorian cycle */
1454 int32_t cents; /* full centuries */
1455 int32_t weeks; /* accumulated weeks */
1457 /* split off full calendar cycles, using floor division */
1458 cycle = years / 400;
1459 years = years % 400;
1465 /* split off full centuries */
1466 cents = years / 100;
1467 years = years % 100;
1470 * calculate elapsed weeks, taking into account that the
1471 * first, third and fourth century have 5218 weeks but the
1472 * second century falls short by one week.
1474 weeks = (years * 53431 + bctab[cents]) / 1024;
1476 return cycle * GREGORIAN_CYCLE_WEEKS
1477 + cents * 5218 - (cents > 1)
1482 * Given a number of elapsed weeks since the begin of the christian
1483 * era, split this number into the number of elapsed years in res.hi
1484 * and the excessive number of weeks in res.lo. (That is, res.lo is
1485 * the number of elapsed weeks in the remaining partial year.)
1488 isocal_split_eraweeks(
1493 * use: y = (w * 157 + b[c]) / 8192 as interpolation
1495 static const int32_t bctab[4] = { 85, 131, 17, 62 };
1500 * split off 400-year cycles, using the fact that a 400-year
1501 * cycle has 146097 days, which is exactly 20871 weeks.
1503 res.hi = weeks / GREGORIAN_CYCLE_WEEKS;
1504 res.lo = weeks % GREGORIAN_CYCLE_WEEKS;
1507 res.lo += GREGORIAN_CYCLE_WEEKS;
1512 * split off centuries, taking into account that the first,
1513 * third and fourth century have 5218 weeks but that the
1514 * second century falls short by one week.
1516 res.lo += (res.lo >= 10435);
1517 cents = res.lo / 5218;
1518 res.lo %= 5218; /* res.lo is weeks in century now */
1520 /* convert elapsed weeks in century to elapsed years and weeks */
1521 res.lo = res.lo * 157 + bctab[cents];
1522 res.hi += cents * 100 + res.lo / 8192;
1523 res.lo = (res.lo % 8192) / 157;
1529 * Given a second in the NTP time scale and a pivot, expand the NTP
1530 * time stamp around the pivot and convert into an ISO calendar time
1534 isocal_ntp64_to_date(
1543 * Split NTP time into days and seconds, shift days into CE
1544 * domain and process the parts.
1546 ds = ntpcal_daysplit(ntp);
1548 /* split time part */
1549 ds.hi += priv_timesplit(ts, ds.lo);
1550 id->hour = (uint8_t)ts[0];
1551 id->minute = (uint8_t)ts[1];
1552 id->second = (uint8_t)ts[2];
1554 /* split date part */
1555 ds.lo = ds.hi + DAY_NTP_STARTS - 1; /* elapsed era days */
1556 ds.hi = ds.lo / 7; /* elapsed era weeks */
1557 ds.lo = ds.lo % 7; /* elapsed week days */
1558 if (ds.lo < 0) { /* floor division! */
1562 id->weekday = (uint8_t)ds.lo + 1; /* weekday result */
1564 ds = isocal_split_eraweeks(ds.hi); /* elapsed years&week*/
1565 id->year = (uint16_t)ds.hi + 1; /* shift to current */
1566 id->week = (uint8_t )ds.lo + 1;
1568 return (ds.hi >= 0 && ds.hi < 0x0000FFFF);
1581 * Unfold ntp time around current time into NTP domain, then
1582 * convert the full time stamp.
1584 ntp64 = ntpcal_ntp_to_ntp(ntp, piv);
1585 return isocal_ntp64_to_date(id, &ntp64);
1589 * Convert a ISO date spec into a second in the NTP time scale,
1590 * properly truncated to 32 bit.
1593 isocal_date_to_ntp64(
1594 const struct isodate *id
1597 int32_t weeks, days, secs;
1599 weeks = isocal_weeks_in_years((int32_t)id->year - 1)
1600 + (int32_t)id->week - 1;
1601 days = weeks * 7 + (int32_t)id->weekday;
1602 /* days is RDN of ISO date now */
1603 secs = ntpcal_etime_to_seconds(id->hour, id->minute, id->second);
1605 return ntpcal_dayjoin(days - DAY_NTP_STARTS, secs);
1610 const struct isodate *id
1614 * Get lower half of 64-bit NTP timestamp from date/time.
1616 return isocal_date_to_ntp64(id).d_s.lo;