1 /* $NetBSD: clock_subr.c,v 1.20 2014/09/01 12:25:52 apb Exp $ */ 2 3 /* 4 * Copyright (c) 1988 University of Utah. 5 * Copyright (c) 1982, 1990, 1993 6 * The Regents of the University of California. All rights reserved. 7 * 8 * This code is derived from software contributed to Berkeley by 9 * the Systems Programming Group of the University of Utah Computer 10 * Science Department. 11 * 12 * Redistribution and use in source and binary forms, with or without 13 * modification, are permitted provided that the following conditions 14 * are met: 15 * 1. Redistributions of source code must retain the above copyright 16 * notice, this list of conditions and the following disclaimer. 17 * 2. Redistributions in binary form must reproduce the above copyright 18 * notice, this list of conditions and the following disclaimer in the 19 * documentation and/or other materials provided with the distribution. 20 * 3. Neither the name of the University nor the names of its contributors 21 * may be used to endorse or promote products derived from this software 22 * without specific prior written permission. 23 * 24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34 * SUCH DAMAGE. 35 * 36 * from: Utah $Hdr: clock.c 1.18 91/01/21$ 37 * 38 * @(#)clock.c 8.2 (Berkeley) 1/12/94 39 */ 40 41 /* 42 * Generic routines to convert between a POSIX date 43 * (seconds since 1/1/1970) and yr/mo/day/hr/min/sec 44 * Derived from arch/hp300/hp300/clock.c 45 */ 46 47 #if HAVE_NBTOOL_CONFIG_H 48 #include "nbtool_config.h" 49 #endif /* HAVE_NBTOOL_CONFIG_H */ 50 51 #ifdef _KERNEL 52 #include <sys/cdefs.h> 53 __KERNEL_RCSID(0, "$NetBSD: clock_subr.c,v 1.20 2014/09/01 12:25:52 apb Exp $"); 54 55 #include <sys/param.h> 56 #include <sys/systm.h> 57 #else /* ! _KERNEL */ 58 #include <string.h> 59 #include <time.h> 60 #endif /* ! _KERNEL */ 61 62 #include <dev/clock_subr.h> 63 64 static inline int leapyear(int year); 65 #define FEBRUARY 2 66 #define days_in_year(a) (leapyear(a) ? 366 : 365) 67 #define days_in_month(a) (month_days[(a) - 1]) 68 69 static const int month_days[12] = { 70 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 71 }; 72 73 /* 74 * This inline avoids some unnecessary modulo operations 75 * as compared with the usual macro: 76 * ( ((year % 4) == 0 && 77 * (year % 100) != 0) || 78 * ((year % 400) == 0) ) 79 * It is otherwise equivalent. 80 */ 81 static inline int 82 leapyear(int year) 83 { 84 int rv = 0; 85 86 if ((year & 3) == 0) { 87 rv = 1; 88 if ((year % 100) == 0) { 89 rv = 0; 90 if ((year % 400) == 0) 91 rv = 1; 92 } 93 } 94 return rv; 95 } 96 97 time_t 98 clock_ymdhms_to_secs(struct clock_ymdhms *dt) 99 { 100 uint64_t secs; 101 int i, year, days; 102 103 year = dt->dt_year; 104 105 /* 106 * Compute days since start of time 107 * First from years, then from months. 108 */ 109 if (year < POSIX_BASE_YEAR) 110 return -1; 111 days = 0; 112 for (i = POSIX_BASE_YEAR; i < year; i++) 113 days += days_in_year(i); 114 if (leapyear(year) && dt->dt_mon > FEBRUARY) 115 days++; 116 117 /* Months */ 118 for (i = 1; i < dt->dt_mon; i++) 119 days += days_in_month(i); 120 days += (dt->dt_day - 1); 121 122 /* Add hours, minutes, seconds. */ 123 secs = (((uint64_t)days 124 * 24 + dt->dt_hour) 125 * 60 + dt->dt_min) 126 * 60 + dt->dt_sec; 127 128 if ((time_t)secs < 0 || secs > __type_max(time_t)) 129 return -1; 130 return secs; 131 } 132 133 void 134 clock_secs_to_ymdhms(time_t secs, struct clock_ymdhms *dt) 135 { 136 int mthdays[12]; 137 int i; 138 time_t days; 139 time_t rsec; /* remainder seconds */ 140 141 /* 142 * This function uses a local copy of month_days[] 143 * so the copy can be modified (and thread-safe). 144 * See the definition of days_in_month() above. 145 */ 146 memcpy(mthdays, month_days, sizeof(mthdays)); 147 #define month_days mthdays 148 149 days = secs / SECDAY; 150 rsec = secs % SECDAY; 151 152 /* Day of week (Note: 1/1/1970 was a Thursday) */ 153 dt->dt_wday = (days + 4) % 7; 154 155 /* Subtract out whole years, counting them in i. */ 156 for (i = POSIX_BASE_YEAR; days >= days_in_year(i); i++) 157 days -= days_in_year(i); 158 dt->dt_year = i; 159 160 /* Subtract out whole months, counting them in i. */ 161 if (leapyear(i)) 162 days_in_month(FEBRUARY) = 29; 163 for (i = 1; days >= days_in_month(i); i++) 164 days -= days_in_month(i); 165 dt->dt_mon = i; 166 167 /* Days are what is left over (+1) from all that. */ 168 dt->dt_day = days + 1; 169 170 /* Hours, minutes, seconds are easy */ 171 dt->dt_hour = rsec / 3600; 172 rsec = rsec % 3600; 173 dt->dt_min = rsec / 60; 174 rsec = rsec % 60; 175 dt->dt_sec = rsec; 176 #undef month_days 177 } 178