clock_subr.c revision 1.7 1 /* $NetBSD: clock_subr.c,v 1.7 2001/11/13 05:32:49 lukem 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. All advertising materials mentioning features or use of this software
21 * must display the following acknowledgement:
22 * This product includes software developed by the University of
23 * California, Berkeley and its contributors.
24 * 4. Neither the name of the University nor the names of its contributors
25 * may be used to endorse or promote products derived from this software
26 * without specific prior written permission.
27 *
28 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 * SUCH DAMAGE.
39 *
40 * from: Utah $Hdr: clock.c 1.18 91/01/21$
41 *
42 * @(#)clock.c 8.2 (Berkeley) 1/12/94
43 */
44
45 /*
46 * Generic routines to convert between a POSIX date
47 * (seconds since 1/1/1970) and yr/mo/day/hr/min/sec
48 * Derived from arch/hp300/hp300/clock.c
49 */
50
51 #include <sys/cdefs.h>
52 __KERNEL_RCSID(0, "$NetBSD: clock_subr.c,v 1.7 2001/11/13 05:32:49 lukem Exp $");
53
54 #include <sys/types.h>
55 #include <sys/systm.h>
56
57 #include <dev/clock_subr.h>
58
59 static inline int leapyear __P((int year));
60 #define FEBRUARY 2
61 #define days_in_year(a) (leapyear(a) ? 366 : 365)
62 #define days_in_month(a) (month_days[(a) - 1])
63
64 static const int month_days[12] = {
65 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
66 };
67
68 /*
69 * This inline avoids some unnecessary modulo operations
70 * as compared with the usual macro:
71 * ( ((year % 4) == 0 &&
72 * (year % 100) != 0) ||
73 * ((year % 400) == 0) )
74 * It is otherwise equivalent.
75 */
76 static inline int
77 leapyear(year)
78 int year;
79 {
80 int rv = 0;
81
82 if ((year & 3) == 0) {
83 rv = 1;
84 if ((year % 100) == 0) {
85 rv = 0;
86 if ((year % 400) == 0)
87 rv = 1;
88 }
89 }
90 return (rv);
91 }
92
93 time_t
94 clock_ymdhms_to_secs(dt)
95 struct clock_ymdhms *dt;
96 {
97 time_t secs;
98 int i, year, days;
99
100 year = dt->dt_year;
101
102 /*
103 * Compute days since start of time
104 * First from years, then from months.
105 */
106 days = 0;
107 for (i = POSIX_BASE_YEAR; i < year; i++)
108 days += days_in_year(i);
109 if (leapyear(year) && dt->dt_mon > FEBRUARY)
110 days++;
111
112 /* Months */
113 for (i = 1; i < dt->dt_mon; i++)
114 days += days_in_month(i);
115 days += (dt->dt_day - 1);
116
117 /* Add hours, minutes, seconds. */
118 secs = ((days
119 * 24 + dt->dt_hour)
120 * 60 + dt->dt_min)
121 * 60 + dt->dt_sec;
122
123 return (secs);
124 }
125
126 void
127 clock_secs_to_ymdhms(secs, dt)
128 time_t secs;
129 struct clock_ymdhms *dt;
130 {
131 int mthdays[12];
132 int i, days;
133 int rsec; /* remainder seconds */
134
135 /*
136 * This function uses a local copy of month_days[]
137 * so the copy can be modified (and thread-safe).
138 * See the definition of days_in_month() above.
139 */
140 memcpy(mthdays, month_days, sizeof(mthdays));
141 #define month_days mthdays
142
143 days = secs / SECDAY;
144 rsec = secs % SECDAY;
145
146 /* Day of week (Note: 1/1/1970 was a Thursday) */
147 dt->dt_wday = (days + 4) % 7;
148
149 /* Subtract out whole years, counting them in i. */
150 for (i = POSIX_BASE_YEAR; days >= days_in_year(i); i++)
151 days -= days_in_year(i);
152 dt->dt_year = i;
153
154 /* Subtract out whole months, counting them in i. */
155 if (leapyear(i))
156 days_in_month(FEBRUARY) = 29;
157 for (i = 1; days >= days_in_month(i); i++)
158 days -= days_in_month(i);
159 dt->dt_mon = i;
160
161 /* Days are what is left over (+1) from all that. */
162 dt->dt_day = days + 1;
163
164 /* Hours, minutes, seconds are easy */
165 dt->dt_hour = rsec / 3600;
166 rsec = rsec % 3600;
167 dt->dt_min = rsec / 60;
168 rsec = rsec % 60;
169 dt->dt_sec = rsec;
170 #undef month_days
171 }
172