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