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