xref: /freebsd/sys/kern/subr_clock.c (revision 329f0aa95244ad1e615e8c2abb50366b5b957bdf)
19454b2d8SWarner Losh /*-
251369649SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
351369649SPedro F. Giffuni  *
4d7f7792eSThomas Moestl  * Copyright (c) 1988 University of Utah.
5d7f7792eSThomas Moestl  * Copyright (c) 1982, 1990, 1993
6d7f7792eSThomas Moestl  *	The Regents of the University of California.  All rights reserved.
7d7f7792eSThomas Moestl  *
8d7f7792eSThomas Moestl  * This code is derived from software contributed to Berkeley by
9d7f7792eSThomas Moestl  * the Systems Programming Group of the University of Utah Computer
10d7f7792eSThomas Moestl  * Science Department.
11d7f7792eSThomas Moestl  *
12d7f7792eSThomas Moestl  * Redistribution and use in source and binary forms, with or without
13d7f7792eSThomas Moestl  * modification, are permitted provided that the following conditions
14d7f7792eSThomas Moestl  * are met:
15d7f7792eSThomas Moestl  * 1. Redistributions of source code must retain the above copyright
16d7f7792eSThomas Moestl  *    notice, this list of conditions and the following disclaimer.
17d7f7792eSThomas Moestl  * 2. Redistributions in binary form must reproduce the above copyright
18d7f7792eSThomas Moestl  *    notice, this list of conditions and the following disclaimer in the
19d7f7792eSThomas Moestl  *    documentation and/or other materials provided with the distribution.
2069a28758SEd Maste  * 3. Neither the name of the University nor the names of its contributors
21d7f7792eSThomas Moestl  *    may be used to endorse or promote products derived from this software
22d7f7792eSThomas Moestl  *    without specific prior written permission.
23d7f7792eSThomas Moestl  *
24d7f7792eSThomas Moestl  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25d7f7792eSThomas Moestl  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26d7f7792eSThomas Moestl  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27d7f7792eSThomas Moestl  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28d7f7792eSThomas Moestl  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29d7f7792eSThomas Moestl  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30d7f7792eSThomas Moestl  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31d7f7792eSThomas Moestl  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32d7f7792eSThomas Moestl  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33d7f7792eSThomas Moestl  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34d7f7792eSThomas Moestl  * SUCH DAMAGE.
35d7f7792eSThomas Moestl  *
36d7f7792eSThomas Moestl  *	from: Utah $Hdr: clock.c 1.18 91/01/21$
37d7f7792eSThomas Moestl  *	from: @(#)clock.c	8.2 (Berkeley) 1/12/94
38d7f7792eSThomas Moestl  *	from: NetBSD: clock_subr.c,v 1.6 2001/07/07 17:04:02 thorpej Exp
39d7f7792eSThomas Moestl  *	and
40d7f7792eSThomas Moestl  *	from: src/sys/i386/isa/clock.c,v 1.176 2001/09/04
41d7f7792eSThomas Moestl  */
42d7f7792eSThomas Moestl 
43677b542eSDavid E. O'Brien #include <sys/cdefs.h>
44677b542eSDavid E. O'Brien __FBSDID("$FreeBSD$");
45677b542eSDavid E. O'Brien 
46d7f7792eSThomas Moestl #include <sys/param.h>
47d7f7792eSThomas Moestl #include <sys/systm.h>
48d7f7792eSThomas Moestl #include <sys/kernel.h>
49d7f7792eSThomas Moestl #include <sys/bus.h>
50d7f7792eSThomas Moestl #include <sys/clock.h>
51ee9bc599SBrooks Davis #include <sys/limits.h>
52d7f7792eSThomas Moestl #include <sys/sysctl.h>
53d7f7792eSThomas Moestl #include <sys/timetc.h>
54d7f7792eSThomas Moestl 
55d7f7792eSThomas Moestl /*
568d12fab9SBjoern A. Zeeb  * The adjkerntz and wall_cmos_clock sysctls are in the "machdep" sysctl
578d12fab9SBjoern A. Zeeb  * namespace because they were misplaced there originally.
58d7f7792eSThomas Moestl  */
598d12fab9SBjoern A. Zeeb static int adjkerntz;
60d7f7792eSThomas Moestl static int
61d7f7792eSThomas Moestl sysctl_machdep_adjkerntz(SYSCTL_HANDLER_ARGS)
62d7f7792eSThomas Moestl {
63d7f7792eSThomas Moestl 	int error;
649b4a8ab7SPoul-Henning Kamp 	error = sysctl_handle_int(oidp, oidp->oid_arg1, oidp->oid_arg2, req);
65d7f7792eSThomas Moestl 	if (!error && req->newptr)
66d7f7792eSThomas Moestl 		resettodr();
67d7f7792eSThomas Moestl 	return (error);
68d7f7792eSThomas Moestl }
693a0e6f92SKonstantin Belousov SYSCTL_PROC(_machdep, OID_AUTO, adjkerntz, CTLTYPE_INT | CTLFLAG_RW |
703a0e6f92SKonstantin Belousov     CTLFLAG_MPSAFE, &adjkerntz, 0, sysctl_machdep_adjkerntz, "I",
71b389be97SRebecca Cran     "Local offset from UTC in seconds");
72b69f71ebSPoul-Henning Kamp 
738d12fab9SBjoern A. Zeeb static int ct_debug;
74932d14c6SIan Lepore SYSCTL_INT(_debug, OID_AUTO, clocktime, CTLFLAG_RWTUN,
758d12fab9SBjoern A. Zeeb     &ct_debug, 0, "Enable printing of clocktime debugging");
768d12fab9SBjoern A. Zeeb 
778d12fab9SBjoern A. Zeeb static int wall_cmos_clock;
788d12fab9SBjoern A. Zeeb SYSCTL_INT(_machdep, OID_AUTO, wall_cmos_clock, CTLFLAG_RW,
798d12fab9SBjoern A. Zeeb     &wall_cmos_clock, 0, "Enables application of machdep.adjkerntz");
808d12fab9SBjoern A. Zeeb 
81b69f71ebSPoul-Henning Kamp /*--------------------------------------------------------------------*
82b69f71ebSPoul-Henning Kamp  * Generic routines to convert between a POSIX date
83b69f71ebSPoul-Henning Kamp  * (seconds since 1/1/1970) and yr/mo/day/hr/min/sec
84b69f71ebSPoul-Henning Kamp  * Derived from NetBSD arch/hp300/hp300/clock.c
85b69f71ebSPoul-Henning Kamp  */
86b69f71ebSPoul-Henning Kamp 
87b69f71ebSPoul-Henning Kamp 
88b69f71ebSPoul-Henning Kamp #define	FEBRUARY	2
89b69f71ebSPoul-Henning Kamp #define	days_in_year(y) 	(leapyear(y) ? 366 : 365)
90b69f71ebSPoul-Henning Kamp #define	days_in_month(y, m) \
91b69f71ebSPoul-Henning Kamp 	(month_days[(m) - 1] + (m == FEBRUARY ? leapyear(y) : 0))
92b69f71ebSPoul-Henning Kamp /* Day of week. Days are counted from 1/1/1970, which was a Thursday */
93b69f71ebSPoul-Henning Kamp #define	day_of_week(days)	(((days) + 4) % 7)
94b69f71ebSPoul-Henning Kamp 
95b69f71ebSPoul-Henning Kamp static const int month_days[12] = {
96b69f71ebSPoul-Henning Kamp 	31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
97b69f71ebSPoul-Henning Kamp };
98b69f71ebSPoul-Henning Kamp 
99f7afe767SIan Lepore /*
100f7afe767SIan Lepore  * Optimization: using a precomputed count of days between POSIX_BASE_YEAR and
101f7afe767SIan Lepore  * some recent year avoids lots of unnecessary loop iterations in conversion.
102f7afe767SIan Lepore  * recent_base_days is the number of days before the start of recent_base_year.
103f7afe767SIan Lepore  */
104f7afe767SIan Lepore static const int recent_base_year = 2017;
105f7afe767SIan Lepore static const int recent_base_days = 17167;
106b69f71ebSPoul-Henning Kamp 
107d7f7792eSThomas Moestl /*
108aedc51f1SIan Lepore  * Table to 'calculate' pow(10, 9 - nsdigits) via lookup of nsdigits.
109aedc51f1SIan Lepore  * Before doing the lookup, the code asserts 0 <= nsdigits <= 9.
110aedc51f1SIan Lepore  */
111aedc51f1SIan Lepore static u_int nsdivisors[] = {
112aedc51f1SIan Lepore     1000000000, 100000000, 10000000, 1000000, 100000, 10000, 1000, 100, 10, 1
113aedc51f1SIan Lepore };
114aedc51f1SIan Lepore 
115aedc51f1SIan Lepore /*
116d7f7792eSThomas Moestl  * This inline avoids some unnecessary modulo operations
117d7f7792eSThomas Moestl  * as compared with the usual macro:
118d7f7792eSThomas Moestl  *   ( ((year % 4) == 0 &&
119d7f7792eSThomas Moestl  *      (year % 100) != 0) ||
120d7f7792eSThomas Moestl  *     ((year % 400) == 0) )
121d7f7792eSThomas Moestl  * It is otherwise equivalent.
122d7f7792eSThomas Moestl  */
123c9ad6040SPoul-Henning Kamp static int
124d7f7792eSThomas Moestl leapyear(int year)
125d7f7792eSThomas Moestl {
126d7f7792eSThomas Moestl 	int rv = 0;
127d7f7792eSThomas Moestl 
128d7f7792eSThomas Moestl 	if ((year & 3) == 0) {
129d7f7792eSThomas Moestl 		rv = 1;
130d7f7792eSThomas Moestl 		if ((year % 100) == 0) {
131d7f7792eSThomas Moestl 			rv = 0;
132d7f7792eSThomas Moestl 			if ((year % 400) == 0)
133d7f7792eSThomas Moestl 				rv = 1;
134d7f7792eSThomas Moestl 		}
135d7f7792eSThomas Moestl 	}
136d7f7792eSThomas Moestl 	return (rv);
137d7f7792eSThomas Moestl }
138d7f7792eSThomas Moestl 
139d7f7792eSThomas Moestl int
140ce75945dSIan Lepore clock_ct_to_ts(const struct clocktime *ct, struct timespec *ts)
141d7f7792eSThomas Moestl {
142d7f7792eSThomas Moestl 	int i, year, days;
143d7f7792eSThomas Moestl 
144c9ad6040SPoul-Henning Kamp 	if (ct_debug) {
145aedc51f1SIan Lepore 		printf("ct_to_ts([");
146aedc51f1SIan Lepore 		clock_print_ct(ct, 9);
147aedc51f1SIan Lepore 		printf("])");
148c9ad6040SPoul-Henning Kamp 	}
149c9ad6040SPoul-Henning Kamp 
150f1b21e2cSIan Lepore 	/*
151f1b21e2cSIan Lepore 	 * Many realtime clocks store the year as 2-digit BCD; pivot on 70 to
152f1b21e2cSIan Lepore 	 * determine century.  Some clocks have a "century bit" and drivers do
153f1b21e2cSIan Lepore 	 * year += 100, so interpret values between 70-199 as relative to 1900.
154f1b21e2cSIan Lepore 	 */
155f1b21e2cSIan Lepore 	year = ct->year;
156f1b21e2cSIan Lepore 	if (year < 70)
157f1b21e2cSIan Lepore 		year += 2000;
158f1b21e2cSIan Lepore 	else if (year < 200)
159f1b21e2cSIan Lepore 		year += 1900;
160f1b21e2cSIan Lepore 
161d7f7792eSThomas Moestl 	/* Sanity checks. */
162d7f7792eSThomas Moestl 	if (ct->mon < 1 || ct->mon > 12 || ct->day < 1 ||
163d7f7792eSThomas Moestl 	    ct->day > days_in_month(year, ct->mon) ||
164f1b21e2cSIan Lepore 	    ct->hour > 23 ||  ct->min > 59 || ct->sec > 59 || year < 1970 ||
165ee9bc599SBrooks Davis 	    (sizeof(time_t) == 4 && year > 2037)) {	/* time_t overflow */
166c9ad6040SPoul-Henning Kamp 		if (ct_debug)
167c9ad6040SPoul-Henning Kamp 			printf(" = EINVAL\n");
168d7f7792eSThomas Moestl 		return (EINVAL);
169c9ad6040SPoul-Henning Kamp 	}
170d7f7792eSThomas Moestl 
171d7f7792eSThomas Moestl 	/*
172d7f7792eSThomas Moestl 	 * Compute days since start of time
173d7f7792eSThomas Moestl 	 * First from years, then from months.
174d7f7792eSThomas Moestl 	 */
175f7afe767SIan Lepore 	if (year >= recent_base_year) {
176f7afe767SIan Lepore 		i = recent_base_year;
177f7afe767SIan Lepore 		days = recent_base_days;
178f7afe767SIan Lepore 	} else {
179f7afe767SIan Lepore 		i = POSIX_BASE_YEAR;
180d7f7792eSThomas Moestl 		days = 0;
181f7afe767SIan Lepore 	}
182f7afe767SIan Lepore 	for (; i < year; i++)
183d7f7792eSThomas Moestl 		days += days_in_year(i);
184d7f7792eSThomas Moestl 
185d7f7792eSThomas Moestl 	/* Months */
186d7f7792eSThomas Moestl 	for (i = 1; i < ct->mon; i++)
187d7f7792eSThomas Moestl 	  	days += days_in_month(year, i);
188d7f7792eSThomas Moestl 	days += (ct->day - 1);
189d7f7792eSThomas Moestl 
190fe21241eSKonstantin Belousov 	ts->tv_sec = (((time_t)days * 24 + ct->hour) * 60 + ct->min) * 60 +
191fe21241eSKonstantin Belousov 	    ct->sec;
192d7f7792eSThomas Moestl 	ts->tv_nsec = ct->nsec;
193fe21241eSKonstantin Belousov 
194c9ad6040SPoul-Henning Kamp 	if (ct_debug)
195932d14c6SIan Lepore 		printf(" = %jd.%09ld\n", (intmax_t)ts->tv_sec, ts->tv_nsec);
196d7f7792eSThomas Moestl 	return (0);
197d7f7792eSThomas Moestl }
198d7f7792eSThomas Moestl 
19986299375SIan Lepore int
200ce75945dSIan Lepore clock_bcd_to_ts(const struct bcd_clocktime *bct, struct timespec *ts, bool ampm)
20186299375SIan Lepore {
20286299375SIan Lepore 	struct clocktime ct;
20386299375SIan Lepore 	int bcent, byear;
20486299375SIan Lepore 
20586299375SIan Lepore 	/*
20686299375SIan Lepore 	 * Year may come in as 2-digit or 4-digit BCD.  Split the value into
20786299375SIan Lepore 	 * separate BCD century and year values for validation and conversion.
20886299375SIan Lepore 	 */
20986299375SIan Lepore 	bcent = bct->year >> 8;
21086299375SIan Lepore 	byear = bct->year & 0xff;
21186299375SIan Lepore 
21286299375SIan Lepore 	/*
21386299375SIan Lepore 	 * Ensure that all values are valid BCD numbers, to avoid assertions in
21486299375SIan Lepore 	 * the BCD-to-binary conversion routines.  clock_ct_to_ts() will further
21586299375SIan Lepore 	 * validate the field ranges (such as 0 <= min <= 59) during conversion.
21686299375SIan Lepore 	 */
21786299375SIan Lepore 	if (!validbcd(bcent) || !validbcd(byear) || !validbcd(bct->mon) ||
21886299375SIan Lepore 	    !validbcd(bct->day) || !validbcd(bct->hour) ||
21986299375SIan Lepore 	    !validbcd(bct->min) || !validbcd(bct->sec)) {
22086299375SIan Lepore 		if (ct_debug)
22186299375SIan Lepore 			printf("clock_bcd_to_ts: bad BCD: "
22286299375SIan Lepore 			    "[%04x-%02x-%02x %02x:%02x:%02x]\n",
22386299375SIan Lepore 			    bct->year, bct->mon, bct->day,
22486299375SIan Lepore 			    bct->hour, bct->min, bct->sec);
22586299375SIan Lepore 		return (EINVAL);
22686299375SIan Lepore 	}
22786299375SIan Lepore 
22886299375SIan Lepore 	ct.year = FROMBCD(byear) + FROMBCD(bcent) * 100;
22986299375SIan Lepore 	ct.mon  = FROMBCD(bct->mon);
23086299375SIan Lepore 	ct.day  = FROMBCD(bct->day);
23186299375SIan Lepore 	ct.hour = FROMBCD(bct->hour);
23286299375SIan Lepore 	ct.min  = FROMBCD(bct->min);
23386299375SIan Lepore 	ct.sec  = FROMBCD(bct->sec);
23486299375SIan Lepore 	ct.dow  = bct->dow;
23586299375SIan Lepore 	ct.nsec = bct->nsec;
23686299375SIan Lepore 
23786299375SIan Lepore 	/* If asked to handle am/pm, convert from 12hr+pmflag to 24hr. */
23886299375SIan Lepore 	if (ampm) {
23986299375SIan Lepore 		if (ct.hour == 12)
24086299375SIan Lepore 			ct.hour = 0;
24186299375SIan Lepore 		if (bct->ispm)
24286299375SIan Lepore 			ct.hour += 12;
24386299375SIan Lepore 	}
24486299375SIan Lepore 
24586299375SIan Lepore 	return (clock_ct_to_ts(&ct, ts));
24686299375SIan Lepore }
24786299375SIan Lepore 
248d7f7792eSThomas Moestl void
249ce75945dSIan Lepore clock_ts_to_ct(const struct timespec *ts, struct clocktime *ct)
250d7f7792eSThomas Moestl {
25190a79ac5SConrad Meyer 	time_t i, year, days;
252d7f7792eSThomas Moestl 	time_t rsec;	/* remainder seconds */
253d7f7792eSThomas Moestl 	time_t secs;
254d7f7792eSThomas Moestl 
255d7f7792eSThomas Moestl 	secs = ts->tv_sec;
256d7f7792eSThomas Moestl 	days = secs / SECDAY;
257d7f7792eSThomas Moestl 	rsec = secs % SECDAY;
258d7f7792eSThomas Moestl 
259d7f7792eSThomas Moestl 	ct->dow = day_of_week(days);
260d7f7792eSThomas Moestl 
261f7afe767SIan Lepore 	/* Subtract out whole years. */
262f7afe767SIan Lepore 	if (days >= recent_base_days) {
263f7afe767SIan Lepore 		year = recent_base_year;
264f7afe767SIan Lepore 		days -= recent_base_days;
265f7afe767SIan Lepore 	} else {
266f7afe767SIan Lepore 		year = POSIX_BASE_YEAR;
267f7afe767SIan Lepore 	}
268f7afe767SIan Lepore 	for (; days >= days_in_year(year); year++)
269d7f7792eSThomas Moestl 		days -= days_in_year(year);
270d7f7792eSThomas Moestl 	ct->year = year;
271d7f7792eSThomas Moestl 
272d7f7792eSThomas Moestl 	/* Subtract out whole months, counting them in i. */
273d7f7792eSThomas Moestl 	for (i = 1; days >= days_in_month(year, i); i++)
274d7f7792eSThomas Moestl 		days -= days_in_month(year, i);
275d7f7792eSThomas Moestl 	ct->mon = i;
276d7f7792eSThomas Moestl 
277d7f7792eSThomas Moestl 	/* Days are what is left over (+1) from all that. */
278d7f7792eSThomas Moestl 	ct->day = days + 1;
279d7f7792eSThomas Moestl 
280d7f7792eSThomas Moestl 	/* Hours, minutes, seconds are easy */
281d7f7792eSThomas Moestl 	ct->hour = rsec / 3600;
282d7f7792eSThomas Moestl 	rsec = rsec % 3600;
283d7f7792eSThomas Moestl 	ct->min  = rsec / 60;
284d7f7792eSThomas Moestl 	rsec = rsec % 60;
285d7f7792eSThomas Moestl 	ct->sec  = rsec;
286d7f7792eSThomas Moestl 	ct->nsec = ts->tv_nsec;
287c9ad6040SPoul-Henning Kamp 	if (ct_debug) {
288aedc51f1SIan Lepore 		printf("ts_to_ct(%jd.%09ld) = [",
289932d14c6SIan Lepore 		    (intmax_t)ts->tv_sec, ts->tv_nsec);
290aedc51f1SIan Lepore 		clock_print_ct(ct, 9);
291aedc51f1SIan Lepore 		printf("]\n");
292c9ad6040SPoul-Henning Kamp 	}
29390a79ac5SConrad Meyer 
29490a79ac5SConrad Meyer 	KASSERT(ct->year >= 0 && ct->year < 10000,
29590a79ac5SConrad Meyer 	    ("year %d isn't a 4 digit year", ct->year));
29690a79ac5SConrad Meyer 	KASSERT(ct->mon >= 1 && ct->mon <= 12,
29790a79ac5SConrad Meyer 	    ("month %d not in 1-12", ct->mon));
29890a79ac5SConrad Meyer 	KASSERT(ct->day >= 1 && ct->day <= 31,
29990a79ac5SConrad Meyer 	    ("day %d not in 1-31", ct->day));
30090a79ac5SConrad Meyer 	KASSERT(ct->hour >= 0 && ct->hour <= 23,
30190a79ac5SConrad Meyer 	    ("hour %d not in 0-23", ct->hour));
30290a79ac5SConrad Meyer 	KASSERT(ct->min >= 0 && ct->min <= 59,
30390a79ac5SConrad Meyer 	    ("minute %d not in 0-59", ct->min));
30490a79ac5SConrad Meyer 	/* Not sure if this interface needs to handle leapseconds or not. */
30590a79ac5SConrad Meyer 	KASSERT(ct->sec >= 0 && ct->sec <= 60,
30690a79ac5SConrad Meyer 	    ("seconds %d not in 0-60", ct->sec));
307d7f7792eSThomas Moestl }
308f97c1c4bSPoul-Henning Kamp 
30986299375SIan Lepore void
310ce75945dSIan Lepore clock_ts_to_bcd(const struct timespec *ts, struct bcd_clocktime *bct, bool ampm)
31186299375SIan Lepore {
31286299375SIan Lepore 	struct clocktime ct;
31386299375SIan Lepore 
31486299375SIan Lepore 	clock_ts_to_ct(ts, &ct);
31586299375SIan Lepore 
31686299375SIan Lepore 	/* If asked to handle am/pm, convert from 24hr to 12hr+pmflag. */
31786299375SIan Lepore 	bct->ispm = false;
31886299375SIan Lepore 	if (ampm) {
31986299375SIan Lepore 		if (ct.hour >= 12) {
32086299375SIan Lepore 			ct.hour -= 12;
32186299375SIan Lepore 			bct->ispm = true;
32286299375SIan Lepore 		}
32386299375SIan Lepore 		if (ct.hour == 0)
32486299375SIan Lepore 			ct.hour = 12;
32586299375SIan Lepore 	}
32686299375SIan Lepore 
32786299375SIan Lepore 	bct->year = TOBCD(ct.year % 100) | (TOBCD(ct.year / 100) << 8);
32886299375SIan Lepore 	bct->mon  = TOBCD(ct.mon);
32986299375SIan Lepore 	bct->day  = TOBCD(ct.day);
33086299375SIan Lepore 	bct->hour = TOBCD(ct.hour);
33186299375SIan Lepore 	bct->min  = TOBCD(ct.min);
33286299375SIan Lepore 	bct->sec  = TOBCD(ct.sec);
33386299375SIan Lepore 	bct->dow  = ct.dow;
33486299375SIan Lepore 	bct->nsec = ct.nsec;
33586299375SIan Lepore }
33686299375SIan Lepore 
337aedc51f1SIan Lepore void
338aedc51f1SIan Lepore clock_print_bcd(const struct bcd_clocktime *bct, int nsdigits)
339aedc51f1SIan Lepore {
340aedc51f1SIan Lepore 
341aedc51f1SIan Lepore 	KASSERT(nsdigits >= 0 && nsdigits <= 9, ("bad nsdigits %d", nsdigits));
342aedc51f1SIan Lepore 
343aedc51f1SIan Lepore 	if (nsdigits > 0) {
344aedc51f1SIan Lepore 		printf("%4.4x-%2.2x-%2.2x %2.2x:%2.2x:%2.2x.%*.*ld",
345aedc51f1SIan Lepore 		    bct->year, bct->mon, bct->day,
346aedc51f1SIan Lepore 		    bct->hour, bct->min, bct->sec,
347aedc51f1SIan Lepore 		    nsdigits, nsdigits, bct->nsec / nsdivisors[nsdigits]);
348aedc51f1SIan Lepore 	} else {
349aedc51f1SIan Lepore 		printf("%4.4x-%2.2x-%2.2x %2.2x:%2.2x:%2.2x",
350aedc51f1SIan Lepore 		    bct->year, bct->mon, bct->day,
351aedc51f1SIan Lepore 		    bct->hour, bct->min, bct->sec);
352aedc51f1SIan Lepore 	}
353aedc51f1SIan Lepore }
354aedc51f1SIan Lepore 
355aedc51f1SIan Lepore void
356aedc51f1SIan Lepore clock_print_ct(const struct clocktime *ct, int nsdigits)
357aedc51f1SIan Lepore {
358aedc51f1SIan Lepore 
359aedc51f1SIan Lepore 	KASSERT(nsdigits >= 0 && nsdigits <= 9, ("bad nsdigits %d", nsdigits));
360aedc51f1SIan Lepore 
361aedc51f1SIan Lepore 	if (nsdigits > 0) {
362aedc51f1SIan Lepore 		printf("%04d-%02d-%02d %02d:%02d:%02d.%*.*ld",
363aedc51f1SIan Lepore 		    ct->year, ct->mon, ct->day,
364aedc51f1SIan Lepore 		    ct->hour, ct->min, ct->sec,
365aedc51f1SIan Lepore 		    nsdigits, nsdigits, ct->nsec / nsdivisors[nsdigits]);
366aedc51f1SIan Lepore 	} else {
367aedc51f1SIan Lepore 		printf("%04d-%02d-%02d %02d:%02d:%02d",
368aedc51f1SIan Lepore 		    ct->year, ct->mon, ct->day,
369aedc51f1SIan Lepore 		    ct->hour, ct->min, ct->sec);
370aedc51f1SIan Lepore 	}
371aedc51f1SIan Lepore }
372aedc51f1SIan Lepore 
373aedc51f1SIan Lepore void
374aedc51f1SIan Lepore clock_print_ts(const struct timespec *ts, int nsdigits)
375aedc51f1SIan Lepore {
376aedc51f1SIan Lepore 	struct clocktime ct;
377aedc51f1SIan Lepore 
378aedc51f1SIan Lepore 	clock_ts_to_ct(ts, &ct);
379aedc51f1SIan Lepore 	clock_print_ct(&ct, nsdigits);
380aedc51f1SIan Lepore }
381aedc51f1SIan Lepore 
382f97c1c4bSPoul-Henning Kamp int
383f97c1c4bSPoul-Henning Kamp utc_offset(void)
384f97c1c4bSPoul-Henning Kamp {
385f97c1c4bSPoul-Henning Kamp 
386*329f0aa9SWarner Losh 	return (wall_cmos_clock ? adjkerntz : 0);
387f97c1c4bSPoul-Henning Kamp }
388