Home | History | Annotate | Line # | Download | only in dev
clock_subr.c revision 1.24
      1 /*	$NetBSD: clock_subr.c,v 1.24 2014/11/17 02:23:33 christos 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.24 2014/11/17 02:23:33 christos Exp $");
     54 
     55 #include <sys/param.h>
     56 #include <sys/systm.h>
     57 #include <sys/errno.h>
     58 #else /* ! _KERNEL */
     59 #include <string.h>
     60 #include <time.h>
     61 #include <errno.h>
     62 #endif /* ! _KERNEL */
     63 
     64 #include <dev/clock_subr.h>
     65 
     66 #define FEBRUARY	2
     67 
     68 /* for easier alignment:
     69  * time from the epoch to 2000 (there were 7 leap years): */
     70 #define	DAYSTO2000	(365*30+7)
     71 
     72 /* 4 year intervals include 1 leap year */
     73 #define	DAYS4YEARS	(365*4+1)
     74 
     75 /* 100 year intervals include 24 leap years */
     76 #define	DAYS100YEARS	(365*100+24)
     77 
     78 /* 400 year intervals include 97 leap years */
     79 #define	DAYS400YEARS	(365*400+97)
     80 
     81 time_t
     82 clock_ymdhms_to_secs(struct clock_ymdhms *dt)
     83 {
     84 	uint64_t secs, i, year, days;
     85 
     86 	year = dt->dt_year;
     87 
     88 	/*
     89 	 * Compute days since start of time
     90 	 * First from years, then from months.
     91 	 */
     92 	if (year < POSIX_BASE_YEAR)
     93 		return -1;
     94 	days = 0;
     95 	if (is_leap_year(year) && dt->dt_mon > FEBRUARY)
     96 		days++;
     97 
     98 	if (year < 2000) {
     99 		/* simple way for early years */
    100 		for (i = POSIX_BASE_YEAR; i < year; i++)
    101 			days += days_per_year(i);
    102 	} else {
    103 		/* years are properly aligned */
    104 		days += DAYSTO2000;
    105 		year -= 2000;
    106 
    107 		i = year / 400;
    108 		days += i * DAYS400YEARS;
    109 		year -= i * 400;
    110 
    111 		i = year / 100;
    112 		days += i * DAYS100YEARS;
    113 		year -= i * 100;
    114 
    115 		i = year / 4;
    116 		days += i * DAYS4YEARS;
    117 		year -= i * 4;
    118 
    119 		for (i = dt->dt_year-year; i < dt->dt_year; i++)
    120 			days += days_per_year(i);
    121 	}
    122 
    123 
    124 	/* Months */
    125 	for (i = 1; i < dt->dt_mon; i++)
    126 	  	days += days_in_month(i);
    127 	days += (dt->dt_day - 1);
    128 
    129 	/* Add hours, minutes, seconds. */
    130 	secs = (((uint64_t)days
    131 	    * 24 + dt->dt_hour)
    132 	    * 60 + dt->dt_min)
    133 	    * 60 + dt->dt_sec;
    134 
    135 	if ((time_t)secs < 0 || secs > __type_max(time_t))
    136 		return -1;
    137 	return secs;
    138 }
    139 
    140 int
    141 clock_secs_to_ymdhms(time_t secs, struct clock_ymdhms *dt)
    142 {
    143 	int leap;
    144 	uint64_t i;
    145 	time_t days;
    146 	time_t rsec;	/* remainder seconds */
    147 
    148 	if (secs < 0)
    149 		return EINVAL;
    150 
    151 	days = secs / SECS_PER_DAY;
    152 	rsec = secs % SECS_PER_DAY;
    153 
    154 	/* Day of week (Note: 1/1/1970 was a Thursday) */
    155 	dt->dt_wday = (days + 4) % 7;
    156 
    157 	if (days >= DAYSTO2000) {
    158 		days -= DAYSTO2000;
    159 		dt->dt_year = 2000;
    160 
    161 		i = days / DAYS400YEARS;
    162 		days -= i*DAYS400YEARS;
    163 		dt->dt_year += i*400;
    164 
    165 		i = days / DAYS100YEARS;
    166 		days -= i*DAYS100YEARS;
    167 		dt->dt_year += i*100;
    168 
    169 		i = days / DAYS4YEARS;
    170 		days -= i*DAYS4YEARS;
    171 		dt->dt_year += i*4;
    172 
    173 		for (i = dt->dt_year; days >= days_per_year(i); i++)
    174 			days -= days_per_year(i);
    175 		dt->dt_year = i;
    176 	} else {
    177 		/* Subtract out whole years, counting them in i. */
    178 		for (i = POSIX_BASE_YEAR; days >= days_per_year(i); i++)
    179 			days -= days_per_year(i);
    180 		dt->dt_year = i;
    181 	}
    182 
    183 	/* Subtract out whole months, counting them in i. */
    184 	for (leap = 0, i = 1; days >= days_in_month(i)+leap; i++) {
    185 		days -= days_in_month(i)+leap;
    186 		if (i == 1 && is_leap_year(dt->dt_year))
    187 			leap = 1;
    188 		else
    189 			leap = 0;
    190 	}
    191 	dt->dt_mon = i;
    192 
    193 	/* Days are what is left over (+1) from all that. */
    194 	dt->dt_day = days + 1;
    195 
    196 	/* Hours, minutes, seconds are easy */
    197 	dt->dt_hour = rsec / 3600;
    198 	rsec = rsec % 3600;
    199 	dt->dt_min  = rsec / 60;
    200 	rsec = rsec % 60;
    201 	dt->dt_sec  = rsec;
    202 
    203 	return 0;
    204 }
    205