xref: /freebsd/sys/kern/subr_clock.c (revision f7afe7679c13db6e156babe26c8394d82a60e73a)
19454b2d8SWarner Losh /*-
2d7f7792eSThomas Moestl  * Copyright (c) 1988 University of Utah.
3d7f7792eSThomas Moestl  * Copyright (c) 1982, 1990, 1993
4d7f7792eSThomas Moestl  *	The Regents of the University of California.  All rights reserved.
5d7f7792eSThomas Moestl  *
6d7f7792eSThomas Moestl  * This code is derived from software contributed to Berkeley by
7d7f7792eSThomas Moestl  * the Systems Programming Group of the University of Utah Computer
8d7f7792eSThomas Moestl  * Science Department.
9d7f7792eSThomas Moestl  *
10d7f7792eSThomas Moestl  * Redistribution and use in source and binary forms, with or without
11d7f7792eSThomas Moestl  * modification, are permitted provided that the following conditions
12d7f7792eSThomas Moestl  * are met:
13d7f7792eSThomas Moestl  * 1. Redistributions of source code must retain the above copyright
14d7f7792eSThomas Moestl  *    notice, this list of conditions and the following disclaimer.
15d7f7792eSThomas Moestl  * 2. Redistributions in binary form must reproduce the above copyright
16d7f7792eSThomas Moestl  *    notice, this list of conditions and the following disclaimer in the
17d7f7792eSThomas Moestl  *    documentation and/or other materials provided with the distribution.
1869a28758SEd Maste  * 3. Neither the name of the University nor the names of its contributors
19d7f7792eSThomas Moestl  *    may be used to endorse or promote products derived from this software
20d7f7792eSThomas Moestl  *    without specific prior written permission.
21d7f7792eSThomas Moestl  *
22d7f7792eSThomas Moestl  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23d7f7792eSThomas Moestl  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24d7f7792eSThomas Moestl  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25d7f7792eSThomas Moestl  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26d7f7792eSThomas Moestl  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27d7f7792eSThomas Moestl  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28d7f7792eSThomas Moestl  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29d7f7792eSThomas Moestl  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30d7f7792eSThomas Moestl  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31d7f7792eSThomas Moestl  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32d7f7792eSThomas Moestl  * SUCH DAMAGE.
33d7f7792eSThomas Moestl  *
34d7f7792eSThomas Moestl  *	from: Utah $Hdr: clock.c 1.18 91/01/21$
35d7f7792eSThomas Moestl  *	from: @(#)clock.c	8.2 (Berkeley) 1/12/94
36d7f7792eSThomas Moestl  *	from: NetBSD: clock_subr.c,v 1.6 2001/07/07 17:04:02 thorpej Exp
37d7f7792eSThomas Moestl  *	and
38d7f7792eSThomas Moestl  *	from: src/sys/i386/isa/clock.c,v 1.176 2001/09/04
39d7f7792eSThomas Moestl  */
40d7f7792eSThomas Moestl 
41677b542eSDavid E. O'Brien #include <sys/cdefs.h>
42677b542eSDavid E. O'Brien __FBSDID("$FreeBSD$");
43677b542eSDavid E. O'Brien 
44d7f7792eSThomas Moestl #include <sys/param.h>
45d7f7792eSThomas Moestl #include <sys/systm.h>
46d7f7792eSThomas Moestl #include <sys/kernel.h>
47d7f7792eSThomas Moestl #include <sys/bus.h>
48d7f7792eSThomas Moestl #include <sys/clock.h>
49ee9bc599SBrooks Davis #include <sys/limits.h>
50d7f7792eSThomas Moestl #include <sys/sysctl.h>
51d7f7792eSThomas Moestl #include <sys/timetc.h>
52d7f7792eSThomas Moestl 
5394d67e0fSPoul-Henning Kamp int tz_minuteswest;
5494d67e0fSPoul-Henning Kamp int tz_dsttime;
5594d67e0fSPoul-Henning Kamp 
56d7f7792eSThomas Moestl /*
578d12fab9SBjoern A. Zeeb  * The adjkerntz and wall_cmos_clock sysctls are in the "machdep" sysctl
588d12fab9SBjoern A. Zeeb  * namespace because they were misplaced there originally.
59d7f7792eSThomas Moestl  */
608d12fab9SBjoern A. Zeeb static int adjkerntz;
61d7f7792eSThomas Moestl static int
62d7f7792eSThomas Moestl sysctl_machdep_adjkerntz(SYSCTL_HANDLER_ARGS)
63d7f7792eSThomas Moestl {
64d7f7792eSThomas Moestl 	int error;
659b4a8ab7SPoul-Henning Kamp 	error = sysctl_handle_int(oidp, oidp->oid_arg1, oidp->oid_arg2, req);
66d7f7792eSThomas Moestl 	if (!error && req->newptr)
67d7f7792eSThomas Moestl 		resettodr();
68d7f7792eSThomas Moestl 	return (error);
69d7f7792eSThomas Moestl }
703a0e6f92SKonstantin Belousov SYSCTL_PROC(_machdep, OID_AUTO, adjkerntz, CTLTYPE_INT | CTLFLAG_RW |
713a0e6f92SKonstantin Belousov     CTLFLAG_MPSAFE, &adjkerntz, 0, sysctl_machdep_adjkerntz, "I",
72b389be97SRebecca Cran     "Local offset from UTC in seconds");
73b69f71ebSPoul-Henning Kamp 
748d12fab9SBjoern A. Zeeb static int ct_debug;
75932d14c6SIan Lepore SYSCTL_INT(_debug, OID_AUTO, clocktime, CTLFLAG_RWTUN,
768d12fab9SBjoern A. Zeeb     &ct_debug, 0, "Enable printing of clocktime debugging");
778d12fab9SBjoern A. Zeeb 
788d12fab9SBjoern A. Zeeb static int wall_cmos_clock;
798d12fab9SBjoern A. Zeeb SYSCTL_INT(_machdep, OID_AUTO, wall_cmos_clock, CTLFLAG_RW,
808d12fab9SBjoern A. Zeeb     &wall_cmos_clock, 0, "Enables application of machdep.adjkerntz");
818d12fab9SBjoern A. Zeeb 
82b69f71ebSPoul-Henning Kamp /*--------------------------------------------------------------------*
83b69f71ebSPoul-Henning Kamp  * Generic routines to convert between a POSIX date
84b69f71ebSPoul-Henning Kamp  * (seconds since 1/1/1970) and yr/mo/day/hr/min/sec
85b69f71ebSPoul-Henning Kamp  * Derived from NetBSD arch/hp300/hp300/clock.c
86b69f71ebSPoul-Henning Kamp  */
87b69f71ebSPoul-Henning Kamp 
88b69f71ebSPoul-Henning Kamp 
89b69f71ebSPoul-Henning Kamp #define	FEBRUARY	2
90b69f71ebSPoul-Henning Kamp #define	days_in_year(y) 	(leapyear(y) ? 366 : 365)
91b69f71ebSPoul-Henning Kamp #define	days_in_month(y, m) \
92b69f71ebSPoul-Henning Kamp 	(month_days[(m) - 1] + (m == FEBRUARY ? leapyear(y) : 0))
93b69f71ebSPoul-Henning Kamp /* Day of week. Days are counted from 1/1/1970, which was a Thursday */
94b69f71ebSPoul-Henning Kamp #define	day_of_week(days)	(((days) + 4) % 7)
95b69f71ebSPoul-Henning Kamp 
96b69f71ebSPoul-Henning Kamp static const int month_days[12] = {
97b69f71ebSPoul-Henning Kamp 	31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
98b69f71ebSPoul-Henning Kamp };
99b69f71ebSPoul-Henning Kamp 
100*f7afe767SIan Lepore /*
101*f7afe767SIan Lepore  * Optimization: using a precomputed count of days between POSIX_BASE_YEAR and
102*f7afe767SIan Lepore  * some recent year avoids lots of unnecessary loop iterations in conversion.
103*f7afe767SIan Lepore  * recent_base_days is the number of days before the start of recent_base_year.
104*f7afe767SIan Lepore  */
105*f7afe767SIan Lepore static const int recent_base_year = 2017;
106*f7afe767SIan Lepore static const int recent_base_days = 17167;
107b69f71ebSPoul-Henning Kamp 
108d7f7792eSThomas Moestl /*
109d7f7792eSThomas Moestl  * This inline avoids some unnecessary modulo operations
110d7f7792eSThomas Moestl  * as compared with the usual macro:
111d7f7792eSThomas Moestl  *   ( ((year % 4) == 0 &&
112d7f7792eSThomas Moestl  *      (year % 100) != 0) ||
113d7f7792eSThomas Moestl  *     ((year % 400) == 0) )
114d7f7792eSThomas Moestl  * It is otherwise equivalent.
115d7f7792eSThomas Moestl  */
116c9ad6040SPoul-Henning Kamp static int
117d7f7792eSThomas Moestl leapyear(int year)
118d7f7792eSThomas Moestl {
119d7f7792eSThomas Moestl 	int rv = 0;
120d7f7792eSThomas Moestl 
121d7f7792eSThomas Moestl 	if ((year & 3) == 0) {
122d7f7792eSThomas Moestl 		rv = 1;
123d7f7792eSThomas Moestl 		if ((year % 100) == 0) {
124d7f7792eSThomas Moestl 			rv = 0;
125d7f7792eSThomas Moestl 			if ((year % 400) == 0)
126d7f7792eSThomas Moestl 				rv = 1;
127d7f7792eSThomas Moestl 		}
128d7f7792eSThomas Moestl 	}
129d7f7792eSThomas Moestl 	return (rv);
130d7f7792eSThomas Moestl }
131d7f7792eSThomas Moestl 
132c9ad6040SPoul-Henning Kamp static void
133c9ad6040SPoul-Henning Kamp print_ct(struct clocktime *ct)
134c9ad6040SPoul-Henning Kamp {
135c9ad6040SPoul-Henning Kamp 	printf("[%04d-%02d-%02d %02d:%02d:%02d]",
136c9ad6040SPoul-Henning Kamp 	    ct->year, ct->mon, ct->day,
137c9ad6040SPoul-Henning Kamp 	    ct->hour, ct->min, ct->sec);
138c9ad6040SPoul-Henning Kamp }
139c9ad6040SPoul-Henning Kamp 
140d7f7792eSThomas Moestl int
141d7f7792eSThomas Moestl clock_ct_to_ts(struct clocktime *ct, struct timespec *ts)
142d7f7792eSThomas Moestl {
143d7f7792eSThomas Moestl 	int i, year, days;
144d7f7792eSThomas Moestl 
145d7f7792eSThomas Moestl 	year = ct->year;
146d7f7792eSThomas Moestl 
147c9ad6040SPoul-Henning Kamp 	if (ct_debug) {
148c9ad6040SPoul-Henning Kamp 		printf("ct_to_ts(");
149c9ad6040SPoul-Henning Kamp 		print_ct(ct);
150c9ad6040SPoul-Henning Kamp 		printf(")");
151c9ad6040SPoul-Henning Kamp 	}
152c9ad6040SPoul-Henning Kamp 
153d7f7792eSThomas Moestl 	/* Sanity checks. */
154d7f7792eSThomas Moestl 	if (ct->mon < 1 || ct->mon > 12 || ct->day < 1 ||
155d7f7792eSThomas Moestl 	    ct->day > days_in_month(year, ct->mon) ||
156d7f7792eSThomas Moestl 	    ct->hour > 23 ||  ct->min > 59 || ct->sec > 59 ||
157ee9bc599SBrooks Davis 	    (sizeof(time_t) == 4 && year > 2037)) {	/* time_t overflow */
158c9ad6040SPoul-Henning Kamp 		if (ct_debug)
159c9ad6040SPoul-Henning Kamp 			printf(" = EINVAL\n");
160d7f7792eSThomas Moestl 		return (EINVAL);
161c9ad6040SPoul-Henning Kamp 	}
162d7f7792eSThomas Moestl 
163d7f7792eSThomas Moestl 	/*
164d7f7792eSThomas Moestl 	 * Compute days since start of time
165d7f7792eSThomas Moestl 	 * First from years, then from months.
166d7f7792eSThomas Moestl 	 */
167*f7afe767SIan Lepore 	if (year >= recent_base_year) {
168*f7afe767SIan Lepore 		i = recent_base_year;
169*f7afe767SIan Lepore 		days = recent_base_days;
170*f7afe767SIan Lepore 	} else {
171*f7afe767SIan Lepore 		i = POSIX_BASE_YEAR;
172d7f7792eSThomas Moestl 		days = 0;
173*f7afe767SIan Lepore 	}
174*f7afe767SIan Lepore 	for (; i < year; i++)
175d7f7792eSThomas Moestl 		days += days_in_year(i);
176d7f7792eSThomas Moestl 
177d7f7792eSThomas Moestl 	/* Months */
178d7f7792eSThomas Moestl 	for (i = 1; i < ct->mon; i++)
179d7f7792eSThomas Moestl 	  	days += days_in_month(year, i);
180d7f7792eSThomas Moestl 	days += (ct->day - 1);
181d7f7792eSThomas Moestl 
182fe21241eSKonstantin Belousov 	ts->tv_sec = (((time_t)days * 24 + ct->hour) * 60 + ct->min) * 60 +
183fe21241eSKonstantin Belousov 	    ct->sec;
184d7f7792eSThomas Moestl 	ts->tv_nsec = ct->nsec;
185fe21241eSKonstantin Belousov 
186c9ad6040SPoul-Henning Kamp 	if (ct_debug)
187932d14c6SIan Lepore 		printf(" = %jd.%09ld\n", (intmax_t)ts->tv_sec, ts->tv_nsec);
188d7f7792eSThomas Moestl 	return (0);
189d7f7792eSThomas Moestl }
190d7f7792eSThomas Moestl 
191d7f7792eSThomas Moestl void
192d7f7792eSThomas Moestl clock_ts_to_ct(struct timespec *ts, struct clocktime *ct)
193d7f7792eSThomas Moestl {
19490a79ac5SConrad Meyer 	time_t i, year, days;
195d7f7792eSThomas Moestl 	time_t rsec;	/* remainder seconds */
196d7f7792eSThomas Moestl 	time_t secs;
197d7f7792eSThomas Moestl 
198d7f7792eSThomas Moestl 	secs = ts->tv_sec;
199d7f7792eSThomas Moestl 	days = secs / SECDAY;
200d7f7792eSThomas Moestl 	rsec = secs % SECDAY;
201d7f7792eSThomas Moestl 
202d7f7792eSThomas Moestl 	ct->dow = day_of_week(days);
203d7f7792eSThomas Moestl 
204*f7afe767SIan Lepore 	/* Subtract out whole years. */
205*f7afe767SIan Lepore 	if (days >= recent_base_days) {
206*f7afe767SIan Lepore 		year = recent_base_year;
207*f7afe767SIan Lepore 		days -= recent_base_days;
208*f7afe767SIan Lepore 	} else {
209*f7afe767SIan Lepore 		year = POSIX_BASE_YEAR;
210*f7afe767SIan Lepore 	}
211*f7afe767SIan Lepore 	for (; days >= days_in_year(year); year++)
212d7f7792eSThomas Moestl 		days -= days_in_year(year);
213d7f7792eSThomas Moestl 	ct->year = year;
214d7f7792eSThomas Moestl 
215d7f7792eSThomas Moestl 	/* Subtract out whole months, counting them in i. */
216d7f7792eSThomas Moestl 	for (i = 1; days >= days_in_month(year, i); i++)
217d7f7792eSThomas Moestl 		days -= days_in_month(year, i);
218d7f7792eSThomas Moestl 	ct->mon = i;
219d7f7792eSThomas Moestl 
220d7f7792eSThomas Moestl 	/* Days are what is left over (+1) from all that. */
221d7f7792eSThomas Moestl 	ct->day = days + 1;
222d7f7792eSThomas Moestl 
223d7f7792eSThomas Moestl 	/* Hours, minutes, seconds are easy */
224d7f7792eSThomas Moestl 	ct->hour = rsec / 3600;
225d7f7792eSThomas Moestl 	rsec = rsec % 3600;
226d7f7792eSThomas Moestl 	ct->min  = rsec / 60;
227d7f7792eSThomas Moestl 	rsec = rsec % 60;
228d7f7792eSThomas Moestl 	ct->sec  = rsec;
229d7f7792eSThomas Moestl 	ct->nsec = ts->tv_nsec;
230c9ad6040SPoul-Henning Kamp 	if (ct_debug) {
231932d14c6SIan Lepore 		printf("ts_to_ct(%jd.%09ld) = ",
232932d14c6SIan Lepore 		    (intmax_t)ts->tv_sec, ts->tv_nsec);
233c9ad6040SPoul-Henning Kamp 		print_ct(ct);
234c9ad6040SPoul-Henning Kamp 		printf("\n");
235c9ad6040SPoul-Henning Kamp 	}
23690a79ac5SConrad Meyer 
23790a79ac5SConrad Meyer 	KASSERT(ct->year >= 0 && ct->year < 10000,
23890a79ac5SConrad Meyer 	    ("year %d isn't a 4 digit year", ct->year));
23990a79ac5SConrad Meyer 	KASSERT(ct->mon >= 1 && ct->mon <= 12,
24090a79ac5SConrad Meyer 	    ("month %d not in 1-12", ct->mon));
24190a79ac5SConrad Meyer 	KASSERT(ct->day >= 1 && ct->day <= 31,
24290a79ac5SConrad Meyer 	    ("day %d not in 1-31", ct->day));
24390a79ac5SConrad Meyer 	KASSERT(ct->hour >= 0 && ct->hour <= 23,
24490a79ac5SConrad Meyer 	    ("hour %d not in 0-23", ct->hour));
24590a79ac5SConrad Meyer 	KASSERT(ct->min >= 0 && ct->min <= 59,
24690a79ac5SConrad Meyer 	    ("minute %d not in 0-59", ct->min));
24790a79ac5SConrad Meyer 	/* Not sure if this interface needs to handle leapseconds or not. */
24890a79ac5SConrad Meyer 	KASSERT(ct->sec >= 0 && ct->sec <= 60,
24990a79ac5SConrad Meyer 	    ("seconds %d not in 0-60", ct->sec));
250d7f7792eSThomas Moestl }
251f97c1c4bSPoul-Henning Kamp 
252f97c1c4bSPoul-Henning Kamp int
253f97c1c4bSPoul-Henning Kamp utc_offset(void)
254f97c1c4bSPoul-Henning Kamp {
255f97c1c4bSPoul-Henning Kamp 
256f97c1c4bSPoul-Henning Kamp 	return (tz_minuteswest * 60 + (wall_cmos_clock ? adjkerntz : 0));
257f97c1c4bSPoul-Henning Kamp }
258