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