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