Home | History | Annotate | Line # | Download | only in dev
clock_subr.c revision 1.21
      1 /*	$NetBSD: clock_subr.c,v 1.21 2014/09/06 18:04:28 martin 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.21 2014/09/06 18:04:28 martin 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 /* for easier alignment:
     70  * time from the epoch to 2000 (there were 7 leap years): */
     71 #define	DAYSTO2000	(365*30+7)
     72 
     73 /* 4 year intervals include 1 leap year */
     74 #define	DAYS4YEARS	(365*4+1)
     75 
     76 /* 100 year intervals include 24 leap years */
     77 #define	DAYS100YEARS	(365*100+24)
     78 
     79 /* 400 year intervals include 97 leap years */
     80 #define	DAYS400YEARS	(365*400+97)
     81 
     82 static const int month_days[12] = {
     83 	31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
     84 };
     85 
     86 /*
     87  * This inline avoids some unnecessary modulo operations
     88  * as compared with the usual macro:
     89  *   ( ((year % 4) == 0 &&
     90  *      (year % 100) != 0) ||
     91  *     ((year % 400) == 0) )
     92  * It is otherwise equivalent.
     93  */
     94 static inline int
     95 leapyear(int year)
     96 {
     97 	int rv = 0;
     98 
     99 	if ((year & 3) == 0) {
    100 		rv = 1;
    101 		if ((year % 100) == 0) {
    102 			rv = 0;
    103 			if ((year % 400) == 0)
    104 				rv = 1;
    105 		}
    106 	}
    107 	return rv;
    108 }
    109 
    110 time_t
    111 clock_ymdhms_to_secs(struct clock_ymdhms *dt)
    112 {
    113 	uint64_t secs;
    114 	int i, year, days;
    115 
    116 	year = dt->dt_year;
    117 
    118 	/*
    119 	 * Compute days since start of time
    120 	 * First from years, then from months.
    121 	 */
    122 	if (year < POSIX_BASE_YEAR)
    123 		return -1;
    124 	days = 0;
    125 	if (leapyear(year) && dt->dt_mon > FEBRUARY)
    126 		days++;
    127 
    128 	if (year < 2000) {
    129 		/* simple way for early years */
    130 		for (i = POSIX_BASE_YEAR; i < year; i++)
    131 			days += days_in_year(i);
    132 	} else {
    133 		/* years are properly aligned */
    134 		days += DAYSTO2000;
    135 		year -= 2000;
    136 
    137 		i = year / 400;
    138 		days += i * DAYS400YEARS;
    139 		year -= i * 400;
    140 
    141 		i = year / 100;
    142 		days += i * DAYS100YEARS;
    143 		year -= i * 100;
    144 
    145 		i = year / 4;
    146 		days += i * DAYS4YEARS;
    147 		year -= i * 4;
    148 
    149 		for (i = dt->dt_year-year; i < dt->dt_year; i++)
    150 			days += days_in_year(i);
    151 	}
    152 
    153 
    154 	/* Months */
    155 	for (i = 1; i < dt->dt_mon; i++)
    156 	  	days += days_in_month(i);
    157 	days += (dt->dt_day - 1);
    158 
    159 	/* Add hours, minutes, seconds. */
    160 	secs = (((uint64_t)days
    161 	    * 24 + dt->dt_hour)
    162 	    * 60 + dt->dt_min)
    163 	    * 60 + dt->dt_sec;
    164 
    165 	if ((time_t)secs < 0 || secs > __type_max(time_t))
    166 		return -1;
    167 	return secs;
    168 }
    169 
    170 void
    171 clock_secs_to_ymdhms(time_t secs, struct clock_ymdhms *dt)
    172 {
    173 	int i, leap;
    174 	time_t days;
    175 	time_t rsec;	/* remainder seconds */
    176 
    177 	days = secs / SECDAY;
    178 	rsec = secs % SECDAY;
    179 
    180 	/* Day of week (Note: 1/1/1970 was a Thursday) */
    181 	dt->dt_wday = (days + 4) % 7;
    182 
    183 	if (days >= DAYSTO2000) {
    184 		days -= DAYSTO2000;
    185 		dt->dt_year = 2000;
    186 
    187 		i = days / DAYS400YEARS;
    188 		days -= i*DAYS400YEARS;
    189 		dt->dt_year += i*400;
    190 
    191 		i = days / DAYS100YEARS;
    192 		days -= i*DAYS100YEARS;
    193 		dt->dt_year += i*100;
    194 
    195 		i = days / DAYS4YEARS;
    196 		days -= i*DAYS4YEARS;
    197 		dt->dt_year += i*4;
    198 
    199 		for (i = dt->dt_year; days >= days_in_year(i); i++)
    200 			days -= days_in_year(i);
    201 		dt->dt_year = i;
    202 	} else {
    203 		/* Subtract out whole years, counting them in i. */
    204 		for (i = POSIX_BASE_YEAR; days >= days_in_year(i); i++)
    205 			days -= days_in_year(i);
    206 		dt->dt_year = i;
    207 	}
    208 
    209 	/* Subtract out whole months, counting them in i. */
    210 	for (leap = 0, i = 1; days >= days_in_month(i)+leap; i++) {
    211 		days -= days_in_month(i)+leap;
    212 		if (i == 1 && leapyear(dt->dt_year))
    213 			leap = 1;
    214 		else
    215 			leap = 0;
    216 	}
    217 	dt->dt_mon = i;
    218 
    219 	/* Days are what is left over (+1) from all that. */
    220 	dt->dt_day = days + 1;
    221 
    222 	/* Hours, minutes, seconds are easy */
    223 	dt->dt_hour = rsec / 3600;
    224 	rsec = rsec % 3600;
    225 	dt->dt_min  = rsec / 60;
    226 	rsec = rsec % 60;
    227 	dt->dt_sec  = rsec;
    228 }
    229