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