19454b2d8SWarner Losh /*- 251369649SPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause 351369649SPedro F. Giffuni * 4d7f7792eSThomas Moestl * Copyright (c) 1988 University of Utah. 5d7f7792eSThomas Moestl * Copyright (c) 1982, 1990, 1993 6d7f7792eSThomas Moestl * The Regents of the University of California. All rights reserved. 7d7f7792eSThomas Moestl * 8d7f7792eSThomas Moestl * This code is derived from software contributed to Berkeley by 9d7f7792eSThomas Moestl * the Systems Programming Group of the University of Utah Computer 10d7f7792eSThomas Moestl * Science Department. 11d7f7792eSThomas Moestl * 12d7f7792eSThomas Moestl * Redistribution and use in source and binary forms, with or without 13d7f7792eSThomas Moestl * modification, are permitted provided that the following conditions 14d7f7792eSThomas Moestl * are met: 15d7f7792eSThomas Moestl * 1. Redistributions of source code must retain the above copyright 16d7f7792eSThomas Moestl * notice, this list of conditions and the following disclaimer. 17d7f7792eSThomas Moestl * 2. Redistributions in binary form must reproduce the above copyright 18d7f7792eSThomas Moestl * notice, this list of conditions and the following disclaimer in the 19d7f7792eSThomas Moestl * documentation and/or other materials provided with the distribution. 2069a28758SEd Maste * 3. Neither the name of the University nor the names of its contributors 21d7f7792eSThomas Moestl * may be used to endorse or promote products derived from this software 22d7f7792eSThomas Moestl * without specific prior written permission. 23d7f7792eSThomas Moestl * 24d7f7792eSThomas Moestl * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25d7f7792eSThomas Moestl * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26d7f7792eSThomas Moestl * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27d7f7792eSThomas Moestl * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28d7f7792eSThomas Moestl * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29d7f7792eSThomas Moestl * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30d7f7792eSThomas Moestl * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31d7f7792eSThomas Moestl * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32d7f7792eSThomas Moestl * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33d7f7792eSThomas Moestl * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34d7f7792eSThomas Moestl * SUCH DAMAGE. 35d7f7792eSThomas Moestl * 36d7f7792eSThomas Moestl * from: Utah $Hdr: clock.c 1.18 91/01/21$ 37d7f7792eSThomas Moestl * from: @(#)clock.c 8.2 (Berkeley) 1/12/94 38d7f7792eSThomas Moestl * from: NetBSD: clock_subr.c,v 1.6 2001/07/07 17:04:02 thorpej Exp 39d7f7792eSThomas Moestl * and 40d7f7792eSThomas Moestl * from: src/sys/i386/isa/clock.c,v 1.176 2001/09/04 41d7f7792eSThomas Moestl */ 42d7f7792eSThomas Moestl 43677b542eSDavid E. O'Brien #include <sys/cdefs.h> 44677b542eSDavid E. O'Brien __FBSDID("$FreeBSD$"); 45677b542eSDavid E. O'Brien 46d7f7792eSThomas Moestl #include <sys/param.h> 47d7f7792eSThomas Moestl #include <sys/systm.h> 48d7f7792eSThomas Moestl #include <sys/kernel.h> 49d7f7792eSThomas Moestl #include <sys/bus.h> 50d7f7792eSThomas Moestl #include <sys/clock.h> 51ee9bc599SBrooks Davis #include <sys/limits.h> 52d7f7792eSThomas Moestl #include <sys/sysctl.h> 53d7f7792eSThomas Moestl #include <sys/timetc.h> 54d7f7792eSThomas Moestl 5594d67e0fSPoul-Henning Kamp int tz_minuteswest; 5694d67e0fSPoul-Henning Kamp int tz_dsttime; 5794d67e0fSPoul-Henning Kamp 58d7f7792eSThomas Moestl /* 598d12fab9SBjoern A. Zeeb * The adjkerntz and wall_cmos_clock sysctls are in the "machdep" sysctl 608d12fab9SBjoern A. Zeeb * namespace because they were misplaced there originally. 61d7f7792eSThomas Moestl */ 628d12fab9SBjoern A. Zeeb static int adjkerntz; 63d7f7792eSThomas Moestl static int 64d7f7792eSThomas Moestl sysctl_machdep_adjkerntz(SYSCTL_HANDLER_ARGS) 65d7f7792eSThomas Moestl { 66d7f7792eSThomas Moestl int error; 679b4a8ab7SPoul-Henning Kamp error = sysctl_handle_int(oidp, oidp->oid_arg1, oidp->oid_arg2, req); 68d7f7792eSThomas Moestl if (!error && req->newptr) 69d7f7792eSThomas Moestl resettodr(); 70d7f7792eSThomas Moestl return (error); 71d7f7792eSThomas Moestl } 723a0e6f92SKonstantin Belousov SYSCTL_PROC(_machdep, OID_AUTO, adjkerntz, CTLTYPE_INT | CTLFLAG_RW | 733a0e6f92SKonstantin Belousov CTLFLAG_MPSAFE, &adjkerntz, 0, sysctl_machdep_adjkerntz, "I", 74b389be97SRebecca Cran "Local offset from UTC in seconds"); 75b69f71ebSPoul-Henning Kamp 768d12fab9SBjoern A. Zeeb static int ct_debug; 77932d14c6SIan Lepore SYSCTL_INT(_debug, OID_AUTO, clocktime, CTLFLAG_RWTUN, 788d12fab9SBjoern A. Zeeb &ct_debug, 0, "Enable printing of clocktime debugging"); 798d12fab9SBjoern A. Zeeb 808d12fab9SBjoern A. Zeeb static int wall_cmos_clock; 818d12fab9SBjoern A. Zeeb SYSCTL_INT(_machdep, OID_AUTO, wall_cmos_clock, CTLFLAG_RW, 828d12fab9SBjoern A. Zeeb &wall_cmos_clock, 0, "Enables application of machdep.adjkerntz"); 838d12fab9SBjoern A. Zeeb 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 102f7afe767SIan Lepore /* 103f7afe767SIan Lepore * Optimization: using a precomputed count of days between POSIX_BASE_YEAR and 104f7afe767SIan Lepore * some recent year avoids lots of unnecessary loop iterations in conversion. 105f7afe767SIan Lepore * recent_base_days is the number of days before the start of recent_base_year. 106f7afe767SIan Lepore */ 107f7afe767SIan Lepore static const int recent_base_year = 2017; 108f7afe767SIan Lepore static const int recent_base_days = 17167; 109b69f71ebSPoul-Henning Kamp 110d7f7792eSThomas Moestl /* 111d7f7792eSThomas Moestl * This inline avoids some unnecessary modulo operations 112d7f7792eSThomas Moestl * as compared with the usual macro: 113d7f7792eSThomas Moestl * ( ((year % 4) == 0 && 114d7f7792eSThomas Moestl * (year % 100) != 0) || 115d7f7792eSThomas Moestl * ((year % 400) == 0) ) 116d7f7792eSThomas Moestl * It is otherwise equivalent. 117d7f7792eSThomas Moestl */ 118c9ad6040SPoul-Henning Kamp static int 119d7f7792eSThomas Moestl leapyear(int year) 120d7f7792eSThomas Moestl { 121d7f7792eSThomas Moestl int rv = 0; 122d7f7792eSThomas Moestl 123d7f7792eSThomas Moestl if ((year & 3) == 0) { 124d7f7792eSThomas Moestl rv = 1; 125d7f7792eSThomas Moestl if ((year % 100) == 0) { 126d7f7792eSThomas Moestl rv = 0; 127d7f7792eSThomas Moestl if ((year % 400) == 0) 128d7f7792eSThomas Moestl rv = 1; 129d7f7792eSThomas Moestl } 130d7f7792eSThomas Moestl } 131d7f7792eSThomas Moestl return (rv); 132d7f7792eSThomas Moestl } 133d7f7792eSThomas Moestl 134c9ad6040SPoul-Henning Kamp static void 135*ce75945dSIan Lepore print_ct(const struct clocktime *ct) 136c9ad6040SPoul-Henning Kamp { 137c9ad6040SPoul-Henning Kamp printf("[%04d-%02d-%02d %02d:%02d:%02d]", 138c9ad6040SPoul-Henning Kamp ct->year, ct->mon, ct->day, 139c9ad6040SPoul-Henning Kamp ct->hour, ct->min, ct->sec); 140c9ad6040SPoul-Henning Kamp } 141c9ad6040SPoul-Henning Kamp 142d7f7792eSThomas Moestl int 143*ce75945dSIan Lepore clock_ct_to_ts(const struct clocktime *ct, struct timespec *ts) 144d7f7792eSThomas Moestl { 145d7f7792eSThomas Moestl int i, year, days; 146d7f7792eSThomas Moestl 147c9ad6040SPoul-Henning Kamp if (ct_debug) { 148c9ad6040SPoul-Henning Kamp printf("ct_to_ts("); 149c9ad6040SPoul-Henning Kamp print_ct(ct); 150c9ad6040SPoul-Henning Kamp printf(")"); 151c9ad6040SPoul-Henning Kamp } 152c9ad6040SPoul-Henning Kamp 153f1b21e2cSIan Lepore /* 154f1b21e2cSIan Lepore * Many realtime clocks store the year as 2-digit BCD; pivot on 70 to 155f1b21e2cSIan Lepore * determine century. Some clocks have a "century bit" and drivers do 156f1b21e2cSIan Lepore * year += 100, so interpret values between 70-199 as relative to 1900. 157f1b21e2cSIan Lepore */ 158f1b21e2cSIan Lepore year = ct->year; 159f1b21e2cSIan Lepore if (year < 70) 160f1b21e2cSIan Lepore year += 2000; 161f1b21e2cSIan Lepore else if (year < 200) 162f1b21e2cSIan Lepore year += 1900; 163f1b21e2cSIan Lepore 164d7f7792eSThomas Moestl /* Sanity checks. */ 165d7f7792eSThomas Moestl if (ct->mon < 1 || ct->mon > 12 || ct->day < 1 || 166d7f7792eSThomas Moestl ct->day > days_in_month(year, ct->mon) || 167f1b21e2cSIan Lepore ct->hour > 23 || ct->min > 59 || ct->sec > 59 || year < 1970 || 168ee9bc599SBrooks Davis (sizeof(time_t) == 4 && year > 2037)) { /* time_t overflow */ 169c9ad6040SPoul-Henning Kamp if (ct_debug) 170c9ad6040SPoul-Henning Kamp printf(" = EINVAL\n"); 171d7f7792eSThomas Moestl return (EINVAL); 172c9ad6040SPoul-Henning Kamp } 173d7f7792eSThomas Moestl 174d7f7792eSThomas Moestl /* 175d7f7792eSThomas Moestl * Compute days since start of time 176d7f7792eSThomas Moestl * First from years, then from months. 177d7f7792eSThomas Moestl */ 178f7afe767SIan Lepore if (year >= recent_base_year) { 179f7afe767SIan Lepore i = recent_base_year; 180f7afe767SIan Lepore days = recent_base_days; 181f7afe767SIan Lepore } else { 182f7afe767SIan Lepore i = POSIX_BASE_YEAR; 183d7f7792eSThomas Moestl days = 0; 184f7afe767SIan Lepore } 185f7afe767SIan Lepore for (; i < year; i++) 186d7f7792eSThomas Moestl days += days_in_year(i); 187d7f7792eSThomas Moestl 188d7f7792eSThomas Moestl /* Months */ 189d7f7792eSThomas Moestl for (i = 1; i < ct->mon; i++) 190d7f7792eSThomas Moestl days += days_in_month(year, i); 191d7f7792eSThomas Moestl days += (ct->day - 1); 192d7f7792eSThomas Moestl 193fe21241eSKonstantin Belousov ts->tv_sec = (((time_t)days * 24 + ct->hour) * 60 + ct->min) * 60 + 194fe21241eSKonstantin Belousov ct->sec; 195d7f7792eSThomas Moestl ts->tv_nsec = ct->nsec; 196fe21241eSKonstantin Belousov 197c9ad6040SPoul-Henning Kamp if (ct_debug) 198932d14c6SIan Lepore printf(" = %jd.%09ld\n", (intmax_t)ts->tv_sec, ts->tv_nsec); 199d7f7792eSThomas Moestl return (0); 200d7f7792eSThomas Moestl } 201d7f7792eSThomas Moestl 20286299375SIan Lepore int 203*ce75945dSIan Lepore clock_bcd_to_ts(const struct bcd_clocktime *bct, struct timespec *ts, bool ampm) 20486299375SIan Lepore { 20586299375SIan Lepore struct clocktime ct; 20686299375SIan Lepore int bcent, byear; 20786299375SIan Lepore 20886299375SIan Lepore /* 20986299375SIan Lepore * Year may come in as 2-digit or 4-digit BCD. Split the value into 21086299375SIan Lepore * separate BCD century and year values for validation and conversion. 21186299375SIan Lepore */ 21286299375SIan Lepore bcent = bct->year >> 8; 21386299375SIan Lepore byear = bct->year & 0xff; 21486299375SIan Lepore 21586299375SIan Lepore /* 21686299375SIan Lepore * Ensure that all values are valid BCD numbers, to avoid assertions in 21786299375SIan Lepore * the BCD-to-binary conversion routines. clock_ct_to_ts() will further 21886299375SIan Lepore * validate the field ranges (such as 0 <= min <= 59) during conversion. 21986299375SIan Lepore */ 22086299375SIan Lepore if (!validbcd(bcent) || !validbcd(byear) || !validbcd(bct->mon) || 22186299375SIan Lepore !validbcd(bct->day) || !validbcd(bct->hour) || 22286299375SIan Lepore !validbcd(bct->min) || !validbcd(bct->sec)) { 22386299375SIan Lepore if (ct_debug) 22486299375SIan Lepore printf("clock_bcd_to_ts: bad BCD: " 22586299375SIan Lepore "[%04x-%02x-%02x %02x:%02x:%02x]\n", 22686299375SIan Lepore bct->year, bct->mon, bct->day, 22786299375SIan Lepore bct->hour, bct->min, bct->sec); 22886299375SIan Lepore return (EINVAL); 22986299375SIan Lepore } 23086299375SIan Lepore 23186299375SIan Lepore ct.year = FROMBCD(byear) + FROMBCD(bcent) * 100; 23286299375SIan Lepore ct.mon = FROMBCD(bct->mon); 23386299375SIan Lepore ct.day = FROMBCD(bct->day); 23486299375SIan Lepore ct.hour = FROMBCD(bct->hour); 23586299375SIan Lepore ct.min = FROMBCD(bct->min); 23686299375SIan Lepore ct.sec = FROMBCD(bct->sec); 23786299375SIan Lepore ct.dow = bct->dow; 23886299375SIan Lepore ct.nsec = bct->nsec; 23986299375SIan Lepore 24086299375SIan Lepore /* If asked to handle am/pm, convert from 12hr+pmflag to 24hr. */ 24186299375SIan Lepore if (ampm) { 24286299375SIan Lepore if (ct.hour == 12) 24386299375SIan Lepore ct.hour = 0; 24486299375SIan Lepore if (bct->ispm) 24586299375SIan Lepore ct.hour += 12; 24686299375SIan Lepore } 24786299375SIan Lepore 24886299375SIan Lepore return (clock_ct_to_ts(&ct, ts)); 24986299375SIan Lepore } 25086299375SIan Lepore 251d7f7792eSThomas Moestl void 252*ce75945dSIan Lepore clock_ts_to_ct(const struct timespec *ts, struct clocktime *ct) 253d7f7792eSThomas Moestl { 25490a79ac5SConrad Meyer time_t i, year, days; 255d7f7792eSThomas Moestl time_t rsec; /* remainder seconds */ 256d7f7792eSThomas Moestl time_t secs; 257d7f7792eSThomas Moestl 258d7f7792eSThomas Moestl secs = ts->tv_sec; 259d7f7792eSThomas Moestl days = secs / SECDAY; 260d7f7792eSThomas Moestl rsec = secs % SECDAY; 261d7f7792eSThomas Moestl 262d7f7792eSThomas Moestl ct->dow = day_of_week(days); 263d7f7792eSThomas Moestl 264f7afe767SIan Lepore /* Subtract out whole years. */ 265f7afe767SIan Lepore if (days >= recent_base_days) { 266f7afe767SIan Lepore year = recent_base_year; 267f7afe767SIan Lepore days -= recent_base_days; 268f7afe767SIan Lepore } else { 269f7afe767SIan Lepore year = POSIX_BASE_YEAR; 270f7afe767SIan Lepore } 271f7afe767SIan Lepore for (; days >= days_in_year(year); year++) 272d7f7792eSThomas Moestl days -= days_in_year(year); 273d7f7792eSThomas Moestl ct->year = year; 274d7f7792eSThomas Moestl 275d7f7792eSThomas Moestl /* Subtract out whole months, counting them in i. */ 276d7f7792eSThomas Moestl for (i = 1; days >= days_in_month(year, i); i++) 277d7f7792eSThomas Moestl days -= days_in_month(year, i); 278d7f7792eSThomas Moestl ct->mon = i; 279d7f7792eSThomas Moestl 280d7f7792eSThomas Moestl /* Days are what is left over (+1) from all that. */ 281d7f7792eSThomas Moestl ct->day = days + 1; 282d7f7792eSThomas Moestl 283d7f7792eSThomas Moestl /* Hours, minutes, seconds are easy */ 284d7f7792eSThomas Moestl ct->hour = rsec / 3600; 285d7f7792eSThomas Moestl rsec = rsec % 3600; 286d7f7792eSThomas Moestl ct->min = rsec / 60; 287d7f7792eSThomas Moestl rsec = rsec % 60; 288d7f7792eSThomas Moestl ct->sec = rsec; 289d7f7792eSThomas Moestl ct->nsec = ts->tv_nsec; 290c9ad6040SPoul-Henning Kamp if (ct_debug) { 291932d14c6SIan Lepore printf("ts_to_ct(%jd.%09ld) = ", 292932d14c6SIan Lepore (intmax_t)ts->tv_sec, ts->tv_nsec); 293c9ad6040SPoul-Henning Kamp print_ct(ct); 294c9ad6040SPoul-Henning Kamp printf("\n"); 295c9ad6040SPoul-Henning Kamp } 29690a79ac5SConrad Meyer 29790a79ac5SConrad Meyer KASSERT(ct->year >= 0 && ct->year < 10000, 29890a79ac5SConrad Meyer ("year %d isn't a 4 digit year", ct->year)); 29990a79ac5SConrad Meyer KASSERT(ct->mon >= 1 && ct->mon <= 12, 30090a79ac5SConrad Meyer ("month %d not in 1-12", ct->mon)); 30190a79ac5SConrad Meyer KASSERT(ct->day >= 1 && ct->day <= 31, 30290a79ac5SConrad Meyer ("day %d not in 1-31", ct->day)); 30390a79ac5SConrad Meyer KASSERT(ct->hour >= 0 && ct->hour <= 23, 30490a79ac5SConrad Meyer ("hour %d not in 0-23", ct->hour)); 30590a79ac5SConrad Meyer KASSERT(ct->min >= 0 && ct->min <= 59, 30690a79ac5SConrad Meyer ("minute %d not in 0-59", ct->min)); 30790a79ac5SConrad Meyer /* Not sure if this interface needs to handle leapseconds or not. */ 30890a79ac5SConrad Meyer KASSERT(ct->sec >= 0 && ct->sec <= 60, 30990a79ac5SConrad Meyer ("seconds %d not in 0-60", ct->sec)); 310d7f7792eSThomas Moestl } 311f97c1c4bSPoul-Henning Kamp 31286299375SIan Lepore void 313*ce75945dSIan Lepore clock_ts_to_bcd(const struct timespec *ts, struct bcd_clocktime *bct, bool ampm) 31486299375SIan Lepore { 31586299375SIan Lepore struct clocktime ct; 31686299375SIan Lepore 31786299375SIan Lepore clock_ts_to_ct(ts, &ct); 31886299375SIan Lepore 31986299375SIan Lepore /* If asked to handle am/pm, convert from 24hr to 12hr+pmflag. */ 32086299375SIan Lepore bct->ispm = false; 32186299375SIan Lepore if (ampm) { 32286299375SIan Lepore if (ct.hour >= 12) { 32386299375SIan Lepore ct.hour -= 12; 32486299375SIan Lepore bct->ispm = true; 32586299375SIan Lepore } 32686299375SIan Lepore if (ct.hour == 0) 32786299375SIan Lepore ct.hour = 12; 32886299375SIan Lepore } 32986299375SIan Lepore 33086299375SIan Lepore bct->year = TOBCD(ct.year % 100) | (TOBCD(ct.year / 100) << 8); 33186299375SIan Lepore bct->mon = TOBCD(ct.mon); 33286299375SIan Lepore bct->day = TOBCD(ct.day); 33386299375SIan Lepore bct->hour = TOBCD(ct.hour); 33486299375SIan Lepore bct->min = TOBCD(ct.min); 33586299375SIan Lepore bct->sec = TOBCD(ct.sec); 33686299375SIan Lepore bct->dow = ct.dow; 33786299375SIan Lepore bct->nsec = ct.nsec; 33886299375SIan Lepore } 33986299375SIan Lepore 340f97c1c4bSPoul-Henning Kamp int 341f97c1c4bSPoul-Henning Kamp utc_offset(void) 342f97c1c4bSPoul-Henning Kamp { 343f97c1c4bSPoul-Henning Kamp 344f97c1c4bSPoul-Henning Kamp return (tz_minuteswest * 60 + (wall_cmos_clock ? adjkerntz : 0)); 345f97c1c4bSPoul-Henning Kamp } 346