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