xref: /freebsd/sys/kern/subr_clock.c (revision fdafd315ad0d0f28a11b9fb4476a9ab059c62b92)
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: NetBSD: clock_subr.c,v 1.6 2001/07/07 17:04:02 thorpej Exp
38d7f7792eSThomas Moestl  *	and
39d7f7792eSThomas Moestl  *	from: src/sys/i386/isa/clock.c,v 1.176 2001/09/04
40d7f7792eSThomas Moestl  */
41d7f7792eSThomas Moestl 
42d7f7792eSThomas Moestl #include <sys/param.h>
43d7f7792eSThomas Moestl #include <sys/systm.h>
44d7f7792eSThomas Moestl #include <sys/kernel.h>
45d7f7792eSThomas Moestl #include <sys/bus.h>
46d7f7792eSThomas Moestl #include <sys/clock.h>
47ee9bc599SBrooks Davis #include <sys/limits.h>
48d7f7792eSThomas Moestl #include <sys/sysctl.h>
49d7f7792eSThomas Moestl #include <sys/timetc.h>
50d7f7792eSThomas Moestl 
51d7f7792eSThomas Moestl /*
528d12fab9SBjoern A. Zeeb  * The adjkerntz and wall_cmos_clock sysctls are in the "machdep" sysctl
538d12fab9SBjoern A. Zeeb  * namespace because they were misplaced there originally.
54d7f7792eSThomas Moestl  */
558d12fab9SBjoern A. Zeeb static int adjkerntz;
56d7f7792eSThomas Moestl static int
sysctl_machdep_adjkerntz(SYSCTL_HANDLER_ARGS)57d7f7792eSThomas Moestl sysctl_machdep_adjkerntz(SYSCTL_HANDLER_ARGS)
58d7f7792eSThomas Moestl {
59d7f7792eSThomas Moestl 	int error;
609b4a8ab7SPoul-Henning Kamp 	error = sysctl_handle_int(oidp, oidp->oid_arg1, oidp->oid_arg2, req);
61d7f7792eSThomas Moestl 	if (!error && req->newptr)
62d7f7792eSThomas Moestl 		resettodr();
63d7f7792eSThomas Moestl 	return (error);
64d7f7792eSThomas Moestl }
653a0e6f92SKonstantin Belousov SYSCTL_PROC(_machdep, OID_AUTO, adjkerntz, CTLTYPE_INT | CTLFLAG_RW |
663a0e6f92SKonstantin Belousov     CTLFLAG_MPSAFE, &adjkerntz, 0, sysctl_machdep_adjkerntz, "I",
67b389be97SRebecca Cran     "Local offset from UTC in seconds");
68b69f71ebSPoul-Henning Kamp 
698d12fab9SBjoern A. Zeeb static int ct_debug;
70932d14c6SIan Lepore SYSCTL_INT(_debug, OID_AUTO, clocktime, CTLFLAG_RWTUN,
718d12fab9SBjoern A. Zeeb     &ct_debug, 0, "Enable printing of clocktime debugging");
728d12fab9SBjoern A. Zeeb 
738d12fab9SBjoern A. Zeeb static int wall_cmos_clock;
748d12fab9SBjoern A. Zeeb SYSCTL_INT(_machdep, OID_AUTO, wall_cmos_clock, CTLFLAG_RW,
758d12fab9SBjoern A. Zeeb     &wall_cmos_clock, 0, "Enables application of machdep.adjkerntz");
768d12fab9SBjoern A. Zeeb 
77b69f71ebSPoul-Henning Kamp /*--------------------------------------------------------------------*
78b69f71ebSPoul-Henning Kamp  * Generic routines to convert between a POSIX date
79b69f71ebSPoul-Henning Kamp  * (seconds since 1/1/1970) and yr/mo/day/hr/min/sec
80b69f71ebSPoul-Henning Kamp  * Derived from NetBSD arch/hp300/hp300/clock.c
81b69f71ebSPoul-Henning Kamp  */
82b69f71ebSPoul-Henning Kamp 
83b69f71ebSPoul-Henning Kamp #define	FEBRUARY	2
84b69f71ebSPoul-Henning Kamp #define	days_in_year(y) 	(leapyear(y) ? 366 : 365)
85b69f71ebSPoul-Henning Kamp #define	days_in_month(y, m) \
86b69f71ebSPoul-Henning Kamp 	(month_days[(m) - 1] + (m == FEBRUARY ? leapyear(y) : 0))
87b69f71ebSPoul-Henning Kamp /* Day of week. Days are counted from 1/1/1970, which was a Thursday */
88b69f71ebSPoul-Henning Kamp #define	day_of_week(days)	(((days) + 4) % 7)
89b69f71ebSPoul-Henning Kamp 
90b69f71ebSPoul-Henning Kamp static const int month_days[12] = {
91b69f71ebSPoul-Henning Kamp 	31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
92b69f71ebSPoul-Henning Kamp };
93b69f71ebSPoul-Henning Kamp 
94f7afe767SIan Lepore /*
95f7afe767SIan Lepore  * Optimization: using a precomputed count of days between POSIX_BASE_YEAR and
96f7afe767SIan Lepore  * some recent year avoids lots of unnecessary loop iterations in conversion.
97f7afe767SIan Lepore  * recent_base_days is the number of days before the start of recent_base_year.
98f7afe767SIan Lepore  */
99f7afe767SIan Lepore static const int recent_base_year = 2017;
100f7afe767SIan Lepore static const int recent_base_days = 17167;
101b69f71ebSPoul-Henning Kamp 
102d7f7792eSThomas Moestl /*
103aedc51f1SIan Lepore  * Table to 'calculate' pow(10, 9 - nsdigits) via lookup of nsdigits.
104aedc51f1SIan Lepore  * Before doing the lookup, the code asserts 0 <= nsdigits <= 9.
105aedc51f1SIan Lepore  */
106aedc51f1SIan Lepore static u_int nsdivisors[] = {
107aedc51f1SIan Lepore     1000000000, 100000000, 10000000, 1000000, 100000, 10000, 1000, 100, 10, 1
108aedc51f1SIan Lepore };
109aedc51f1SIan Lepore 
110aedc51f1SIan Lepore /*
111d7f7792eSThomas Moestl  * This inline avoids some unnecessary modulo operations
112d7f7792eSThomas Moestl  * as compared with the usual macro:
113d7f7792eSThomas Moestl  *   ( ((year % 4) == 0 &&
114d7f7792eSThomas Moestl  *      (year % 100) != 0) ||
115d7f7792eSThomas Moestl  *     ((year % 400) == 0) )
116d7f7792eSThomas Moestl  * It is otherwise equivalent.
117d7f7792eSThomas Moestl  */
118c9ad6040SPoul-Henning Kamp static int
leapyear(int year)119d7f7792eSThomas Moestl leapyear(int year)
120d7f7792eSThomas Moestl {
121d7f7792eSThomas Moestl 	int rv = 0;
122d7f7792eSThomas Moestl 
123d7f7792eSThomas Moestl 	if ((year & 3) == 0) {
124d7f7792eSThomas Moestl 		rv = 1;
125d7f7792eSThomas Moestl 		if ((year % 100) == 0) {
126d7f7792eSThomas Moestl 			rv = 0;
127d7f7792eSThomas Moestl 			if ((year % 400) == 0)
128d7f7792eSThomas Moestl 				rv = 1;
129d7f7792eSThomas Moestl 		}
130d7f7792eSThomas Moestl 	}
131d7f7792eSThomas Moestl 	return (rv);
132d7f7792eSThomas Moestl }
133d7f7792eSThomas Moestl 
134d7f7792eSThomas Moestl int
clock_ct_to_ts(const struct clocktime * ct,struct timespec * ts)135ce75945dSIan Lepore clock_ct_to_ts(const struct clocktime *ct, struct timespec *ts)
136d7f7792eSThomas Moestl {
137d7f7792eSThomas Moestl 	int i, year, days;
138d7f7792eSThomas Moestl 
139c9ad6040SPoul-Henning Kamp 	if (ct_debug) {
140aedc51f1SIan Lepore 		printf("ct_to_ts([");
141aedc51f1SIan Lepore 		clock_print_ct(ct, 9);
142aedc51f1SIan Lepore 		printf("])");
143c9ad6040SPoul-Henning Kamp 	}
144c9ad6040SPoul-Henning Kamp 
145f1b21e2cSIan Lepore 	/*
146f1b21e2cSIan Lepore 	 * Many realtime clocks store the year as 2-digit BCD; pivot on 70 to
147f1b21e2cSIan Lepore 	 * determine century.  Some clocks have a "century bit" and drivers do
148f1b21e2cSIan Lepore 	 * year += 100, so interpret values between 70-199 as relative to 1900.
149f1b21e2cSIan Lepore 	 */
150f1b21e2cSIan Lepore 	year = ct->year;
151f1b21e2cSIan Lepore 	if (year < 70)
152f1b21e2cSIan Lepore 		year += 2000;
153f1b21e2cSIan Lepore 	else if (year < 200)
154f1b21e2cSIan Lepore 		year += 1900;
155f1b21e2cSIan Lepore 
156d7f7792eSThomas Moestl 	/* Sanity checks. */
157d7f7792eSThomas Moestl 	if (ct->mon < 1 || ct->mon > 12 || ct->day < 1 ||
158d7f7792eSThomas Moestl 	    ct->day > days_in_month(year, ct->mon) ||
159f1b21e2cSIan Lepore 	    ct->hour > 23 ||  ct->min > 59 || ct->sec > 59 || year < 1970 ||
160ee9bc599SBrooks Davis 	    (sizeof(time_t) == 4 && year > 2037)) {	/* time_t overflow */
161c9ad6040SPoul-Henning Kamp 		if (ct_debug)
162c9ad6040SPoul-Henning Kamp 			printf(" = EINVAL\n");
163d7f7792eSThomas Moestl 		return (EINVAL);
164c9ad6040SPoul-Henning Kamp 	}
165d7f7792eSThomas Moestl 
166d7f7792eSThomas Moestl 	/*
167d7f7792eSThomas Moestl 	 * Compute days since start of time
168d7f7792eSThomas Moestl 	 * First from years, then from months.
169d7f7792eSThomas Moestl 	 */
170f7afe767SIan Lepore 	if (year >= recent_base_year) {
171f7afe767SIan Lepore 		i = recent_base_year;
172f7afe767SIan Lepore 		days = recent_base_days;
173f7afe767SIan Lepore 	} else {
174f7afe767SIan Lepore 		i = POSIX_BASE_YEAR;
175d7f7792eSThomas Moestl 		days = 0;
176f7afe767SIan Lepore 	}
177f7afe767SIan Lepore 	for (; i < year; i++)
178d7f7792eSThomas Moestl 		days += days_in_year(i);
179d7f7792eSThomas Moestl 
180d7f7792eSThomas Moestl 	/* Months */
181d7f7792eSThomas Moestl 	for (i = 1; i < ct->mon; i++)
182d7f7792eSThomas Moestl 	  	days += days_in_month(year, i);
183d7f7792eSThomas Moestl 	days += (ct->day - 1);
184d7f7792eSThomas Moestl 
185fe21241eSKonstantin Belousov 	ts->tv_sec = (((time_t)days * 24 + ct->hour) * 60 + ct->min) * 60 +
186fe21241eSKonstantin Belousov 	    ct->sec;
187d7f7792eSThomas Moestl 	ts->tv_nsec = ct->nsec;
188fe21241eSKonstantin Belousov 
189c9ad6040SPoul-Henning Kamp 	if (ct_debug)
190932d14c6SIan Lepore 		printf(" = %jd.%09ld\n", (intmax_t)ts->tv_sec, ts->tv_nsec);
191d7f7792eSThomas Moestl 	return (0);
192d7f7792eSThomas Moestl }
193d7f7792eSThomas Moestl 
19486299375SIan Lepore int
clock_bcd_to_ts(const struct bcd_clocktime * bct,struct timespec * ts,bool ampm)195ce75945dSIan Lepore clock_bcd_to_ts(const struct bcd_clocktime *bct, struct timespec *ts, bool ampm)
19686299375SIan Lepore {
19786299375SIan Lepore 	struct clocktime ct;
19886299375SIan Lepore 	int bcent, byear;
19986299375SIan Lepore 
20086299375SIan Lepore 	/*
20186299375SIan Lepore 	 * Year may come in as 2-digit or 4-digit BCD.  Split the value into
20286299375SIan Lepore 	 * separate BCD century and year values for validation and conversion.
20386299375SIan Lepore 	 */
20486299375SIan Lepore 	bcent = bct->year >> 8;
20586299375SIan Lepore 	byear = bct->year & 0xff;
20686299375SIan Lepore 
20786299375SIan Lepore 	/*
20886299375SIan Lepore 	 * Ensure that all values are valid BCD numbers, to avoid assertions in
20986299375SIan Lepore 	 * the BCD-to-binary conversion routines.  clock_ct_to_ts() will further
21086299375SIan Lepore 	 * validate the field ranges (such as 0 <= min <= 59) during conversion.
21186299375SIan Lepore 	 */
21286299375SIan Lepore 	if (!validbcd(bcent) || !validbcd(byear) || !validbcd(bct->mon) ||
21386299375SIan Lepore 	    !validbcd(bct->day) || !validbcd(bct->hour) ||
21486299375SIan Lepore 	    !validbcd(bct->min) || !validbcd(bct->sec)) {
21586299375SIan Lepore 		if (ct_debug)
21686299375SIan Lepore 			printf("clock_bcd_to_ts: bad BCD: "
21786299375SIan Lepore 			    "[%04x-%02x-%02x %02x:%02x:%02x]\n",
21886299375SIan Lepore 			    bct->year, bct->mon, bct->day,
21986299375SIan Lepore 			    bct->hour, bct->min, bct->sec);
22086299375SIan Lepore 		return (EINVAL);
22186299375SIan Lepore 	}
22286299375SIan Lepore 
22386299375SIan Lepore 	ct.year = FROMBCD(byear) + FROMBCD(bcent) * 100;
22486299375SIan Lepore 	ct.mon  = FROMBCD(bct->mon);
22586299375SIan Lepore 	ct.day  = FROMBCD(bct->day);
22686299375SIan Lepore 	ct.hour = FROMBCD(bct->hour);
22786299375SIan Lepore 	ct.min  = FROMBCD(bct->min);
22886299375SIan Lepore 	ct.sec  = FROMBCD(bct->sec);
22986299375SIan Lepore 	ct.dow  = bct->dow;
23086299375SIan Lepore 	ct.nsec = bct->nsec;
23186299375SIan Lepore 
23286299375SIan Lepore 	/* If asked to handle am/pm, convert from 12hr+pmflag to 24hr. */
23386299375SIan Lepore 	if (ampm) {
23486299375SIan Lepore 		if (ct.hour == 12)
23586299375SIan Lepore 			ct.hour = 0;
23686299375SIan Lepore 		if (bct->ispm)
23786299375SIan Lepore 			ct.hour += 12;
23886299375SIan Lepore 	}
23986299375SIan Lepore 
24086299375SIan Lepore 	return (clock_ct_to_ts(&ct, ts));
24186299375SIan Lepore }
24286299375SIan Lepore 
243d7f7792eSThomas Moestl void
clock_ts_to_ct(const struct timespec * ts,struct clocktime * ct)244ce75945dSIan Lepore clock_ts_to_ct(const struct timespec *ts, struct clocktime *ct)
245d7f7792eSThomas Moestl {
24690a79ac5SConrad Meyer 	time_t i, year, days;
247d7f7792eSThomas Moestl 	time_t rsec;	/* remainder seconds */
248d7f7792eSThomas Moestl 	time_t secs;
249d7f7792eSThomas Moestl 
250d7f7792eSThomas Moestl 	secs = ts->tv_sec;
251d7f7792eSThomas Moestl 	days = secs / SECDAY;
252d7f7792eSThomas Moestl 	rsec = secs % SECDAY;
253d7f7792eSThomas Moestl 
254d7f7792eSThomas Moestl 	ct->dow = day_of_week(days);
255d7f7792eSThomas Moestl 
256f7afe767SIan Lepore 	/* Subtract out whole years. */
257f7afe767SIan Lepore 	if (days >= recent_base_days) {
258f7afe767SIan Lepore 		year = recent_base_year;
259f7afe767SIan Lepore 		days -= recent_base_days;
260f7afe767SIan Lepore 	} else {
261f7afe767SIan Lepore 		year = POSIX_BASE_YEAR;
262f7afe767SIan Lepore 	}
263f7afe767SIan Lepore 	for (; days >= days_in_year(year); year++)
264d7f7792eSThomas Moestl 		days -= days_in_year(year);
265d7f7792eSThomas Moestl 	ct->year = year;
266d7f7792eSThomas Moestl 
267d7f7792eSThomas Moestl 	/* Subtract out whole months, counting them in i. */
268d7f7792eSThomas Moestl 	for (i = 1; days >= days_in_month(year, i); i++)
269d7f7792eSThomas Moestl 		days -= days_in_month(year, i);
270d7f7792eSThomas Moestl 	ct->mon = i;
271d7f7792eSThomas Moestl 
272d7f7792eSThomas Moestl 	/* Days are what is left over (+1) from all that. */
273d7f7792eSThomas Moestl 	ct->day = days + 1;
274d7f7792eSThomas Moestl 
275d7f7792eSThomas Moestl 	/* Hours, minutes, seconds are easy */
276d7f7792eSThomas Moestl 	ct->hour = rsec / 3600;
277d7f7792eSThomas Moestl 	rsec = rsec % 3600;
278d7f7792eSThomas Moestl 	ct->min  = rsec / 60;
279d7f7792eSThomas Moestl 	rsec = rsec % 60;
280d7f7792eSThomas Moestl 	ct->sec  = rsec;
281d7f7792eSThomas Moestl 	ct->nsec = ts->tv_nsec;
282c9ad6040SPoul-Henning Kamp 	if (ct_debug) {
283aedc51f1SIan Lepore 		printf("ts_to_ct(%jd.%09ld) = [",
284932d14c6SIan Lepore 		    (intmax_t)ts->tv_sec, ts->tv_nsec);
285aedc51f1SIan Lepore 		clock_print_ct(ct, 9);
286aedc51f1SIan Lepore 		printf("]\n");
287c9ad6040SPoul-Henning Kamp 	}
28890a79ac5SConrad Meyer 
28990a79ac5SConrad Meyer 	KASSERT(ct->year >= 0 && ct->year < 10000,
29090a79ac5SConrad Meyer 	    ("year %d isn't a 4 digit year", ct->year));
29190a79ac5SConrad Meyer 	KASSERT(ct->mon >= 1 && ct->mon <= 12,
29290a79ac5SConrad Meyer 	    ("month %d not in 1-12", ct->mon));
29390a79ac5SConrad Meyer 	KASSERT(ct->day >= 1 && ct->day <= 31,
29490a79ac5SConrad Meyer 	    ("day %d not in 1-31", ct->day));
29590a79ac5SConrad Meyer 	KASSERT(ct->hour >= 0 && ct->hour <= 23,
29690a79ac5SConrad Meyer 	    ("hour %d not in 0-23", ct->hour));
29790a79ac5SConrad Meyer 	KASSERT(ct->min >= 0 && ct->min <= 59,
29890a79ac5SConrad Meyer 	    ("minute %d not in 0-59", ct->min));
29990a79ac5SConrad Meyer 	/* Not sure if this interface needs to handle leapseconds or not. */
30090a79ac5SConrad Meyer 	KASSERT(ct->sec >= 0 && ct->sec <= 60,
30190a79ac5SConrad Meyer 	    ("seconds %d not in 0-60", ct->sec));
302d7f7792eSThomas Moestl }
303f97c1c4bSPoul-Henning Kamp 
30486299375SIan Lepore void
clock_ts_to_bcd(const struct timespec * ts,struct bcd_clocktime * bct,bool ampm)305ce75945dSIan Lepore clock_ts_to_bcd(const struct timespec *ts, struct bcd_clocktime *bct, bool ampm)
30686299375SIan Lepore {
30786299375SIan Lepore 	struct clocktime ct;
30886299375SIan Lepore 
30986299375SIan Lepore 	clock_ts_to_ct(ts, &ct);
31086299375SIan Lepore 
31186299375SIan Lepore 	/* If asked to handle am/pm, convert from 24hr to 12hr+pmflag. */
31286299375SIan Lepore 	bct->ispm = false;
31386299375SIan Lepore 	if (ampm) {
31486299375SIan Lepore 		if (ct.hour >= 12) {
31586299375SIan Lepore 			ct.hour -= 12;
31686299375SIan Lepore 			bct->ispm = true;
31786299375SIan Lepore 		}
31886299375SIan Lepore 		if (ct.hour == 0)
31986299375SIan Lepore 			ct.hour = 12;
32086299375SIan Lepore 	}
32186299375SIan Lepore 
32286299375SIan Lepore 	bct->year = TOBCD(ct.year % 100) | (TOBCD(ct.year / 100) << 8);
32386299375SIan Lepore 	bct->mon  = TOBCD(ct.mon);
32486299375SIan Lepore 	bct->day  = TOBCD(ct.day);
32586299375SIan Lepore 	bct->hour = TOBCD(ct.hour);
32686299375SIan Lepore 	bct->min  = TOBCD(ct.min);
32786299375SIan Lepore 	bct->sec  = TOBCD(ct.sec);
32886299375SIan Lepore 	bct->dow  = ct.dow;
32986299375SIan Lepore 	bct->nsec = ct.nsec;
33086299375SIan Lepore }
33186299375SIan Lepore 
332aedc51f1SIan Lepore void
clock_print_bcd(const struct bcd_clocktime * bct,int nsdigits)333aedc51f1SIan Lepore clock_print_bcd(const struct bcd_clocktime *bct, int nsdigits)
334aedc51f1SIan Lepore {
335aedc51f1SIan Lepore 
336aedc51f1SIan Lepore 	KASSERT(nsdigits >= 0 && nsdigits <= 9, ("bad nsdigits %d", nsdigits));
337aedc51f1SIan Lepore 
338aedc51f1SIan Lepore 	if (nsdigits > 0) {
339aedc51f1SIan Lepore 		printf("%4.4x-%2.2x-%2.2x %2.2x:%2.2x:%2.2x.%*.*ld",
340aedc51f1SIan Lepore 		    bct->year, bct->mon, bct->day,
341aedc51f1SIan Lepore 		    bct->hour, bct->min, bct->sec,
342aedc51f1SIan Lepore 		    nsdigits, nsdigits, bct->nsec / nsdivisors[nsdigits]);
343aedc51f1SIan Lepore 	} else {
344aedc51f1SIan Lepore 		printf("%4.4x-%2.2x-%2.2x %2.2x:%2.2x:%2.2x",
345aedc51f1SIan Lepore 		    bct->year, bct->mon, bct->day,
346aedc51f1SIan Lepore 		    bct->hour, bct->min, bct->sec);
347aedc51f1SIan Lepore 	}
348aedc51f1SIan Lepore }
349aedc51f1SIan Lepore 
350aedc51f1SIan Lepore void
clock_print_ct(const struct clocktime * ct,int nsdigits)351aedc51f1SIan Lepore clock_print_ct(const struct clocktime *ct, int nsdigits)
352aedc51f1SIan Lepore {
353aedc51f1SIan Lepore 
354aedc51f1SIan Lepore 	KASSERT(nsdigits >= 0 && nsdigits <= 9, ("bad nsdigits %d", nsdigits));
355aedc51f1SIan Lepore 
356aedc51f1SIan Lepore 	if (nsdigits > 0) {
357aedc51f1SIan Lepore 		printf("%04d-%02d-%02d %02d:%02d:%02d.%*.*ld",
358aedc51f1SIan Lepore 		    ct->year, ct->mon, ct->day,
359aedc51f1SIan Lepore 		    ct->hour, ct->min, ct->sec,
360aedc51f1SIan Lepore 		    nsdigits, nsdigits, ct->nsec / nsdivisors[nsdigits]);
361aedc51f1SIan Lepore 	} else {
362aedc51f1SIan Lepore 		printf("%04d-%02d-%02d %02d:%02d:%02d",
363aedc51f1SIan Lepore 		    ct->year, ct->mon, ct->day,
364aedc51f1SIan Lepore 		    ct->hour, ct->min, ct->sec);
365aedc51f1SIan Lepore 	}
366aedc51f1SIan Lepore }
367aedc51f1SIan Lepore 
368aedc51f1SIan Lepore void
clock_print_ts(const struct timespec * ts,int nsdigits)369aedc51f1SIan Lepore clock_print_ts(const struct timespec *ts, int nsdigits)
370aedc51f1SIan Lepore {
371aedc51f1SIan Lepore 	struct clocktime ct;
372aedc51f1SIan Lepore 
373aedc51f1SIan Lepore 	clock_ts_to_ct(ts, &ct);
374aedc51f1SIan Lepore 	clock_print_ct(&ct, nsdigits);
375aedc51f1SIan Lepore }
376aedc51f1SIan Lepore 
377f97c1c4bSPoul-Henning Kamp int
utc_offset(void)378f97c1c4bSPoul-Henning Kamp utc_offset(void)
379f97c1c4bSPoul-Henning Kamp {
380f97c1c4bSPoul-Henning Kamp 
381*329f0aa9SWarner Losh 	return (wall_cmos_clock ? adjkerntz : 0);
382f97c1c4bSPoul-Henning Kamp }
383