subr_clock.c revision 217195
1139804Simp/*-
293835Stmm * Copyright (c) 1988 University of Utah.
393835Stmm * Copyright (c) 1982, 1990, 1993
493835Stmm *	The Regents of the University of California.  All rights reserved.
593835Stmm *
693835Stmm * This code is derived from software contributed to Berkeley by
793835Stmm * the Systems Programming Group of the University of Utah Computer
893835Stmm * Science Department.
993835Stmm *
1093835Stmm * Redistribution and use in source and binary forms, with or without
1193835Stmm * modification, are permitted provided that the following conditions
1293835Stmm * are met:
1393835Stmm * 1. Redistributions of source code must retain the above copyright
1493835Stmm *    notice, this list of conditions and the following disclaimer.
1593835Stmm * 2. Redistributions in binary form must reproduce the above copyright
1693835Stmm *    notice, this list of conditions and the following disclaimer in the
1793835Stmm *    documentation and/or other materials provided with the distribution.
1893835Stmm * 4. Neither the name of the University nor the names of its contributors
1993835Stmm *    may be used to endorse or promote products derived from this software
2093835Stmm *    without specific prior written permission.
2193835Stmm *
2293835Stmm * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2393835Stmm * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2493835Stmm * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2593835Stmm * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2693835Stmm * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2793835Stmm * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2893835Stmm * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2993835Stmm * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3093835Stmm * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3193835Stmm * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3293835Stmm * SUCH DAMAGE.
3393835Stmm *
3493835Stmm *	from: Utah $Hdr: clock.c 1.18 91/01/21$
3593835Stmm *	from: @(#)clock.c	8.2 (Berkeley) 1/12/94
3693835Stmm *	from: NetBSD: clock_subr.c,v 1.6 2001/07/07 17:04:02 thorpej Exp
3793835Stmm *	and
3893835Stmm *	from: src/sys/i386/isa/clock.c,v 1.176 2001/09/04
3993835Stmm */
4093835Stmm
41116182Sobrien#include <sys/cdefs.h>
42116182Sobrien__FBSDID("$FreeBSD: head/sys/kern/subr_clock.c 217195 2011-01-09 14:34:56Z bz $");
43116182Sobrien
4493835Stmm#include <sys/param.h>
4593835Stmm#include <sys/systm.h>
4693835Stmm#include <sys/kernel.h>
4793835Stmm#include <sys/bus.h>
4893835Stmm#include <sys/clock.h>
4993835Stmm#include <sys/sysctl.h>
5093835Stmm#include <sys/timetc.h>
5193835Stmm
52162960Sphkint tz_minuteswest;
53162960Sphkint tz_dsttime;
54162960Sphk
5593835Stmm/*
56217195Sbz * The adjkerntz and wall_cmos_clock sysctls are in the "machdep" sysctl
57217195Sbz * namespace because they were misplaced there originally.
5893835Stmm */
59217195Sbzstatic int adjkerntz;
6093835Stmmstatic int
6193835Stmmsysctl_machdep_adjkerntz(SYSCTL_HANDLER_ARGS)
6293835Stmm{
6393835Stmm	int error;
64178429Sphk	error = sysctl_handle_int(oidp, oidp->oid_arg1, oidp->oid_arg2, req);
6593835Stmm	if (!error && req->newptr)
6693835Stmm		resettodr();
6793835Stmm	return (error);
6893835Stmm}
69162958SphkSYSCTL_PROC(_machdep, OID_AUTO, adjkerntz, CTLTYPE_INT|CTLFLAG_RW,
70215304Sbrucec    &adjkerntz, 0, sysctl_machdep_adjkerntz, "I",
71215304Sbrucec    "Local offset from UTC in seconds");
72162958Sphk
73217195Sbzstatic int ct_debug;
74217195SbzSYSCTL_INT(_debug, OID_AUTO, clocktime, CTLFLAG_RW,
75217195Sbz    &ct_debug, 0, "Enable printing of clocktime debugging");
76217195Sbz
77217195Sbzstatic int wall_cmos_clock;
78217195SbzSYSCTL_INT(_machdep, OID_AUTO, wall_cmos_clock, CTLFLAG_RW,
79217195Sbz    &wall_cmos_clock, 0, "Enables application of machdep.adjkerntz");
80217195Sbz
81162958Sphk/*--------------------------------------------------------------------*
82162958Sphk * Generic routines to convert between a POSIX date
83162958Sphk * (seconds since 1/1/1970) and yr/mo/day/hr/min/sec
84162958Sphk * Derived from NetBSD arch/hp300/hp300/clock.c
85162958Sphk */
86162958Sphk
87162958Sphk
88162958Sphk#define	FEBRUARY	2
89162958Sphk#define	days_in_year(y) 	(leapyear(y) ? 366 : 365)
90162958Sphk#define	days_in_month(y, m) \
91162958Sphk	(month_days[(m) - 1] + (m == FEBRUARY ? leapyear(y) : 0))
92162958Sphk/* Day of week. Days are counted from 1/1/1970, which was a Thursday */
93162958Sphk#define	day_of_week(days)	(((days) + 4) % 7)
94162958Sphk
95162958Sphkstatic const int month_days[12] = {
96162958Sphk	31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
97162958Sphk};
98162958Sphk
99162958Sphk
10093835Stmm/*
10193835Stmm * This inline avoids some unnecessary modulo operations
10293835Stmm * as compared with the usual macro:
10393835Stmm *   ( ((year % 4) == 0 &&
10493835Stmm *      (year % 100) != 0) ||
10593835Stmm *     ((year % 400) == 0) )
10693835Stmm * It is otherwise equivalent.
10793835Stmm */
108178162Sphkstatic int
10993835Stmmleapyear(int year)
11093835Stmm{
11193835Stmm	int rv = 0;
11293835Stmm
11393835Stmm	if ((year & 3) == 0) {
11493835Stmm		rv = 1;
11593835Stmm		if ((year % 100) == 0) {
11693835Stmm			rv = 0;
11793835Stmm			if ((year % 400) == 0)
11893835Stmm				rv = 1;
11993835Stmm		}
12093835Stmm	}
12193835Stmm	return (rv);
12293835Stmm}
12393835Stmm
124178162Sphkstatic void
125178162Sphkprint_ct(struct clocktime *ct)
126178162Sphk{
127178162Sphk	printf("[%04d-%02d-%02d %02d:%02d:%02d]",
128178162Sphk	    ct->year, ct->mon, ct->day,
129178162Sphk	    ct->hour, ct->min, ct->sec);
130178162Sphk}
131178162Sphk
13293835Stmmint
13393835Stmmclock_ct_to_ts(struct clocktime *ct, struct timespec *ts)
13493835Stmm{
13593835Stmm	time_t secs;
13693835Stmm	int i, year, days;
13793835Stmm
13893835Stmm	year = ct->year;
13993835Stmm
140178162Sphk	if (ct_debug) {
141178162Sphk		printf("ct_to_ts(");
142178162Sphk		print_ct(ct);
143178162Sphk		printf(")");
144178162Sphk	}
145178162Sphk
14693835Stmm	/* Sanity checks. */
14793835Stmm	if (ct->mon < 1 || ct->mon > 12 || ct->day < 1 ||
14893835Stmm	    ct->day > days_in_month(year, ct->mon) ||
14993835Stmm	    ct->hour > 23 ||  ct->min > 59 || ct->sec > 59 ||
150178162Sphk	    ct->year > 2037) {		/* time_t overflow */
151178162Sphk		if (ct_debug)
152178162Sphk			printf(" = EINVAL\n");
15393835Stmm		return (EINVAL);
154178162Sphk	}
15593835Stmm
15693835Stmm	/*
15793835Stmm	 * Compute days since start of time
15893835Stmm	 * First from years, then from months.
15993835Stmm	 */
16093835Stmm	days = 0;
16193835Stmm	for (i = POSIX_BASE_YEAR; i < year; i++)
16293835Stmm		days += days_in_year(i);
16393835Stmm
16493835Stmm	/* Months */
16593835Stmm	for (i = 1; i < ct->mon; i++)
16693835Stmm	  	days += days_in_month(year, i);
16793835Stmm	days += (ct->day - 1);
16893835Stmm
16993835Stmm	/* Add hours, minutes, seconds. */
17093835Stmm	secs = ((days * 24 + ct->hour) * 60 + ct->min) * 60 + ct->sec;
17193835Stmm
17293835Stmm	ts->tv_sec = secs;
17393835Stmm	ts->tv_nsec = ct->nsec;
174178162Sphk	if (ct_debug)
175178164Sphk		printf(" = %ld.%09ld\n", (long)ts->tv_sec, (long)ts->tv_nsec);
17693835Stmm	return (0);
17793835Stmm}
17893835Stmm
17993835Stmmvoid
18093835Stmmclock_ts_to_ct(struct timespec *ts, struct clocktime *ct)
18193835Stmm{
18293835Stmm	int i, year, days;
18393835Stmm	time_t rsec;	/* remainder seconds */
18493835Stmm	time_t secs;
18593835Stmm
18693835Stmm	secs = ts->tv_sec;
18793835Stmm	days = secs / SECDAY;
18893835Stmm	rsec = secs % SECDAY;
18993835Stmm
19093835Stmm	ct->dow = day_of_week(days);
19193835Stmm
19293835Stmm	/* Subtract out whole years, counting them in i. */
19393835Stmm	for (year = POSIX_BASE_YEAR; days >= days_in_year(year); year++)
19493835Stmm		days -= days_in_year(year);
19593835Stmm	ct->year = year;
19693835Stmm
19793835Stmm	/* Subtract out whole months, counting them in i. */
19893835Stmm	for (i = 1; days >= days_in_month(year, i); i++)
19993835Stmm		days -= days_in_month(year, i);
20093835Stmm	ct->mon = i;
20193835Stmm
20293835Stmm	/* Days are what is left over (+1) from all that. */
20393835Stmm	ct->day = days + 1;
20493835Stmm
20593835Stmm	/* Hours, minutes, seconds are easy */
20693835Stmm	ct->hour = rsec / 3600;
20793835Stmm	rsec = rsec % 3600;
20893835Stmm	ct->min  = rsec / 60;
20993835Stmm	rsec = rsec % 60;
21093835Stmm	ct->sec  = rsec;
21193835Stmm	ct->nsec = ts->tv_nsec;
212178162Sphk	if (ct_debug) {
213178164Sphk		printf("ts_to_ct(%ld.%09ld) = ",
214178164Sphk		    (long)ts->tv_sec, (long)ts->tv_nsec);
215178162Sphk		print_ct(ct);
216178162Sphk		printf("\n");
217178162Sphk	}
21893835Stmm}
219162962Sphk
220162962Sphkint
221162962Sphkutc_offset(void)
222162962Sphk{
223162962Sphk
224162962Sphk	return (tz_minuteswest * 60 + (wall_cmos_clock ? adjkerntz : 0));
225162962Sphk}
226