2 * Copyright (c) 1988 University of Utah.
3 * Copyright (c) 1982, 1990, 1993
4 * The Regents of the University of California. All rights reserved.
6 * This code is derived from software contributed to Berkeley by
7 * the Systems Programming Group of the University of Utah Computer
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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
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
38 * from: src/sys/i386/isa/clock.c,v 1.176 2001/09/04
41 #include <sys/cdefs.h>
42 __FBSDID("$FreeBSD$");
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/kernel.h>
48 #include <sys/clock.h>
49 #include <sys/sysctl.h>
50 #include <sys/timetc.h>
56 * The adjkerntz and wall_cmos_clock sysctls are in the "machdep" sysctl
57 * namespace because they were misplaced there originally.
61 sysctl_machdep_adjkerntz(SYSCTL_HANDLER_ARGS)
64 error = sysctl_handle_int(oidp, oidp->oid_arg1, oidp->oid_arg2, req);
65 if (!error && req->newptr)
69 SYSCTL_PROC(_machdep, OID_AUTO, adjkerntz, CTLTYPE_INT|CTLFLAG_RW,
70 &adjkerntz, 0, sysctl_machdep_adjkerntz, "I",
71 "Local offset from UTC in seconds");
74 SYSCTL_INT(_debug, OID_AUTO, clocktime, CTLFLAG_RW,
75 &ct_debug, 0, "Enable printing of clocktime debugging");
77 static int wall_cmos_clock;
78 SYSCTL_INT(_machdep, OID_AUTO, wall_cmos_clock, CTLFLAG_RW,
79 &wall_cmos_clock, 0, "Enables application of machdep.adjkerntz");
81 /*--------------------------------------------------------------------*
82 * Generic routines to convert between a POSIX date
83 * (seconds since 1/1/1970) and yr/mo/day/hr/min/sec
84 * Derived from NetBSD arch/hp300/hp300/clock.c
89 #define days_in_year(y) (leapyear(y) ? 366 : 365)
90 #define days_in_month(y, m) \
91 (month_days[(m) - 1] + (m == FEBRUARY ? leapyear(y) : 0))
92 /* Day of week. Days are counted from 1/1/1970, which was a Thursday */
93 #define day_of_week(days) (((days) + 4) % 7)
95 static const int month_days[12] = {
96 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
101 * This inline avoids some unnecessary modulo operations
102 * as compared with the usual macro:
103 * ( ((year % 4) == 0 &&
104 * (year % 100) != 0) ||
105 * ((year % 400) == 0) )
106 * It is otherwise equivalent.
113 if ((year & 3) == 0) {
115 if ((year % 100) == 0) {
117 if ((year % 400) == 0)
125 print_ct(struct clocktime *ct)
127 printf("[%04d-%02d-%02d %02d:%02d:%02d]",
128 ct->year, ct->mon, ct->day,
129 ct->hour, ct->min, ct->sec);
133 clock_ct_to_ts(struct clocktime *ct, struct timespec *ts)
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 ct->year > 2037) { /* time_t overflow */
152 printf(" = EINVAL\n");
157 * Compute days since start of time
158 * First from years, then from months.
161 for (i = POSIX_BASE_YEAR; i < year; i++)
162 days += days_in_year(i);
165 for (i = 1; i < ct->mon; i++)
166 days += days_in_month(year, i);
167 days += (ct->day - 1);
169 /* Add hours, minutes, seconds. */
170 secs = ((days * 24 + ct->hour) * 60 + ct->min) * 60 + ct->sec;
173 ts->tv_nsec = ct->nsec;
175 printf(" = %ld.%09ld\n", (long)ts->tv_sec, (long)ts->tv_nsec);
180 clock_ts_to_ct(struct timespec *ts, struct clocktime *ct)
183 time_t rsec; /* remainder seconds */
187 days = secs / SECDAY;
188 rsec = secs % SECDAY;
190 ct->dow = day_of_week(days);
192 /* Subtract out whole years, counting them in i. */
193 for (year = POSIX_BASE_YEAR; days >= days_in_year(year); year++)
194 days -= days_in_year(year);
197 /* Subtract out whole months, counting them in i. */
198 for (i = 1; days >= days_in_month(year, i); i++)
199 days -= days_in_month(year, i);
202 /* Days are what is left over (+1) from all that. */
205 /* Hours, minutes, seconds are easy */
206 ct->hour = rsec / 3600;
211 ct->nsec = ts->tv_nsec;
213 printf("ts_to_ct(%ld.%09ld) = ",
214 (long)ts->tv_sec, (long)ts->tv_nsec);
224 return (tz_minuteswest * 60 + (wall_cmos_clock ? adjkerntz : 0));