subr_clock.c revision 285830
1112158Sdas/*-
2112158Sdas * Copyright (c) 1988 University of Utah.
3112158Sdas * Copyright (c) 1982, 1990, 1993
4112158Sdas *	The Regents of the University of California.  All rights reserved.
5112158Sdas *
6112158Sdas * This code is derived from software contributed to Berkeley by
7112158Sdas * the Systems Programming Group of the University of Utah Computer
8112158Sdas * Science Department.
9112158Sdas *
10112158Sdas * Redistribution and use in source and binary forms, with or without
11112158Sdas * modification, are permitted provided that the following conditions
12112158Sdas * are met:
13112158Sdas * 1. Redistributions of source code must retain the above copyright
14112158Sdas *    notice, this list of conditions and the following disclaimer.
15112158Sdas * 2. Redistributions in binary form must reproduce the above copyright
16112158Sdas *    notice, this list of conditions and the following disclaimer in the
17112158Sdas *    documentation and/or other materials provided with the distribution.
18112158Sdas * 4. Neither the name of the University nor the names of its contributors
19112158Sdas *    may be used to endorse or promote products derived from this software
20112158Sdas *    without specific prior written permission.
21112158Sdas *
22112158Sdas * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23112158Sdas * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24112158Sdas * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25112158Sdas * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26112158Sdas * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27112158Sdas * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28112158Sdas * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29112158Sdas * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30112158Sdas * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31112158Sdas * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32112158Sdas * SUCH DAMAGE.
33112158Sdas *
34112158Sdas *	from: Utah $Hdr: clock.c 1.18 91/01/21$
35112158Sdas *	from: @(#)clock.c	8.2 (Berkeley) 1/12/94
36112158Sdas *	from: NetBSD: clock_subr.c,v 1.6 2001/07/07 17:04:02 thorpej Exp
37112158Sdas *	and
38112158Sdas *	from: src/sys/i386/isa/clock.c,v 1.176 2001/09/04
39112158Sdas */
40112158Sdas
41112158Sdas#include <sys/cdefs.h>
42112158Sdas__FBSDID("$FreeBSD: releng/10.2/sys/kern/subr_clock.c 275932 2014-12-19 09:34:14Z kib $");
43112158Sdas
44112158Sdas#include <sys/param.h>
45112158Sdas#include <sys/systm.h>
46112158Sdas#include <sys/kernel.h>
47112158Sdas#include <sys/bus.h>
48112158Sdas#include <sys/clock.h>
49112158Sdas#include <sys/limits.h>
50112158Sdas#include <sys/sysctl.h>
51112158Sdas#include <sys/timetc.h>
52112158Sdas
53112158Sdasint tz_minuteswest;
54112158Sdasint tz_dsttime;
55112158Sdas
56112158Sdas/*
57112158Sdas * The adjkerntz and wall_cmos_clock sysctls are in the "machdep" sysctl
58112158Sdas * namespace because they were misplaced there originally.
59112158Sdas */
60112158Sdasstatic int adjkerntz;
61112158Sdasstatic int
62sysctl_machdep_adjkerntz(SYSCTL_HANDLER_ARGS)
63{
64	int error;
65	error = sysctl_handle_int(oidp, oidp->oid_arg1, oidp->oid_arg2, req);
66	if (!error && req->newptr)
67		resettodr();
68	return (error);
69}
70SYSCTL_PROC(_machdep, OID_AUTO, adjkerntz, CTLTYPE_INT|CTLFLAG_RW,
71    &adjkerntz, 0, sysctl_machdep_adjkerntz, "I",
72    "Local offset from UTC in seconds");
73
74static int ct_debug;
75SYSCTL_INT(_debug, OID_AUTO, clocktime, CTLFLAG_RW,
76    &ct_debug, 0, "Enable printing of clocktime debugging");
77
78static int wall_cmos_clock;
79SYSCTL_INT(_machdep, OID_AUTO, wall_cmos_clock, CTLFLAG_RW,
80    &wall_cmos_clock, 0, "Enables application of machdep.adjkerntz");
81
82/*--------------------------------------------------------------------*
83 * Generic routines to convert between a POSIX date
84 * (seconds since 1/1/1970) and yr/mo/day/hr/min/sec
85 * Derived from NetBSD arch/hp300/hp300/clock.c
86 */
87
88
89#define	FEBRUARY	2
90#define	days_in_year(y) 	(leapyear(y) ? 366 : 365)
91#define	days_in_month(y, m) \
92	(month_days[(m) - 1] + (m == FEBRUARY ? leapyear(y) : 0))
93/* Day of week. Days are counted from 1/1/1970, which was a Thursday */
94#define	day_of_week(days)	(((days) + 4) % 7)
95
96static const int month_days[12] = {
97	31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
98};
99
100
101/*
102 * This inline avoids some unnecessary modulo operations
103 * as compared with the usual macro:
104 *   ( ((year % 4) == 0 &&
105 *      (year % 100) != 0) ||
106 *     ((year % 400) == 0) )
107 * It is otherwise equivalent.
108 */
109static int
110leapyear(int year)
111{
112	int rv = 0;
113
114	if ((year & 3) == 0) {
115		rv = 1;
116		if ((year % 100) == 0) {
117			rv = 0;
118			if ((year % 400) == 0)
119				rv = 1;
120		}
121	}
122	return (rv);
123}
124
125static void
126print_ct(struct clocktime *ct)
127{
128	printf("[%04d-%02d-%02d %02d:%02d:%02d]",
129	    ct->year, ct->mon, ct->day,
130	    ct->hour, ct->min, ct->sec);
131}
132
133int
134clock_ct_to_ts(struct clocktime *ct, struct timespec *ts)
135{
136	int i, year, days;
137
138	year = ct->year;
139
140	if (ct_debug) {
141		printf("ct_to_ts(");
142		print_ct(ct);
143		printf(")");
144	}
145
146	/* Sanity checks. */
147	if (ct->mon < 1 || ct->mon > 12 || ct->day < 1 ||
148	    ct->day > days_in_month(year, ct->mon) ||
149	    ct->hour > 23 ||  ct->min > 59 || ct->sec > 59 ||
150	    (sizeof(time_t) == 4 && year > 2037)) {	/* time_t overflow */
151		if (ct_debug)
152			printf(" = EINVAL\n");
153		return (EINVAL);
154	}
155
156	/*
157	 * Compute days since start of time
158	 * First from years, then from months.
159	 */
160	days = 0;
161	for (i = POSIX_BASE_YEAR; i < year; i++)
162		days += days_in_year(i);
163
164	/* Months */
165	for (i = 1; i < ct->mon; i++)
166	  	days += days_in_month(year, i);
167	days += (ct->day - 1);
168
169	ts->tv_sec = (((time_t)days * 24 + ct->hour) * 60 + ct->min) * 60 +
170	    ct->sec;
171	ts->tv_nsec = ct->nsec;
172
173	if (ct_debug)
174		printf(" = %ld.%09ld\n", (long)ts->tv_sec, (long)ts->tv_nsec);
175	return (0);
176}
177
178void
179clock_ts_to_ct(struct timespec *ts, struct clocktime *ct)
180{
181	int i, year, days;
182	time_t rsec;	/* remainder seconds */
183	time_t secs;
184
185	secs = ts->tv_sec;
186	days = secs / SECDAY;
187	rsec = secs % SECDAY;
188
189	ct->dow = day_of_week(days);
190
191	/* Subtract out whole years, counting them in i. */
192	for (year = POSIX_BASE_YEAR; days >= days_in_year(year); year++)
193		days -= days_in_year(year);
194	ct->year = year;
195
196	/* Subtract out whole months, counting them in i. */
197	for (i = 1; days >= days_in_month(year, i); i++)
198		days -= days_in_month(year, i);
199	ct->mon = i;
200
201	/* Days are what is left over (+1) from all that. */
202	ct->day = days + 1;
203
204	/* Hours, minutes, seconds are easy */
205	ct->hour = rsec / 3600;
206	rsec = rsec % 3600;
207	ct->min  = rsec / 60;
208	rsec = rsec % 60;
209	ct->sec  = rsec;
210	ct->nsec = ts->tv_nsec;
211	if (ct_debug) {
212		printf("ts_to_ct(%ld.%09ld) = ",
213		    (long)ts->tv_sec, (long)ts->tv_nsec);
214		print_ct(ct);
215		printf("\n");
216	}
217}
218
219int
220utc_offset(void)
221{
222
223	return (tz_minuteswest * 60 + (wall_cmos_clock ? adjkerntz : 0));
224}
225