xref: /freebsd/sys/kern/subr_clock.c (revision 531c890b8aecbf157fe3491503b5ca62c0b01093)
1 /*-
2  * Copyright (c) 1988 University of Utah.
3  * Copyright (c) 1982, 1990, 1993
4  *	The Regents of the University of California.  All rights reserved.
5  *
6  * This code is derived from software contributed to Berkeley by
7  * the Systems Programming Group of the University of Utah Computer
8  * Science Department.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 4. Neither the name of the University nor the names of its contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  *
34  *	from: Utah $Hdr: clock.c 1.18 91/01/21$
35  *	from: @(#)clock.c	8.2 (Berkeley) 1/12/94
36  *	from: NetBSD: clock_subr.c,v 1.6 2001/07/07 17:04:02 thorpej Exp
37  *	and
38  *	from: src/sys/i386/isa/clock.c,v 1.176 2001/09/04
39  */
40 
41 #include <sys/cdefs.h>
42 __FBSDID("$FreeBSD$");
43 
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/kernel.h>
47 #include <sys/bus.h>
48 #include <sys/clock.h>
49 #include <sys/sysctl.h>
50 #include <sys/timetc.h>
51 
52 static int adjkerntz;		/* local offset from GMT in seconds */
53 static int wall_cmos_clock;	/* wall CMOS clock assumed if != 0 */
54 int disable_rtc_set;		/* disable resettodr() if != 0 */
55 
56 int tz_minuteswest;
57 int tz_dsttime;
58 
59 /*
60  * These have traditionally been in machdep, but should probably be moved to
61  * kern.
62  */
63 SYSCTL_INT(_machdep, OID_AUTO, disable_rtc_set,
64 	CTLFLAG_RW, &disable_rtc_set, 0, "");
65 
66 SYSCTL_INT(_machdep, OID_AUTO, wall_cmos_clock,
67 	CTLFLAG_RW, &wall_cmos_clock, 0, "");
68 
69 static int
70 sysctl_machdep_adjkerntz(SYSCTL_HANDLER_ARGS)
71 {
72 	int error;
73 	error = sysctl_handle_int(oidp, oidp->oid_arg1, oidp->oid_arg2,
74 		req);
75 	if (!error && req->newptr)
76 		resettodr();
77 	return (error);
78 }
79 
80 SYSCTL_PROC(_machdep, OID_AUTO, adjkerntz, CTLTYPE_INT|CTLFLAG_RW,
81 	&adjkerntz, 0, sysctl_machdep_adjkerntz, "I", "");
82 
83 /*--------------------------------------------------------------------*
84  * Generic routines to convert between a POSIX date
85  * (seconds since 1/1/1970) and yr/mo/day/hr/min/sec
86  * Derived from NetBSD arch/hp300/hp300/clock.c
87  */
88 
89 
90 #define	FEBRUARY	2
91 #define	days_in_year(y) 	(leapyear(y) ? 366 : 365)
92 #define	days_in_month(y, m) \
93 	(month_days[(m) - 1] + (m == FEBRUARY ? leapyear(y) : 0))
94 /* Day of week. Days are counted from 1/1/1970, which was a Thursday */
95 #define	day_of_week(days)	(((days) + 4) % 7)
96 
97 static const int month_days[12] = {
98 	31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
99 };
100 
101 
102 /*
103  * This inline avoids some unnecessary modulo operations
104  * as compared with the usual macro:
105  *   ( ((year % 4) == 0 &&
106  *      (year % 100) != 0) ||
107  *     ((year % 400) == 0) )
108  * It is otherwise equivalent.
109  */
110 static __inline int
111 leapyear(int year)
112 {
113 	int rv = 0;
114 
115 	if ((year & 3) == 0) {
116 		rv = 1;
117 		if ((year % 100) == 0) {
118 			rv = 0;
119 			if ((year % 400) == 0)
120 				rv = 1;
121 		}
122 	}
123 	return (rv);
124 }
125 
126 int
127 clock_ct_to_ts(struct clocktime *ct, struct timespec *ts)
128 {
129 	time_t secs;
130 	int i, year, days;
131 
132 	year = ct->year;
133 
134 	/* Sanity checks. */
135 	if (ct->mon < 1 || ct->mon > 12 || ct->day < 1 ||
136 	    ct->day > days_in_month(year, ct->mon) ||
137 	    ct->hour > 23 ||  ct->min > 59 || ct->sec > 59 ||
138 	    ct->year > 2037)		/* time_t overflow */
139 		return (EINVAL);
140 
141 	/*
142 	 * Compute days since start of time
143 	 * First from years, then from months.
144 	 */
145 	days = 0;
146 	for (i = POSIX_BASE_YEAR; i < year; i++)
147 		days += days_in_year(i);
148 
149 	/* Months */
150 	for (i = 1; i < ct->mon; i++)
151 	  	days += days_in_month(year, i);
152 	days += (ct->day - 1);
153 
154 	/* XXX Dow sanity check. Dow is not used, so should we check it? */
155 	if (ct->dow != -1 && ct->dow != day_of_week(days))
156 		return (EINVAL);
157 
158 	/* Add hours, minutes, seconds. */
159 	secs = ((days * 24 + ct->hour) * 60 + ct->min) * 60 + ct->sec;
160 
161 	ts->tv_sec = secs;
162 	ts->tv_nsec = ct->nsec;
163 	return (0);
164 }
165 
166 void
167 clock_ts_to_ct(struct timespec *ts, struct clocktime *ct)
168 {
169 	int i, year, days;
170 	time_t rsec;	/* remainder seconds */
171 	time_t secs;
172 
173 	secs = ts->tv_sec;
174 	days = secs / SECDAY;
175 	rsec = secs % SECDAY;
176 
177 	ct->dow = day_of_week(days);
178 
179 	/* Subtract out whole years, counting them in i. */
180 	for (year = POSIX_BASE_YEAR; days >= days_in_year(year); year++)
181 		days -= days_in_year(year);
182 	ct->year = year;
183 
184 	/* Subtract out whole months, counting them in i. */
185 	for (i = 1; days >= days_in_month(year, i); i++)
186 		days -= days_in_month(year, i);
187 	ct->mon = i;
188 
189 	/* Days are what is left over (+1) from all that. */
190 	ct->day = days + 1;
191 
192 	/* Hours, minutes, seconds are easy */
193 	ct->hour = rsec / 3600;
194 	rsec = rsec % 3600;
195 	ct->min  = rsec / 60;
196 	rsec = rsec % 60;
197 	ct->sec  = rsec;
198 	ct->nsec = ts->tv_nsec;
199 }
200 
201 int
202 utc_offset(void)
203 {
204 
205 	return (tz_minuteswest * 60 + (wall_cmos_clock ? adjkerntz : 0));
206 }
207