2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1988 University of Utah.
5 * Copyright (c) 1982, 1990, 1993
6 * The Regents of the University of California. All rights reserved.
8 * This code is derived from software contributed to Berkeley by
9 * the Systems Programming Group of the University of Utah Computer
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * from: Utah $Hdr: clock.c 1.18 91/01/21$
37 * from: @(#)clock.c 8.2 (Berkeley) 1/12/94
38 * from: NetBSD: clock_subr.c,v 1.6 2001/07/07 17:04:02 thorpej Exp
40 * from: src/sys/i386/isa/clock.c,v 1.176 2001/09/04
43 #include <sys/cdefs.h>
44 __FBSDID("$FreeBSD$");
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/kernel.h>
50 #include <sys/clock.h>
51 #include <sys/limits.h>
52 #include <sys/sysctl.h>
53 #include <sys/timetc.h>
59 * The adjkerntz and wall_cmos_clock sysctls are in the "machdep" sysctl
60 * namespace because they were misplaced there originally.
64 sysctl_machdep_adjkerntz(SYSCTL_HANDLER_ARGS)
67 error = sysctl_handle_int(oidp, oidp->oid_arg1, oidp->oid_arg2, req);
68 if (!error && req->newptr)
72 SYSCTL_PROC(_machdep, OID_AUTO, adjkerntz, CTLTYPE_INT | CTLFLAG_RW |
73 CTLFLAG_MPSAFE, &adjkerntz, 0, sysctl_machdep_adjkerntz, "I",
74 "Local offset from UTC in seconds");
77 SYSCTL_INT(_debug, OID_AUTO, clocktime, CTLFLAG_RWTUN,
78 &ct_debug, 0, "Enable printing of clocktime debugging");
80 static int wall_cmos_clock;
81 SYSCTL_INT(_machdep, OID_AUTO, wall_cmos_clock, CTLFLAG_RW,
82 &wall_cmos_clock, 0, "Enables application of machdep.adjkerntz");
84 /*--------------------------------------------------------------------*
85 * Generic routines to convert between a POSIX date
86 * (seconds since 1/1/1970) and yr/mo/day/hr/min/sec
87 * Derived from NetBSD arch/hp300/hp300/clock.c
92 #define days_in_year(y) (leapyear(y) ? 366 : 365)
93 #define days_in_month(y, m) \
94 (month_days[(m) - 1] + (m == FEBRUARY ? leapyear(y) : 0))
95 /* Day of week. Days are counted from 1/1/1970, which was a Thursday */
96 #define day_of_week(days) (((days) + 4) % 7)
98 static const int month_days[12] = {
99 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
103 * Optimization: using a precomputed count of days between POSIX_BASE_YEAR and
104 * some recent year avoids lots of unnecessary loop iterations in conversion.
105 * recent_base_days is the number of days before the start of recent_base_year.
107 static const int recent_base_year = 2017;
108 static const int recent_base_days = 17167;
111 * Table to 'calculate' pow(10, 9 - nsdigits) via lookup of nsdigits.
112 * Before doing the lookup, the code asserts 0 <= nsdigits <= 9.
114 static u_int nsdivisors[] = {
115 1000000000, 100000000, 10000000, 1000000, 100000, 10000, 1000, 100, 10, 1
119 * This inline avoids some unnecessary modulo operations
120 * as compared with the usual macro:
121 * ( ((year % 4) == 0 &&
122 * (year % 100) != 0) ||
123 * ((year % 400) == 0) )
124 * It is otherwise equivalent.
131 if ((year & 3) == 0) {
133 if ((year % 100) == 0) {
135 if ((year % 400) == 0)
143 clock_ct_to_ts(const struct clocktime *ct, struct timespec *ts)
148 printf("ct_to_ts([");
149 clock_print_ct(ct, 9);
154 * Many realtime clocks store the year as 2-digit BCD; pivot on 70 to
155 * determine century. Some clocks have a "century bit" and drivers do
156 * year += 100, so interpret values between 70-199 as relative to 1900.
165 if (ct->mon < 1 || ct->mon > 12 || ct->day < 1 ||
166 ct->day > days_in_month(year, ct->mon) ||
167 ct->hour > 23 || ct->min > 59 || ct->sec > 59 || year < 1970 ||
168 (sizeof(time_t) == 4 && year > 2037)) { /* time_t overflow */
170 printf(" = EINVAL\n");
175 * Compute days since start of time
176 * First from years, then from months.
178 if (year >= recent_base_year) {
179 i = recent_base_year;
180 days = recent_base_days;
185 for (; i < year; i++)
186 days += days_in_year(i);
189 for (i = 1; i < ct->mon; i++)
190 days += days_in_month(year, i);
191 days += (ct->day - 1);
193 ts->tv_sec = (((time_t)days * 24 + ct->hour) * 60 + ct->min) * 60 +
195 ts->tv_nsec = ct->nsec;
198 printf(" = %jd.%09ld\n", (intmax_t)ts->tv_sec, ts->tv_nsec);
203 clock_bcd_to_ts(const struct bcd_clocktime *bct, struct timespec *ts, bool ampm)
209 * Year may come in as 2-digit or 4-digit BCD. Split the value into
210 * separate BCD century and year values for validation and conversion.
212 bcent = bct->year >> 8;
213 byear = bct->year & 0xff;
216 * Ensure that all values are valid BCD numbers, to avoid assertions in
217 * the BCD-to-binary conversion routines. clock_ct_to_ts() will further
218 * validate the field ranges (such as 0 <= min <= 59) during conversion.
220 if (!validbcd(bcent) || !validbcd(byear) || !validbcd(bct->mon) ||
221 !validbcd(bct->day) || !validbcd(bct->hour) ||
222 !validbcd(bct->min) || !validbcd(bct->sec)) {
224 printf("clock_bcd_to_ts: bad BCD: "
225 "[%04x-%02x-%02x %02x:%02x:%02x]\n",
226 bct->year, bct->mon, bct->day,
227 bct->hour, bct->min, bct->sec);
231 ct.year = FROMBCD(byear) + FROMBCD(bcent) * 100;
232 ct.mon = FROMBCD(bct->mon);
233 ct.day = FROMBCD(bct->day);
234 ct.hour = FROMBCD(bct->hour);
235 ct.min = FROMBCD(bct->min);
236 ct.sec = FROMBCD(bct->sec);
240 /* If asked to handle am/pm, convert from 12hr+pmflag to 24hr. */
248 return (clock_ct_to_ts(&ct, ts));
252 clock_ts_to_ct(const struct timespec *ts, struct clocktime *ct)
254 time_t i, year, days;
255 time_t rsec; /* remainder seconds */
259 days = secs / SECDAY;
260 rsec = secs % SECDAY;
262 ct->dow = day_of_week(days);
264 /* Subtract out whole years. */
265 if (days >= recent_base_days) {
266 year = recent_base_year;
267 days -= recent_base_days;
269 year = POSIX_BASE_YEAR;
271 for (; days >= days_in_year(year); year++)
272 days -= days_in_year(year);
275 /* Subtract out whole months, counting them in i. */
276 for (i = 1; days >= days_in_month(year, i); i++)
277 days -= days_in_month(year, i);
280 /* Days are what is left over (+1) from all that. */
283 /* Hours, minutes, seconds are easy */
284 ct->hour = rsec / 3600;
289 ct->nsec = ts->tv_nsec;
291 printf("ts_to_ct(%jd.%09ld) = [",
292 (intmax_t)ts->tv_sec, ts->tv_nsec);
293 clock_print_ct(ct, 9);
297 KASSERT(ct->year >= 0 && ct->year < 10000,
298 ("year %d isn't a 4 digit year", ct->year));
299 KASSERT(ct->mon >= 1 && ct->mon <= 12,
300 ("month %d not in 1-12", ct->mon));
301 KASSERT(ct->day >= 1 && ct->day <= 31,
302 ("day %d not in 1-31", ct->day));
303 KASSERT(ct->hour >= 0 && ct->hour <= 23,
304 ("hour %d not in 0-23", ct->hour));
305 KASSERT(ct->min >= 0 && ct->min <= 59,
306 ("minute %d not in 0-59", ct->min));
307 /* Not sure if this interface needs to handle leapseconds or not. */
308 KASSERT(ct->sec >= 0 && ct->sec <= 60,
309 ("seconds %d not in 0-60", ct->sec));
313 clock_ts_to_bcd(const struct timespec *ts, struct bcd_clocktime *bct, bool ampm)
317 clock_ts_to_ct(ts, &ct);
319 /* If asked to handle am/pm, convert from 24hr to 12hr+pmflag. */
330 bct->year = TOBCD(ct.year % 100) | (TOBCD(ct.year / 100) << 8);
331 bct->mon = TOBCD(ct.mon);
332 bct->day = TOBCD(ct.day);
333 bct->hour = TOBCD(ct.hour);
334 bct->min = TOBCD(ct.min);
335 bct->sec = TOBCD(ct.sec);
341 clock_print_bcd(const struct bcd_clocktime *bct, int nsdigits)
344 KASSERT(nsdigits >= 0 && nsdigits <= 9, ("bad nsdigits %d", nsdigits));
347 printf("%4.4x-%2.2x-%2.2x %2.2x:%2.2x:%2.2x.%*.*ld",
348 bct->year, bct->mon, bct->day,
349 bct->hour, bct->min, bct->sec,
350 nsdigits, nsdigits, bct->nsec / nsdivisors[nsdigits]);
352 printf("%4.4x-%2.2x-%2.2x %2.2x:%2.2x:%2.2x",
353 bct->year, bct->mon, bct->day,
354 bct->hour, bct->min, bct->sec);
359 clock_print_ct(const struct clocktime *ct, int nsdigits)
362 KASSERT(nsdigits >= 0 && nsdigits <= 9, ("bad nsdigits %d", nsdigits));
365 printf("%04d-%02d-%02d %02d:%02d:%02d.%*.*ld",
366 ct->year, ct->mon, ct->day,
367 ct->hour, ct->min, ct->sec,
368 nsdigits, nsdigits, ct->nsec / nsdivisors[nsdigits]);
370 printf("%04d-%02d-%02d %02d:%02d:%02d",
371 ct->year, ct->mon, ct->day,
372 ct->hour, ct->min, ct->sec);
377 clock_print_ts(const struct timespec *ts, int nsdigits)
381 clock_ts_to_ct(ts, &ct);
382 clock_print_ct(&ct, nsdigits);
389 return (tz_minuteswest * 60 + (wall_cmos_clock ? adjkerntz : 0));