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