/src/httpd/srclib/apr/time/unix/time.c
Line | Count | Source |
1 | | /* Licensed to the Apache Software Foundation (ASF) under one or more |
2 | | * contributor license agreements. See the NOTICE file distributed with |
3 | | * this work for additional information regarding copyright ownership. |
4 | | * The ASF licenses this file to You under the Apache License, Version 2.0 |
5 | | * (the "License"); you may not use this file except in compliance with |
6 | | * the License. You may obtain a copy of the License at |
7 | | * |
8 | | * http://www.apache.org/licenses/LICENSE-2.0 |
9 | | * |
10 | | * Unless required by applicable law or agreed to in writing, software |
11 | | * distributed under the License is distributed on an "AS IS" BASIS, |
12 | | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13 | | * See the License for the specific language governing permissions and |
14 | | * limitations under the License. |
15 | | */ |
16 | | |
17 | | #include "apr_portable.h" |
18 | | #include "apr_time.h" |
19 | | #include "apr_lib.h" |
20 | | #include "apr_private.h" |
21 | | #include "apr_strings.h" |
22 | | |
23 | | /* private APR headers */ |
24 | | #include "apr_arch_internal_time.h" |
25 | | |
26 | | /* System Headers required for time library */ |
27 | | #if APR_HAVE_SYS_TIME_H |
28 | | #include <sys/time.h> |
29 | | #endif |
30 | | #if APR_HAVE_UNISTD_H |
31 | | #include <unistd.h> |
32 | | #endif |
33 | | #ifdef HAVE_TIME_H |
34 | | #include <time.h> |
35 | | #endif |
36 | | /* End System Headers */ |
37 | | |
38 | | #if !defined(HAVE_STRUCT_TM_TM_GMTOFF) && !defined(HAVE_STRUCT_TM___TM_GMTOFF) |
39 | | static apr_int32_t server_gmt_offset; |
40 | | #define NO_GMTOFF_IN_STRUCT_TM |
41 | | #endif |
42 | | |
43 | | static apr_int32_t get_offset(struct tm *tm) |
44 | 0 | { |
45 | 0 | #if defined(HAVE_STRUCT_TM_TM_GMTOFF) |
46 | 0 | return tm->tm_gmtoff; |
47 | | #elif defined(HAVE_STRUCT_TM___TM_GMTOFF) |
48 | | return tm->__tm_gmtoff; |
49 | | #else |
50 | | if (tm->tm_isdst) |
51 | | return server_gmt_offset + 3600; |
52 | | return server_gmt_offset; |
53 | | #endif |
54 | 0 | } |
55 | | |
56 | | APR_DECLARE(apr_status_t) apr_time_ansi_put(apr_time_t *result, |
57 | | time_t input) |
58 | 3.65k | { |
59 | 3.65k | *result = (apr_time_t)input * APR_USEC_PER_SEC; |
60 | 3.65k | return APR_SUCCESS; |
61 | 3.65k | } |
62 | | |
63 | | /* NB NB NB NB This returns GMT!!!!!!!!!! */ |
64 | | APR_DECLARE(apr_time_t) apr_time_now(void) |
65 | 679 | { |
66 | 679 | struct timeval tv; |
67 | 679 | gettimeofday(&tv, NULL); |
68 | 679 | return tv.tv_sec * (apr_time_t)APR_USEC_PER_SEC + (apr_time_t)tv.tv_usec; |
69 | 679 | } |
70 | | |
71 | | #if defined(OS2) |
72 | | /* The timer's frequency (ticks per second), 0 if not available |
73 | | * (apr_time_now() then) |
74 | | */ |
75 | | static ULONG timer_freq; |
76 | | #endif |
77 | | |
78 | | void apr__time_init(void) |
79 | 0 | { |
80 | | #if defined(OS2) |
81 | | if (DosTmrQueryFreq(&timer_freq)) { |
82 | | timer_freq = 0; |
83 | | } |
84 | | #endif |
85 | 0 | } |
86 | | |
87 | | APR_DECLARE(apr_interval_time_t) apr_time_monotonic(void) |
88 | 0 | { |
89 | | #if defined(OS2) |
90 | | QWORD ticks = {0, 0}; |
91 | | apr_uint64_t t; |
92 | | |
93 | | if (!timer_freq) { |
94 | | return (apr_interval_time_t)apr_time_now(); /* no timer */ |
95 | | } |
96 | | (void)DosTmrQueryTime(&ticks); |
97 | | t = ((apr_uint64_t)ticks.ulHi << 32) | ticks.ulLo; |
98 | | return apr_time_make(t / timer_freq, |
99 | | t % timer_freq * APR_USEC_PER_SEC / timer_freq); |
100 | | #elif defined(HAVE_CLOCK_BOOTTIME) || defined(HAVE_CLOCK_MONOTONIC) |
101 | | struct timespec ts = {0, 0}; |
102 | | |
103 | | /* Checked by configure: it can't fail. CLOCK_BOOTTIME counts the time |
104 | | * suspended (a network peer doesn't pause meanwhile), CLOCK_MONOTONIC |
105 | | * does or not depending on the system. |
106 | | */ |
107 | 0 | #if defined(HAVE_CLOCK_BOOTTIME) |
108 | 0 | (void)clock_gettime(CLOCK_BOOTTIME, &ts); |
109 | | #else |
110 | | (void)clock_gettime(CLOCK_MONOTONIC, &ts); |
111 | | #endif |
112 | 0 | return apr_time_make(ts.tv_sec, ts.tv_nsec / 1000); |
113 | | #else |
114 | | return (apr_interval_time_t)apr_time_now(); |
115 | | #endif |
116 | 0 | } |
117 | | |
118 | | static void explode_time(apr_time_exp_t *xt, apr_time_t t, |
119 | | apr_int32_t offset, int use_localtime) |
120 | 0 | { |
121 | 0 | struct tm tm; |
122 | 0 | time_t tt = (t / APR_USEC_PER_SEC) + offset; |
123 | 0 | xt->tm_usec = t % APR_USEC_PER_SEC; |
124 | |
|
125 | 0 | #if APR_HAS_THREADS && defined (_POSIX_THREAD_SAFE_FUNCTIONS) |
126 | 0 | if (use_localtime) |
127 | 0 | localtime_r(&tt, &tm); |
128 | 0 | else |
129 | 0 | gmtime_r(&tt, &tm); |
130 | | #else |
131 | | if (use_localtime) |
132 | | tm = *localtime(&tt); |
133 | | else |
134 | | tm = *gmtime(&tt); |
135 | | #endif |
136 | |
|
137 | 0 | xt->tm_sec = tm.tm_sec; |
138 | 0 | xt->tm_min = tm.tm_min; |
139 | 0 | xt->tm_hour = tm.tm_hour; |
140 | 0 | xt->tm_mday = tm.tm_mday; |
141 | 0 | xt->tm_mon = tm.tm_mon; |
142 | 0 | xt->tm_year = tm.tm_year; |
143 | 0 | xt->tm_wday = tm.tm_wday; |
144 | 0 | xt->tm_yday = tm.tm_yday; |
145 | 0 | xt->tm_isdst = tm.tm_isdst; |
146 | 0 | xt->tm_gmtoff = get_offset(&tm); |
147 | 0 | } |
148 | | |
149 | | APR_DECLARE(apr_status_t) apr_time_exp_tz(apr_time_exp_t *result, |
150 | | apr_time_t input, apr_int32_t offs) |
151 | 0 | { |
152 | 0 | explode_time(result, input, offs, 0); |
153 | 0 | result->tm_gmtoff = offs; |
154 | 0 | return APR_SUCCESS; |
155 | 0 | } |
156 | | |
157 | | APR_DECLARE(apr_status_t) apr_time_exp_gmt(apr_time_exp_t *result, |
158 | | apr_time_t input) |
159 | 0 | { |
160 | 0 | return apr_time_exp_tz(result, input, 0); |
161 | 0 | } |
162 | | |
163 | | APR_DECLARE(apr_status_t) apr_time_exp_lt(apr_time_exp_t *result, |
164 | | apr_time_t input) |
165 | 0 | { |
166 | | #if defined(__EMX__) |
167 | | /* EMX gcc (OS/2) has a timezone global we can use */ |
168 | | return apr_time_exp_tz(result, input, -timezone); |
169 | | #else |
170 | 0 | explode_time(result, input, 0, 1); |
171 | 0 | return APR_SUCCESS; |
172 | 0 | #endif /* __EMX__ */ |
173 | 0 | } |
174 | | |
175 | | APR_DECLARE(apr_status_t) apr_time_exp_get(apr_time_t *t, apr_time_exp_t *xt) |
176 | 0 | { |
177 | 0 | apr_time_t year = xt->tm_year; |
178 | 0 | apr_time_t days; |
179 | 0 | static const int dayoffset[12] = |
180 | 0 | {306, 337, 0, 31, 61, 92, 122, 153, 184, 214, 245, 275}; |
181 | |
|
182 | 0 | if (xt->tm_mon < 0 || xt->tm_mon >= 12) |
183 | 0 | return APR_EBADDATE; |
184 | | |
185 | | /* shift new year to 1st March in order to make leap year calc easy */ |
186 | | |
187 | 0 | if (xt->tm_mon < 2) |
188 | 0 | year--; |
189 | | |
190 | | /* Find number of days since 1st March 1900 (in the Gregorian calendar). */ |
191 | |
|
192 | 0 | days = year * 365 + year / 4 - year / 100 + (year / 100 + 3) / 4; |
193 | 0 | days += dayoffset[xt->tm_mon] + xt->tm_mday - 1; |
194 | 0 | days -= 25508; /* 1 jan 1970 is 25508 days since 1 mar 1900 */ |
195 | 0 | days = ((days * 24 + xt->tm_hour) * 60 + xt->tm_min) * 60 + xt->tm_sec; |
196 | |
|
197 | 0 | if (days < 0) { |
198 | 0 | return APR_EBADDATE; |
199 | 0 | } |
200 | 0 | *t = days * APR_USEC_PER_SEC + xt->tm_usec; |
201 | 0 | return APR_SUCCESS; |
202 | 0 | } |
203 | | |
204 | | APR_DECLARE(apr_status_t) apr_time_exp_gmt_get(apr_time_t *t, |
205 | | apr_time_exp_t *xt) |
206 | 0 | { |
207 | 0 | apr_status_t status = apr_time_exp_get(t, xt); |
208 | 0 | if (status == APR_SUCCESS) |
209 | 0 | *t -= (apr_time_t) xt->tm_gmtoff * APR_USEC_PER_SEC; |
210 | 0 | return status; |
211 | 0 | } |
212 | | |
213 | | APR_DECLARE(apr_status_t) apr_os_imp_time_get(apr_os_imp_time_t **ostime, |
214 | | apr_time_t *aprtime) |
215 | 0 | { |
216 | 0 | (*ostime)->tv_usec = *aprtime % APR_USEC_PER_SEC; |
217 | 0 | (*ostime)->tv_sec = *aprtime / APR_USEC_PER_SEC; |
218 | 0 | return APR_SUCCESS; |
219 | 0 | } |
220 | | |
221 | | APR_DECLARE(apr_status_t) apr_os_exp_time_get(apr_os_exp_time_t **ostime, |
222 | | apr_time_exp_t *aprtime) |
223 | 0 | { |
224 | 0 | (*ostime)->tm_sec = aprtime->tm_sec; |
225 | 0 | (*ostime)->tm_min = aprtime->tm_min; |
226 | 0 | (*ostime)->tm_hour = aprtime->tm_hour; |
227 | 0 | (*ostime)->tm_mday = aprtime->tm_mday; |
228 | 0 | (*ostime)->tm_mon = aprtime->tm_mon; |
229 | 0 | (*ostime)->tm_year = aprtime->tm_year; |
230 | 0 | (*ostime)->tm_wday = aprtime->tm_wday; |
231 | 0 | (*ostime)->tm_yday = aprtime->tm_yday; |
232 | 0 | (*ostime)->tm_isdst = aprtime->tm_isdst; |
233 | |
|
234 | 0 | #if defined(HAVE_STRUCT_TM_TM_GMTOFF) |
235 | 0 | (*ostime)->tm_gmtoff = aprtime->tm_gmtoff; |
236 | | #elif defined(HAVE_STRUCT_TM___TM_GMTOFF) |
237 | | (*ostime)->__tm_gmtoff = aprtime->tm_gmtoff; |
238 | | #endif |
239 | |
|
240 | 0 | return APR_SUCCESS; |
241 | 0 | } |
242 | | |
243 | | APR_DECLARE(apr_status_t) apr_os_imp_time_put(apr_time_t *aprtime, |
244 | | apr_os_imp_time_t **ostime, |
245 | | apr_pool_t *cont) |
246 | 0 | { |
247 | 0 | *aprtime = (*ostime)->tv_sec * APR_USEC_PER_SEC + (*ostime)->tv_usec; |
248 | 0 | return APR_SUCCESS; |
249 | 0 | } |
250 | | |
251 | | APR_DECLARE(apr_status_t) apr_os_exp_time_put(apr_time_exp_t *aprtime, |
252 | | apr_os_exp_time_t **ostime, |
253 | | apr_pool_t *cont) |
254 | 0 | { |
255 | 0 | aprtime->tm_sec = (*ostime)->tm_sec; |
256 | 0 | aprtime->tm_min = (*ostime)->tm_min; |
257 | 0 | aprtime->tm_hour = (*ostime)->tm_hour; |
258 | 0 | aprtime->tm_mday = (*ostime)->tm_mday; |
259 | 0 | aprtime->tm_mon = (*ostime)->tm_mon; |
260 | 0 | aprtime->tm_year = (*ostime)->tm_year; |
261 | 0 | aprtime->tm_wday = (*ostime)->tm_wday; |
262 | 0 | aprtime->tm_yday = (*ostime)->tm_yday; |
263 | 0 | aprtime->tm_isdst = (*ostime)->tm_isdst; |
264 | |
|
265 | 0 | #if defined(HAVE_STRUCT_TM_TM_GMTOFF) |
266 | 0 | aprtime->tm_gmtoff = (*ostime)->tm_gmtoff; |
267 | | #elif defined(HAVE_STRUCT_TM___TM_GMTOFF) |
268 | | aprtime->tm_gmtoff = (*ostime)->__tm_gmtoff; |
269 | | #endif |
270 | |
|
271 | 0 | return APR_SUCCESS; |
272 | 0 | } |
273 | | |
274 | | APR_DECLARE(void) apr_sleep(apr_interval_time_t t) |
275 | 0 | { |
276 | | #ifdef OS2 |
277 | | DosSleep((t + 999) / 1000); |
278 | | #elif defined(HAVE_NANOSLEEP) |
279 | | struct timespec ts; |
280 | 0 | ts.tv_sec = t / APR_USEC_PER_SEC; |
281 | 0 | ts.tv_nsec = (t % APR_USEC_PER_SEC) * 1000; |
282 | 0 | nanosleep(&ts, NULL); |
283 | | #else |
284 | | struct timeval tv; |
285 | | tv.tv_usec = t % APR_USEC_PER_SEC; |
286 | | tv.tv_sec = t / APR_USEC_PER_SEC; |
287 | | select(0, NULL, NULL, NULL, &tv); |
288 | | #endif |
289 | 0 | } |
290 | | |
291 | | #ifdef OS2 |
292 | | APR_DECLARE(apr_status_t) apr_os2_time_to_apr_time(apr_time_t *result, |
293 | | FDATE os2date, |
294 | | FTIME os2time) |
295 | | { |
296 | | struct tm tmpdate; |
297 | | |
298 | | memset(&tmpdate, 0, sizeof(tmpdate)); |
299 | | tmpdate.tm_hour = os2time.hours; |
300 | | tmpdate.tm_min = os2time.minutes; |
301 | | tmpdate.tm_sec = os2time.twosecs * 2; |
302 | | |
303 | | tmpdate.tm_mday = os2date.day; |
304 | | tmpdate.tm_mon = os2date.month - 1; |
305 | | tmpdate.tm_year = os2date.year + 80; |
306 | | tmpdate.tm_isdst = -1; |
307 | | |
308 | | *result = mktime(&tmpdate) * APR_USEC_PER_SEC; |
309 | | return APR_SUCCESS; |
310 | | } |
311 | | |
312 | | APR_DECLARE(apr_status_t) apr_apr_time_to_os2_time(FDATE *os2date, |
313 | | FTIME *os2time, |
314 | | apr_time_t aprtime) |
315 | | { |
316 | | time_t ansitime = aprtime / APR_USEC_PER_SEC; |
317 | | struct tm *lt; |
318 | | lt = localtime(&ansitime); |
319 | | os2time->hours = lt->tm_hour; |
320 | | os2time->minutes = lt->tm_min; |
321 | | os2time->twosecs = lt->tm_sec / 2; |
322 | | |
323 | | os2date->day = lt->tm_mday; |
324 | | os2date->month = lt->tm_mon + 1; |
325 | | os2date->year = lt->tm_year - 80; |
326 | | return APR_SUCCESS; |
327 | | } |
328 | | #endif |
329 | | |
330 | | APR_DECLARE(void) apr_unix_setup_time(void) |
331 | 0 | { |
332 | | #ifdef NO_GMTOFF_IN_STRUCT_TM |
333 | | /* Precompute the offset from GMT on systems where it's not |
334 | | in struct tm. |
335 | | |
336 | | Note: This offset is normalized to be independent of daylight |
337 | | savings time; if the calculation happens to be done in a |
338 | | time/place where a daylight savings adjustment is in effect, |
339 | | the returned offset has the same value that it would have |
340 | | in the same location if daylight savings were not in effect. |
341 | | The reason for this is that the returned offset can be |
342 | | applied to a past or future timestamp in explode_time(), |
343 | | so the DST adjustment obtained from the current time won't |
344 | | necessarily be applicable. |
345 | | |
346 | | mktime() is the inverse of localtime(); so, presumably, |
347 | | passing in a struct tm made by gmtime() let's us calculate |
348 | | the true GMT offset. However, there's a catch: if daylight |
349 | | savings is in effect, gmtime()will set the tm_isdst field |
350 | | and confuse mktime() into returning a time that's offset |
351 | | by one hour. In that case, we must adjust the calculated GMT |
352 | | offset. |
353 | | |
354 | | */ |
355 | | |
356 | | struct timeval now; |
357 | | time_t t1, t2; |
358 | | struct tm t; |
359 | | |
360 | | gettimeofday(&now, NULL); |
361 | | t1 = now.tv_sec; |
362 | | t2 = 0; |
363 | | |
364 | | #if APR_HAS_THREADS && defined(_POSIX_THREAD_SAFE_FUNCTIONS) |
365 | | gmtime_r(&t1, &t); |
366 | | #else |
367 | | t = *gmtime(&t1); |
368 | | #endif |
369 | | t.tm_isdst = 0; /* we know this GMT time isn't daylight-savings */ |
370 | | t2 = mktime(&t); |
371 | | server_gmt_offset = (apr_int32_t) difftime(t1, t2); |
372 | | #endif /* NO_GMTOFF_IN_STRUCT_TM */ |
373 | 0 | } |
374 | | |
375 | | |
376 | | /* A noop on all known Unix implementations */ |
377 | | APR_DECLARE(void) apr_time_clock_hires(apr_pool_t *p) |
378 | 0 | { |
379 | 0 | return; |
380 | 0 | } |
381 | | |
382 | | |