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$"); 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> 49265687Sbrooks#include <sys/limits.h> 5093835Stmm#include <sys/sysctl.h> 5193835Stmm#include <sys/timetc.h> 5293835Stmm 53162960Sphkint tz_minuteswest; 54162960Sphkint tz_dsttime; 55162960Sphk 5693835Stmm/* 57217195Sbz * The adjkerntz and wall_cmos_clock sysctls are in the "machdep" sysctl 58217195Sbz * namespace because they were misplaced there originally. 5993835Stmm */ 60217195Sbzstatic int adjkerntz; 6193835Stmmstatic int 6293835Stmmsysctl_machdep_adjkerntz(SYSCTL_HANDLER_ARGS) 6393835Stmm{ 6493835Stmm int error; 65178429Sphk error = sysctl_handle_int(oidp, oidp->oid_arg1, oidp->oid_arg2, req); 6693835Stmm if (!error && req->newptr) 6793835Stmm resettodr(); 6893835Stmm return (error); 6993835Stmm} 70162958SphkSYSCTL_PROC(_machdep, OID_AUTO, adjkerntz, CTLTYPE_INT|CTLFLAG_RW, 71215304Sbrucec &adjkerntz, 0, sysctl_machdep_adjkerntz, "I", 72215304Sbrucec "Local offset from UTC in seconds"); 73162958Sphk 74217195Sbzstatic int ct_debug; 75217195SbzSYSCTL_INT(_debug, OID_AUTO, clocktime, CTLFLAG_RW, 76217195Sbz &ct_debug, 0, "Enable printing of clocktime debugging"); 77217195Sbz 78217195Sbzstatic int wall_cmos_clock; 79217195SbzSYSCTL_INT(_machdep, OID_AUTO, wall_cmos_clock, CTLFLAG_RW, 80217195Sbz &wall_cmos_clock, 0, "Enables application of machdep.adjkerntz"); 81217195Sbz 82162958Sphk/*--------------------------------------------------------------------* 83162958Sphk * Generic routines to convert between a POSIX date 84162958Sphk * (seconds since 1/1/1970) and yr/mo/day/hr/min/sec 85162958Sphk * Derived from NetBSD arch/hp300/hp300/clock.c 86162958Sphk */ 87162958Sphk 88162958Sphk 89162958Sphk#define FEBRUARY 2 90162958Sphk#define days_in_year(y) (leapyear(y) ? 366 : 365) 91162958Sphk#define days_in_month(y, m) \ 92162958Sphk (month_days[(m) - 1] + (m == FEBRUARY ? leapyear(y) : 0)) 93162958Sphk/* Day of week. Days are counted from 1/1/1970, which was a Thursday */ 94162958Sphk#define day_of_week(days) (((days) + 4) % 7) 95162958Sphk 96162958Sphkstatic const int month_days[12] = { 97162958Sphk 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 98162958Sphk}; 99162958Sphk 100162958Sphk 10193835Stmm/* 10293835Stmm * This inline avoids some unnecessary modulo operations 10393835Stmm * as compared with the usual macro: 10493835Stmm * ( ((year % 4) == 0 && 10593835Stmm * (year % 100) != 0) || 10693835Stmm * ((year % 400) == 0) ) 10793835Stmm * It is otherwise equivalent. 10893835Stmm */ 109178162Sphkstatic int 11093835Stmmleapyear(int year) 11193835Stmm{ 11293835Stmm int rv = 0; 11393835Stmm 11493835Stmm if ((year & 3) == 0) { 11593835Stmm rv = 1; 11693835Stmm if ((year % 100) == 0) { 11793835Stmm rv = 0; 11893835Stmm if ((year % 400) == 0) 11993835Stmm rv = 1; 12093835Stmm } 12193835Stmm } 12293835Stmm return (rv); 12393835Stmm} 12493835Stmm 125178162Sphkstatic void 126178162Sphkprint_ct(struct clocktime *ct) 127178162Sphk{ 128178162Sphk printf("[%04d-%02d-%02d %02d:%02d:%02d]", 129178162Sphk ct->year, ct->mon, ct->day, 130178162Sphk ct->hour, ct->min, ct->sec); 131178162Sphk} 132178162Sphk 13393835Stmmint 13493835Stmmclock_ct_to_ts(struct clocktime *ct, struct timespec *ts) 13593835Stmm{ 13693835Stmm time_t secs; 13793835Stmm int i, year, days; 13893835Stmm 13993835Stmm year = ct->year; 14093835Stmm 141178162Sphk if (ct_debug) { 142178162Sphk printf("ct_to_ts("); 143178162Sphk print_ct(ct); 144178162Sphk printf(")"); 145178162Sphk } 146178162Sphk 14793835Stmm /* Sanity checks. */ 14893835Stmm if (ct->mon < 1 || ct->mon > 12 || ct->day < 1 || 14993835Stmm ct->day > days_in_month(year, ct->mon) || 15093835Stmm ct->hour > 23 || ct->min > 59 || ct->sec > 59 || 151265687Sbrooks (sizeof(time_t) == 4 && year > 2037)) { /* time_t overflow */ 152178162Sphk if (ct_debug) 153178162Sphk printf(" = EINVAL\n"); 15493835Stmm return (EINVAL); 155178162Sphk } 15693835Stmm 15793835Stmm /* 15893835Stmm * Compute days since start of time 15993835Stmm * First from years, then from months. 16093835Stmm */ 16193835Stmm days = 0; 16293835Stmm for (i = POSIX_BASE_YEAR; i < year; i++) 16393835Stmm days += days_in_year(i); 16493835Stmm 16593835Stmm /* Months */ 16693835Stmm for (i = 1; i < ct->mon; i++) 16793835Stmm days += days_in_month(year, i); 16893835Stmm days += (ct->day - 1); 16993835Stmm 17093835Stmm /* Add hours, minutes, seconds. */ 17193835Stmm secs = ((days * 24 + ct->hour) * 60 + ct->min) * 60 + ct->sec; 17293835Stmm 17393835Stmm ts->tv_sec = secs; 17493835Stmm ts->tv_nsec = ct->nsec; 175178162Sphk if (ct_debug) 176178164Sphk printf(" = %ld.%09ld\n", (long)ts->tv_sec, (long)ts->tv_nsec); 17793835Stmm return (0); 17893835Stmm} 17993835Stmm 18093835Stmmvoid 18193835Stmmclock_ts_to_ct(struct timespec *ts, struct clocktime *ct) 18293835Stmm{ 18393835Stmm int i, year, days; 18493835Stmm time_t rsec; /* remainder seconds */ 18593835Stmm time_t secs; 18693835Stmm 18793835Stmm secs = ts->tv_sec; 18893835Stmm days = secs / SECDAY; 18993835Stmm rsec = secs % SECDAY; 19093835Stmm 19193835Stmm ct->dow = day_of_week(days); 19293835Stmm 19393835Stmm /* Subtract out whole years, counting them in i. */ 19493835Stmm for (year = POSIX_BASE_YEAR; days >= days_in_year(year); year++) 19593835Stmm days -= days_in_year(year); 19693835Stmm ct->year = year; 19793835Stmm 19893835Stmm /* Subtract out whole months, counting them in i. */ 19993835Stmm for (i = 1; days >= days_in_month(year, i); i++) 20093835Stmm days -= days_in_month(year, i); 20193835Stmm ct->mon = i; 20293835Stmm 20393835Stmm /* Days are what is left over (+1) from all that. */ 20493835Stmm ct->day = days + 1; 20593835Stmm 20693835Stmm /* Hours, minutes, seconds are easy */ 20793835Stmm ct->hour = rsec / 3600; 20893835Stmm rsec = rsec % 3600; 20993835Stmm ct->min = rsec / 60; 21093835Stmm rsec = rsec % 60; 21193835Stmm ct->sec = rsec; 21293835Stmm ct->nsec = ts->tv_nsec; 213178162Sphk if (ct_debug) { 214178164Sphk printf("ts_to_ct(%ld.%09ld) = ", 215178164Sphk (long)ts->tv_sec, (long)ts->tv_nsec); 216178162Sphk print_ct(ct); 217178162Sphk printf("\n"); 218178162Sphk } 21993835Stmm} 220162962Sphk 221162962Sphkint 222162962Sphkutc_offset(void) 223162962Sphk{ 224162962Sphk 225162962Sphk return (tz_minuteswest * 60 + (wall_cmos_clock ? adjkerntz : 0)); 226162962Sphk} 227