Index: head/sys/kern/subr_clock.c =================================================================== --- head/sys/kern/subr_clock.c (revision 328955) +++ head/sys/kern/subr_clock.c (revision 328956) @@ -1,345 +1,345 @@ /*- * SPDX-License-Identifier: BSD-3-Clause * * Copyright (c) 1988 University of Utah. * Copyright (c) 1982, 1990, 1993 * The Regents of the University of California. All rights reserved. * * This code is derived from software contributed to Berkeley by * the Systems Programming Group of the University of Utah Computer * Science Department. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * from: Utah $Hdr: clock.c 1.18 91/01/21$ * from: @(#)clock.c 8.2 (Berkeley) 1/12/94 * from: NetBSD: clock_subr.c,v 1.6 2001/07/07 17:04:02 thorpej Exp * and * from: src/sys/i386/isa/clock.c,v 1.176 2001/09/04 */ #include __FBSDID("$FreeBSD$"); #include #include #include #include #include #include #include #include int tz_minuteswest; int tz_dsttime; /* * The adjkerntz and wall_cmos_clock sysctls are in the "machdep" sysctl * namespace because they were misplaced there originally. */ static int adjkerntz; static int sysctl_machdep_adjkerntz(SYSCTL_HANDLER_ARGS) { int error; error = sysctl_handle_int(oidp, oidp->oid_arg1, oidp->oid_arg2, req); if (!error && req->newptr) resettodr(); return (error); } SYSCTL_PROC(_machdep, OID_AUTO, adjkerntz, CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, &adjkerntz, 0, sysctl_machdep_adjkerntz, "I", "Local offset from UTC in seconds"); static int ct_debug; SYSCTL_INT(_debug, OID_AUTO, clocktime, CTLFLAG_RWTUN, &ct_debug, 0, "Enable printing of clocktime debugging"); static int wall_cmos_clock; SYSCTL_INT(_machdep, OID_AUTO, wall_cmos_clock, CTLFLAG_RW, &wall_cmos_clock, 0, "Enables application of machdep.adjkerntz"); /*--------------------------------------------------------------------* * Generic routines to convert between a POSIX date * (seconds since 1/1/1970) and yr/mo/day/hr/min/sec * Derived from NetBSD arch/hp300/hp300/clock.c */ #define FEBRUARY 2 #define days_in_year(y) (leapyear(y) ? 366 : 365) #define days_in_month(y, m) \ (month_days[(m) - 1] + (m == FEBRUARY ? leapyear(y) : 0)) /* Day of week. Days are counted from 1/1/1970, which was a Thursday */ #define day_of_week(days) (((days) + 4) % 7) static const int month_days[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; /* * Optimization: using a precomputed count of days between POSIX_BASE_YEAR and * some recent year avoids lots of unnecessary loop iterations in conversion. * recent_base_days is the number of days before the start of recent_base_year. */ static const int recent_base_year = 2017; static const int recent_base_days = 17167; /* * This inline avoids some unnecessary modulo operations * as compared with the usual macro: * ( ((year % 4) == 0 && * (year % 100) != 0) || * ((year % 400) == 0) ) * It is otherwise equivalent. */ static int leapyear(int year) { int rv = 0; if ((year & 3) == 0) { rv = 1; if ((year % 100) == 0) { rv = 0; if ((year % 400) == 0) rv = 1; } } return (rv); } static void -print_ct(struct clocktime *ct) +print_ct(const struct clocktime *ct) { printf("[%04d-%02d-%02d %02d:%02d:%02d]", ct->year, ct->mon, ct->day, ct->hour, ct->min, ct->sec); } int -clock_ct_to_ts(struct clocktime *ct, struct timespec *ts) +clock_ct_to_ts(const struct clocktime *ct, struct timespec *ts) { int i, year, days; if (ct_debug) { printf("ct_to_ts("); print_ct(ct); printf(")"); } /* * Many realtime clocks store the year as 2-digit BCD; pivot on 70 to * determine century. Some clocks have a "century bit" and drivers do * year += 100, so interpret values between 70-199 as relative to 1900. */ year = ct->year; if (year < 70) year += 2000; else if (year < 200) year += 1900; /* Sanity checks. */ if (ct->mon < 1 || ct->mon > 12 || ct->day < 1 || ct->day > days_in_month(year, ct->mon) || ct->hour > 23 || ct->min > 59 || ct->sec > 59 || year < 1970 || (sizeof(time_t) == 4 && year > 2037)) { /* time_t overflow */ if (ct_debug) printf(" = EINVAL\n"); return (EINVAL); } /* * Compute days since start of time * First from years, then from months. */ if (year >= recent_base_year) { i = recent_base_year; days = recent_base_days; } else { i = POSIX_BASE_YEAR; days = 0; } for (; i < year; i++) days += days_in_year(i); /* Months */ for (i = 1; i < ct->mon; i++) days += days_in_month(year, i); days += (ct->day - 1); ts->tv_sec = (((time_t)days * 24 + ct->hour) * 60 + ct->min) * 60 + ct->sec; ts->tv_nsec = ct->nsec; if (ct_debug) printf(" = %jd.%09ld\n", (intmax_t)ts->tv_sec, ts->tv_nsec); return (0); } int -clock_bcd_to_ts(struct bcd_clocktime *bct, struct timespec *ts, bool ampm) +clock_bcd_to_ts(const struct bcd_clocktime *bct, struct timespec *ts, bool ampm) { struct clocktime ct; int bcent, byear; /* * Year may come in as 2-digit or 4-digit BCD. Split the value into * separate BCD century and year values for validation and conversion. */ bcent = bct->year >> 8; byear = bct->year & 0xff; /* * Ensure that all values are valid BCD numbers, to avoid assertions in * the BCD-to-binary conversion routines. clock_ct_to_ts() will further * validate the field ranges (such as 0 <= min <= 59) during conversion. */ if (!validbcd(bcent) || !validbcd(byear) || !validbcd(bct->mon) || !validbcd(bct->day) || !validbcd(bct->hour) || !validbcd(bct->min) || !validbcd(bct->sec)) { if (ct_debug) printf("clock_bcd_to_ts: bad BCD: " "[%04x-%02x-%02x %02x:%02x:%02x]\n", bct->year, bct->mon, bct->day, bct->hour, bct->min, bct->sec); return (EINVAL); } ct.year = FROMBCD(byear) + FROMBCD(bcent) * 100; ct.mon = FROMBCD(bct->mon); ct.day = FROMBCD(bct->day); ct.hour = FROMBCD(bct->hour); ct.min = FROMBCD(bct->min); ct.sec = FROMBCD(bct->sec); ct.dow = bct->dow; ct.nsec = bct->nsec; /* If asked to handle am/pm, convert from 12hr+pmflag to 24hr. */ if (ampm) { if (ct.hour == 12) ct.hour = 0; if (bct->ispm) ct.hour += 12; } return (clock_ct_to_ts(&ct, ts)); } void -clock_ts_to_ct(struct timespec *ts, struct clocktime *ct) +clock_ts_to_ct(const struct timespec *ts, struct clocktime *ct) { time_t i, year, days; time_t rsec; /* remainder seconds */ time_t secs; secs = ts->tv_sec; days = secs / SECDAY; rsec = secs % SECDAY; ct->dow = day_of_week(days); /* Subtract out whole years. */ if (days >= recent_base_days) { year = recent_base_year; days -= recent_base_days; } else { year = POSIX_BASE_YEAR; } for (; days >= days_in_year(year); year++) days -= days_in_year(year); ct->year = year; /* Subtract out whole months, counting them in i. */ for (i = 1; days >= days_in_month(year, i); i++) days -= days_in_month(year, i); ct->mon = i; /* Days are what is left over (+1) from all that. */ ct->day = days + 1; /* Hours, minutes, seconds are easy */ ct->hour = rsec / 3600; rsec = rsec % 3600; ct->min = rsec / 60; rsec = rsec % 60; ct->sec = rsec; ct->nsec = ts->tv_nsec; if (ct_debug) { printf("ts_to_ct(%jd.%09ld) = ", (intmax_t)ts->tv_sec, ts->tv_nsec); print_ct(ct); printf("\n"); } KASSERT(ct->year >= 0 && ct->year < 10000, ("year %d isn't a 4 digit year", ct->year)); KASSERT(ct->mon >= 1 && ct->mon <= 12, ("month %d not in 1-12", ct->mon)); KASSERT(ct->day >= 1 && ct->day <= 31, ("day %d not in 1-31", ct->day)); KASSERT(ct->hour >= 0 && ct->hour <= 23, ("hour %d not in 0-23", ct->hour)); KASSERT(ct->min >= 0 && ct->min <= 59, ("minute %d not in 0-59", ct->min)); /* Not sure if this interface needs to handle leapseconds or not. */ KASSERT(ct->sec >= 0 && ct->sec <= 60, ("seconds %d not in 0-60", ct->sec)); } void -clock_ts_to_bcd(struct timespec *ts, struct bcd_clocktime *bct, bool ampm) +clock_ts_to_bcd(const struct timespec *ts, struct bcd_clocktime *bct, bool ampm) { struct clocktime ct; clock_ts_to_ct(ts, &ct); /* If asked to handle am/pm, convert from 24hr to 12hr+pmflag. */ bct->ispm = false; if (ampm) { if (ct.hour >= 12) { ct.hour -= 12; bct->ispm = true; } if (ct.hour == 0) ct.hour = 12; } bct->year = TOBCD(ct.year % 100) | (TOBCD(ct.year / 100) << 8); bct->mon = TOBCD(ct.mon); bct->day = TOBCD(ct.day); bct->hour = TOBCD(ct.hour); bct->min = TOBCD(ct.min); bct->sec = TOBCD(ct.sec); bct->dow = ct.dow; bct->nsec = ct.nsec; } int utc_offset(void) { return (tz_minuteswest * 60 + (wall_cmos_clock ? adjkerntz : 0)); } Index: head/sys/kern/subr_fattime.c =================================================================== --- head/sys/kern/subr_fattime.c (revision 328955) +++ head/sys/kern/subr_fattime.c (revision 328956) @@ -1,309 +1,311 @@ /*- * SPDX-License-Identifier: BSD-2-Clause-FreeBSD * * Copyright (c) 2006 Poul-Henning Kamp * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * $FreeBSD$ * * Convert MS-DOS FAT format timestamps to and from unix timespecs * * FAT filestamps originally consisted of two 16 bit integers, encoded like * this: * * yyyyyyymmmmddddd (year - 1980, month, day) * * hhhhhmmmmmmsssss (hour, minutes, seconds divided by two) * * Subsequently even Microsoft realized that files could be accessed in less * than two seconds and a byte was added containing: * * sfffffff (second mod two, 100ths of second) * * FAT timestamps are in the local timezone, with no indication of which * timezone much less if daylight savings time applies. * * Later on again, in Windows NT, timestamps were defined relative to GMT. * * Purists will point out that UTC replaced GMT for such uses around * half a century ago, already then. Ironically "NT" was an abbreviation of * "New Technology". Anyway... * * The 'utc' argument determines if the resulting FATTIME timestamp * should be on the UTC or local timezone calendar. * * The conversion functions below cut time into four-year leap-year * cycles rather than single years and uses table lookups inside those * cycles to get the months and years sorted out. * * Obviously we cannot calculate the correct table index going from * a posix seconds count to Y/M/D, but we can get pretty close by * dividing the daycount by 32 (giving a too low index), and then * adjusting upwards a couple of steps if necessary. * * FAT timestamps have 7 bits for the year and starts at 1980, so * they can represent up to 2107 which means that the non-leap-year * 2100 must be handled. * * XXX: As long as time_t is 32 bits this is not relevant or easily * XXX: testable. Revisit when time_t grows bigger. * XXX: grepfodder: 64 bit time_t, y2100, y2.1k, 2100, leap year * */ #include #include #include #include #define DAY (24 * 60 * 60) /* Length of day in seconds */ #define YEAR 365 /* Length of normal year */ #define LYC (4 * YEAR + 1) /* Length of 4 year leap-year cycle */ #define T1980 (10 * 365 + 2) /* Days from 1970 to 1980 */ /* End of month is N days from start of (normal) year */ #define JAN 31 #define FEB (JAN + 28) #define MAR (FEB + 31) #define APR (MAR + 30) #define MAY (APR + 31) #define JUN (MAY + 30) #define JUL (JUN + 31) #define AUG (JUL + 31) #define SEP (AUG + 30) #define OCT (SEP + 31) #define NOV (OCT + 30) #define DEC (NOV + 31) /* Table of months in a 4 year leap-year cycle */ #define ENC(y,m) (((y) << 9) | ((m) << 5)) static const struct { uint16_t days; /* month start in days relative to cycle */ uint16_t coded; /* encoded year + month information */ } mtab[48] = { { 0 + 0 * YEAR, ENC(0, 1) }, { JAN + 0 * YEAR, ENC(0, 2) }, { FEB + 0 * YEAR + 1, ENC(0, 3) }, { MAR + 0 * YEAR + 1, ENC(0, 4) }, { APR + 0 * YEAR + 1, ENC(0, 5) }, { MAY + 0 * YEAR + 1, ENC(0, 6) }, { JUN + 0 * YEAR + 1, ENC(0, 7) }, { JUL + 0 * YEAR + 1, ENC(0, 8) }, { AUG + 0 * YEAR + 1, ENC(0, 9) }, { SEP + 0 * YEAR + 1, ENC(0, 10) }, { OCT + 0 * YEAR + 1, ENC(0, 11) }, { NOV + 0 * YEAR + 1, ENC(0, 12) }, { DEC + 0 * YEAR + 1, ENC(1, 1) }, { JAN + 1 * YEAR + 1, ENC(1, 2) }, { FEB + 1 * YEAR + 1, ENC(1, 3) }, { MAR + 1 * YEAR + 1, ENC(1, 4) }, { APR + 1 * YEAR + 1, ENC(1, 5) }, { MAY + 1 * YEAR + 1, ENC(1, 6) }, { JUN + 1 * YEAR + 1, ENC(1, 7) }, { JUL + 1 * YEAR + 1, ENC(1, 8) }, { AUG + 1 * YEAR + 1, ENC(1, 9) }, { SEP + 1 * YEAR + 1, ENC(1, 10) }, { OCT + 1 * YEAR + 1, ENC(1, 11) }, { NOV + 1 * YEAR + 1, ENC(1, 12) }, { DEC + 1 * YEAR + 1, ENC(2, 1) }, { JAN + 2 * YEAR + 1, ENC(2, 2) }, { FEB + 2 * YEAR + 1, ENC(2, 3) }, { MAR + 2 * YEAR + 1, ENC(2, 4) }, { APR + 2 * YEAR + 1, ENC(2, 5) }, { MAY + 2 * YEAR + 1, ENC(2, 6) }, { JUN + 2 * YEAR + 1, ENC(2, 7) }, { JUL + 2 * YEAR + 1, ENC(2, 8) }, { AUG + 2 * YEAR + 1, ENC(2, 9) }, { SEP + 2 * YEAR + 1, ENC(2, 10) }, { OCT + 2 * YEAR + 1, ENC(2, 11) }, { NOV + 2 * YEAR + 1, ENC(2, 12) }, { DEC + 2 * YEAR + 1, ENC(3, 1) }, { JAN + 3 * YEAR + 1, ENC(3, 2) }, { FEB + 3 * YEAR + 1, ENC(3, 3) }, { MAR + 3 * YEAR + 1, ENC(3, 4) }, { APR + 3 * YEAR + 1, ENC(3, 5) }, { MAY + 3 * YEAR + 1, ENC(3, 6) }, { JUN + 3 * YEAR + 1, ENC(3, 7) }, { JUL + 3 * YEAR + 1, ENC(3, 8) }, { AUG + 3 * YEAR + 1, ENC(3, 9) }, { SEP + 3 * YEAR + 1, ENC(3, 10) }, { OCT + 3 * YEAR + 1, ENC(3, 11) }, { NOV + 3 * YEAR + 1, ENC(3, 12) } }; void -timespec2fattime(struct timespec *tsp, int utc, uint16_t *ddp, uint16_t *dtp, uint8_t *dhp) +timespec2fattime(const struct timespec *tsp, int utc, uint16_t *ddp, + uint16_t *dtp, uint8_t *dhp) { time_t t1; unsigned t2, l, m; t1 = tsp->tv_sec; if (!utc) t1 -= utc_offset(); if (dhp != NULL) *dhp = (tsp->tv_sec & 1) * 100 + tsp->tv_nsec / 10000000; if (dtp != NULL) { *dtp = (t1 / 2) % 30; *dtp |= ((t1 / 60) % 60) << 5; *dtp |= ((t1 / 3600) % 24) << 11; } if (ddp != NULL) { t2 = t1 / DAY; if (t2 < T1980) { /* Impossible date, truncate to 1980-01-01 */ *ddp = 0x0021; } else { t2 -= T1980; /* * 2100 is not a leap year. * XXX: a 32 bit time_t can not get us here. */ if (t2 >= ((2100 - 1980) / 4 * LYC + FEB)) t2++; /* Account for full leapyear cycles */ l = t2 / LYC; *ddp = (l * 4) << 9; t2 -= l * LYC; /* Find approximate table entry */ m = t2 / 32; /* Find correct table entry */ while (m < 47 && mtab[m + 1].days <= t2) m++; /* Get year + month from the table */ *ddp += mtab[m].coded; /* And apply the day in the month */ t2 -= mtab[m].days - 1; *ddp |= t2; } } } /* * Table indexed by the bottom two bits of year + four bits of the month * from the FAT timestamp, returning number of days into 4 year long * leap-year cycle */ #define DCOD(m, y, l) ((m) + YEAR * (y) + (l)) static const uint16_t daytab[64] = { 0, DCOD( 0, 0, 0), DCOD(JAN, 0, 0), DCOD(FEB, 0, 1), DCOD(MAR, 0, 1), DCOD(APR, 0, 1), DCOD(MAY, 0, 1), DCOD(JUN, 0, 1), DCOD(JUL, 0, 1), DCOD(AUG, 0, 1), DCOD(SEP, 0, 1), DCOD(OCT, 0, 1), DCOD(NOV, 0, 1), DCOD(DEC, 0, 1), 0, 0, 0, DCOD( 0, 1, 1), DCOD(JAN, 1, 1), DCOD(FEB, 1, 1), DCOD(MAR, 1, 1), DCOD(APR, 1, 1), DCOD(MAY, 1, 1), DCOD(JUN, 1, 1), DCOD(JUL, 1, 1), DCOD(AUG, 1, 1), DCOD(SEP, 1, 1), DCOD(OCT, 1, 1), DCOD(NOV, 1, 1), DCOD(DEC, 1, 1), 0, 0, 0, DCOD( 0, 2, 1), DCOD(JAN, 2, 1), DCOD(FEB, 2, 1), DCOD(MAR, 2, 1), DCOD(APR, 2, 1), DCOD(MAY, 2, 1), DCOD(JUN, 2, 1), DCOD(JUL, 2, 1), DCOD(AUG, 2, 1), DCOD(SEP, 2, 1), DCOD(OCT, 2, 1), DCOD(NOV, 2, 1), DCOD(DEC, 2, 1), 0, 0, 0, DCOD( 0, 3, 1), DCOD(JAN, 3, 1), DCOD(FEB, 3, 1), DCOD(MAR, 3, 1), DCOD(APR, 3, 1), DCOD(MAY, 3, 1), DCOD(JUN, 3, 1), DCOD(JUL, 3, 1), DCOD(AUG, 3, 1), DCOD(SEP, 3, 1), DCOD(OCT, 3, 1), DCOD(NOV, 3, 1), DCOD(DEC, 3, 1), 0, 0 }; void -fattime2timespec(unsigned dd, unsigned dt, unsigned dh, int utc, struct timespec *tsp) +fattime2timespec(unsigned dd, unsigned dt, unsigned dh, int utc, + struct timespec *tsp) { unsigned day; /* Unpack time fields */ tsp->tv_sec = (dt & 0x1f) << 1; tsp->tv_sec += ((dt & 0x7e0) >> 5) * 60; tsp->tv_sec += ((dt & 0xf800) >> 11) * 3600; tsp->tv_sec += dh / 100; tsp->tv_nsec = (dh % 100) * 10000000; /* Day of month */ day = (dd & 0x1f) - 1; /* Full leap-year cycles */ day += LYC * ((dd >> 11) & 0x1f); /* Month offset from leap-year cycle */ day += daytab[(dd >> 5) & 0x3f]; /* * 2100 is not a leap year. * XXX: a 32 bit time_t can not get us here. */ if (day >= ((2100 - 1980) / 4 * LYC + FEB)) day--; /* Align with time_t epoch */ day += T1980; tsp->tv_sec += DAY * day; if (!utc) tsp->tv_sec += utc_offset(); } #ifdef TEST_DRIVER #include #include #include int main(int argc __unused, char **argv __unused) { int i; struct timespec ts; struct tm tm; double a; uint16_t d, t; uint8_t p; char buf[100]; for (i = 0; i < 10000; i++) { do { ts.tv_sec = random(); } while (ts.tv_sec < T1980 * 86400); ts.tv_nsec = random() % 1000000000; printf("%10d.%03ld -- ", ts.tv_sec, ts.tv_nsec / 1000000); gmtime_r(&ts.tv_sec, &tm); strftime(buf, sizeof buf, "%Y %m %d %H %M %S", &tm); printf("%s -- ", buf); a = ts.tv_sec + ts.tv_nsec * 1e-9; d = t = p = 0; timet2fattime(&ts, &d, &t, &p); printf("%04x %04x %02x -- ", d, t, p); printf("%3d %02d %02d %02d %02d %02d -- ", ((d >> 9) & 0x7f) + 1980, (d >> 5) & 0x0f, (d >> 0) & 0x1f, (t >> 11) & 0x1f, (t >> 5) & 0x3f, ((t >> 0) & 0x1f) * 2); ts.tv_sec = ts.tv_nsec = 0; fattime2timet(d, t, p, &ts); printf("%10d.%03ld == ", ts.tv_sec, ts.tv_nsec / 1000000); gmtime_r(&ts.tv_sec, &tm); strftime(buf, sizeof buf, "%Y %m %d %H %M %S", &tm); printf("%s -- ", buf); a -= ts.tv_sec + ts.tv_nsec * 1e-9; printf("%.3f", a); printf("\n"); } return (0); } #endif /* TEST_DRIVER */ Index: head/sys/sys/clock.h =================================================================== --- head/sys/sys/clock.h (revision 328955) +++ head/sys/sys/clock.h (revision 328956) @@ -1,185 +1,187 @@ /*- * SPDX-License-Identifier: BSD-2-Clause-FreeBSD * * Copyright (c) 1996 The NetBSD Foundation, Inc. * All rights reserved. * * This code is derived from software contributed to The NetBSD Foundation * by Gordon W. Ross * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. * * $NetBSD: clock_subr.h,v 1.7 2000/10/03 13:41:07 tsutsui Exp $ * * * This file is the central clearing-house for calendrical issues. * * In general the kernel does not know about minutes, hours, days, timezones, * daylight savings time, leap-years and such. All that is theoretically a * matter for userland only. * * Parts of kernel code does however care: badly designed filesystems store * timestamps in local time and RTC chips sometimes track time in a local * timezone instead of UTC and so on. * * All that code should go here for service. * * $FreeBSD$ */ #ifndef _SYS_CLOCK_H_ #define _SYS_CLOCK_H_ #ifdef _KERNEL /* No user serviceable parts */ /* * Timezone info from settimeofday(2), usually not used */ extern int tz_minuteswest; extern int tz_dsttime; int utc_offset(void); /* * Structure to hold the values typically reported by time-of-day clocks, * expressed as binary integers (see below for a BCD version). This can be * passed to the conversion functions to be converted to/from a struct timespec. * * On input, the year is interpreted as follows: * 0 - 69 = 2000 - 2069 * 70 - 99 = 1970 - 1999 * 100 - 199 = 2000 - 2099 (Supports hardware "century bit".) * 200 - 1969 = Invalid. * 1970 - 9999 = Full 4-digit century+year. * * The dow field is ignored (not even validated) on input, but is always * populated with day-of-week on output. * * clock_ct_to_ts() returns EINVAL if any values are out of range. The year * field will always be 4-digit on output. */ struct clocktime { int year; /* year (4 digit year) */ int mon; /* month (1 - 12) */ int day; /* day (1 - 31) */ int hour; /* hour (0 - 23) */ int min; /* minute (0 - 59) */ int sec; /* second (0 - 59) */ int dow; /* day of week (0 - 6; 0 = Sunday) */ long nsec; /* nano seconds */ }; -int clock_ct_to_ts(struct clocktime *, struct timespec *); -void clock_ts_to_ct(struct timespec *, struct clocktime *); +int clock_ct_to_ts(const struct clocktime *, struct timespec *); +void clock_ts_to_ct(const struct timespec *, struct clocktime *); /* * Structure to hold the values typically reported by time-of-day clocks, * expressed as BCD. This can be passed to the conversion functions to be * converted to/from a struct timespec. * * The clock_bcd_to_ts() function interprets the values in the year through sec * fields as BCD numbers, and returns EINVAL if any BCD values are out of range. * After conversion to binary, the values are passed to clock_ct_to_ts() and * undergo further validation as described above. Year may be 2 or 4-digit BCD, * interpreted as described above. The nsec field is binary. If the ampm arg * is true, the incoming hour and ispm values are interpreted as 12-hour am/pm * representation of the hour, otherwise hour is interpreted as 24-hour and ispm * is ignored. * * The clock_ts_to_bcd() function converts the timespec to BCD values stored * into year through sec. The value in year will be 4-digit BCD (e.g., * 0x2017). The mon through sec values will be 2-digit BCD. The nsec field will * be binary, and the range of dow makes its binary and BCD values identical. * If the ampm arg is true, the hour and ispm fields are set to the 12-hour * time plus a pm flag, otherwise the hour is set to 24-hour time and ispm is * set to false. */ struct bcd_clocktime { uint16_t year; /* year (2 or 4 digit year) */ uint8_t mon; /* month (1 - 12) */ uint8_t day; /* day (1 - 31) */ uint8_t hour; /* hour (0 - 23 or 1 - 12) */ uint8_t min; /* minute (0 - 59) */ uint8_t sec; /* second (0 - 59) */ uint8_t dow; /* day of week (0 - 6; 0 = Sunday) */ long nsec; /* nanoseconds */ bool ispm; /* true if hour represents pm time */ }; -int clock_bcd_to_ts(struct bcd_clocktime *, struct timespec *, bool ampm); -void clock_ts_to_bcd(struct timespec *, struct bcd_clocktime *, bool ampm); +int clock_bcd_to_ts(const struct bcd_clocktime *, struct timespec *, bool ampm); +void clock_ts_to_bcd(const struct timespec *, struct bcd_clocktime *, bool ampm); /* * Time-of-day clock functions and flags. These functions might sleep. * * clock_register and clock_unregister() do what they say. Upon return from * unregister, the clock's methods are not running and will not be called again. * * clock_schedule() requests that a registered clock's clock_settime() calls * happen at the given offset into the second. The default is 0, meaning no * specific scheduling. To schedule the call as soon after top-of-second as * possible, specify 1. Each clock has its own schedule, but taskqueue_thread * is shared by many tasks; the timing of the call is not guaranteed. * * Flags: * * CLOCKF_SETTIME_NO_TS * Do not pass a timespec to clock_settime(), the driver obtains its own time * and applies its own adjustments (this flag implies CLOCKF_SETTIME_NO_ADJ). * * CLOCKF_SETTIME_NO_ADJ * Do not apply utc offset and resolution/accuracy adjustments to the value * passed to clock_settime(), the driver applies them itself. * * CLOCKF_GETTIME_NO_ADJ * Do not apply utc offset and resolution/accuracy adjustments to the value * returned from clock_gettime(), the driver has already applied them. */ #define CLOCKF_SETTIME_NO_TS 0x00000001 #define CLOCKF_SETTIME_NO_ADJ 0x00000002 #define CLOCKF_GETTIME_NO_ADJ 0x00000004 void clock_register(device_t _clockdev, long _resolution_us); void clock_register_flags(device_t _clockdev, long _resolution_us, int _flags); void clock_schedule(device_t clockdev, u_int _offsetns); void clock_unregister(device_t _clockdev); /* * BCD to decimal and decimal to BCD. */ #define FROMBCD(x) bcd2bin(x) #define TOBCD(x) bin2bcd(x) /* Some handy constants. */ #define SECDAY (24 * 60 * 60) #define SECYR (SECDAY * 365) /* Traditional POSIX base year */ #define POSIX_BASE_YEAR 1970 -void timespec2fattime(struct timespec *tsp, int utc, u_int16_t *ddp, u_int16_t *dtp, u_int8_t *dhp); -void fattime2timespec(unsigned dd, unsigned dt, unsigned dh, int utc, struct timespec *tsp); +void timespec2fattime(const struct timespec *tsp, int utc, u_int16_t *ddp, + u_int16_t *dtp, u_int8_t *dhp); +void fattime2timespec(unsigned dd, unsigned dt, unsigned dh, int utc, + struct timespec *tsp); #endif /* _KERNEL */ #endif /* !_SYS_CLOCK_H_ */