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