subr_clock.c revision 215304
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 215304 2010-11-14 16:10:15Z brucec $");
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
52178162Sphk#define ct_debug bootverbose
53215281Sbrucecstatic int adjkerntz;		/* local offset from UTC in seconds */
54162970Sphkstatic int wall_cmos_clock;	/* wall CMOS clock assumed if != 0 */
5593835Stmm
56162960Sphkint tz_minuteswest;
57162960Sphkint tz_dsttime;
58162960Sphk
5993835Stmm/*
60178429Sphk * This have traditionally been in machdep, but should probably be moved to
6193835Stmm * kern.
6293835Stmm */
63158450SphkSYSCTL_INT(_machdep, OID_AUTO, wall_cmos_clock,
64215304Sbrucec    CTLFLAG_RW, &wall_cmos_clock, 0, "CMOS clock keeps wall time");
6593835Stmm
6693835Stmmstatic int
6793835Stmmsysctl_machdep_adjkerntz(SYSCTL_HANDLER_ARGS)
6893835Stmm{
6993835Stmm	int error;
70178429Sphk	error = sysctl_handle_int(oidp, oidp->oid_arg1, oidp->oid_arg2, req);
7193835Stmm	if (!error && req->newptr)
7293835Stmm		resettodr();
7393835Stmm	return (error);
7493835Stmm}
7593835Stmm
76162958SphkSYSCTL_PROC(_machdep, OID_AUTO, adjkerntz, CTLTYPE_INT|CTLFLAG_RW,
77215304Sbrucec    &adjkerntz, 0, sysctl_machdep_adjkerntz, "I",
78215304Sbrucec    "Local offset from UTC in seconds");
79162958Sphk
80162958Sphk/*--------------------------------------------------------------------*
81162958Sphk * Generic routines to convert between a POSIX date
82162958Sphk * (seconds since 1/1/1970) and yr/mo/day/hr/min/sec
83162958Sphk * Derived from NetBSD arch/hp300/hp300/clock.c
84162958Sphk */
85162958Sphk
86162958Sphk
87162958Sphk#define	FEBRUARY	2
88162958Sphk#define	days_in_year(y) 	(leapyear(y) ? 366 : 365)
89162958Sphk#define	days_in_month(y, m) \
90162958Sphk	(month_days[(m) - 1] + (m == FEBRUARY ? leapyear(y) : 0))
91162958Sphk/* Day of week. Days are counted from 1/1/1970, which was a Thursday */
92162958Sphk#define	day_of_week(days)	(((days) + 4) % 7)
93162958Sphk
94162958Sphkstatic const int month_days[12] = {
95162958Sphk	31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
96162958Sphk};
97162958Sphk
98162958Sphk
9993835Stmm/*
10093835Stmm * This inline avoids some unnecessary modulo operations
10193835Stmm * as compared with the usual macro:
10293835Stmm *   ( ((year % 4) == 0 &&
10393835Stmm *      (year % 100) != 0) ||
10493835Stmm *     ((year % 400) == 0) )
10593835Stmm * It is otherwise equivalent.
10693835Stmm */
107178162Sphkstatic int
10893835Stmmleapyear(int year)
10993835Stmm{
11093835Stmm	int rv = 0;
11193835Stmm
11293835Stmm	if ((year & 3) == 0) {
11393835Stmm		rv = 1;
11493835Stmm		if ((year % 100) == 0) {
11593835Stmm			rv = 0;
11693835Stmm			if ((year % 400) == 0)
11793835Stmm				rv = 1;
11893835Stmm		}
11993835Stmm	}
12093835Stmm	return (rv);
12193835Stmm}
12293835Stmm
123178162Sphkstatic void
124178162Sphkprint_ct(struct clocktime *ct)
125178162Sphk{
126178162Sphk	printf("[%04d-%02d-%02d %02d:%02d:%02d]",
127178162Sphk	    ct->year, ct->mon, ct->day,
128178162Sphk	    ct->hour, ct->min, ct->sec);
129178162Sphk}
130178162Sphk
13193835Stmmint
13293835Stmmclock_ct_to_ts(struct clocktime *ct, struct timespec *ts)
13393835Stmm{
13493835Stmm	time_t secs;
13593835Stmm	int i, year, days;
13693835Stmm
13793835Stmm	year = ct->year;
13893835Stmm
139178162Sphk	if (ct_debug) {
140178162Sphk		printf("ct_to_ts(");
141178162Sphk		print_ct(ct);
142178162Sphk		printf(")");
143178162Sphk	}
144178162Sphk
14593835Stmm	/* Sanity checks. */
14693835Stmm	if (ct->mon < 1 || ct->mon > 12 || ct->day < 1 ||
14793835Stmm	    ct->day > days_in_month(year, ct->mon) ||
14893835Stmm	    ct->hour > 23 ||  ct->min > 59 || ct->sec > 59 ||
149178162Sphk	    ct->year > 2037) {		/* time_t overflow */
150178162Sphk		if (ct_debug)
151178162Sphk			printf(" = EINVAL\n");
15293835Stmm		return (EINVAL);
153178162Sphk	}
15493835Stmm
15593835Stmm	/*
15693835Stmm	 * Compute days since start of time
15793835Stmm	 * First from years, then from months.
15893835Stmm	 */
15993835Stmm	days = 0;
16093835Stmm	for (i = POSIX_BASE_YEAR; i < year; i++)
16193835Stmm		days += days_in_year(i);
16293835Stmm
16393835Stmm	/* Months */
16493835Stmm	for (i = 1; i < ct->mon; i++)
16593835Stmm	  	days += days_in_month(year, i);
16693835Stmm	days += (ct->day - 1);
16793835Stmm
16893835Stmm	/* Add hours, minutes, seconds. */
16993835Stmm	secs = ((days * 24 + ct->hour) * 60 + ct->min) * 60 + ct->sec;
17093835Stmm
17193835Stmm	ts->tv_sec = secs;
17293835Stmm	ts->tv_nsec = ct->nsec;
173178162Sphk	if (ct_debug)
174178164Sphk		printf(" = %ld.%09ld\n", (long)ts->tv_sec, (long)ts->tv_nsec);
17593835Stmm	return (0);
17693835Stmm}
17793835Stmm
17893835Stmmvoid
17993835Stmmclock_ts_to_ct(struct timespec *ts, struct clocktime *ct)
18093835Stmm{
18193835Stmm	int i, year, days;
18293835Stmm	time_t rsec;	/* remainder seconds */
18393835Stmm	time_t secs;
18493835Stmm
18593835Stmm	secs = ts->tv_sec;
18693835Stmm	days = secs / SECDAY;
18793835Stmm	rsec = secs % SECDAY;
18893835Stmm
18993835Stmm	ct->dow = day_of_week(days);
19093835Stmm
19193835Stmm	/* Subtract out whole years, counting them in i. */
19293835Stmm	for (year = POSIX_BASE_YEAR; days >= days_in_year(year); year++)
19393835Stmm		days -= days_in_year(year);
19493835Stmm	ct->year = year;
19593835Stmm
19693835Stmm	/* Subtract out whole months, counting them in i. */
19793835Stmm	for (i = 1; days >= days_in_month(year, i); i++)
19893835Stmm		days -= days_in_month(year, i);
19993835Stmm	ct->mon = i;
20093835Stmm
20193835Stmm	/* Days are what is left over (+1) from all that. */
20293835Stmm	ct->day = days + 1;
20393835Stmm
20493835Stmm	/* Hours, minutes, seconds are easy */
20593835Stmm	ct->hour = rsec / 3600;
20693835Stmm	rsec = rsec % 3600;
20793835Stmm	ct->min  = rsec / 60;
20893835Stmm	rsec = rsec % 60;
20993835Stmm	ct->sec  = rsec;
21093835Stmm	ct->nsec = ts->tv_nsec;
211178162Sphk	if (ct_debug) {
212178164Sphk		printf("ts_to_ct(%ld.%09ld) = ",
213178164Sphk		    (long)ts->tv_sec, (long)ts->tv_nsec);
214178162Sphk		print_ct(ct);
215178162Sphk		printf("\n");
216178162Sphk	}
21793835Stmm}
218162962Sphk
219162962Sphkint
220162962Sphkutc_offset(void)
221162962Sphk{
222162962Sphk
223162962Sphk	return (tz_minuteswest * 60 + (wall_cmos_clock ? adjkerntz : 0));
224162962Sphk}
225