Apache HTTPD
util_time.c
Go to the documentation of this file.
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 "util_time.h"
18
19
20/* Number of characters needed to format the microsecond part of a timestamp.
21 * Microseconds have 6 digits plus one separator character makes 7.
22 * */
23#define AP_CTIME_USEC_LENGTH 7
24
25/* Length of ISO 8601 date/time (including trailing '\0') */
26#define AP_CTIME_COMPACT_LEN 20
27
28/* Length of timezone offset from GMT ([+-]hhmm) plus leading space */
29#define AP_CTIME_GMTOFF_LEN 6
30
31/* Cache for exploded values of recent timestamps
32 */
33
37 apr_int64_t t_validate; /* please see comments in cached_explode() */
38};
39
40/* the "+ 1" is for the current second: */
41#define TIME_CACHE_SIZE (AP_TIME_RECENT_THRESHOLD + 1)
42
43/* Note that AP_TIME_RECENT_THRESHOLD is defined to
44 * be a power of two minus one in util_time.h, so that
45 * we can replace a modulo operation with a bitwise AND
46 * when hashing items into a cache of size
47 * AP_TIME_RECENT_THRESHOLD+1
48 */
49#define TIME_CACHE_MASK (AP_TIME_RECENT_THRESHOLD)
50
53
54
57 int use_gmt)
58{
63
64 /* The cache is implemented as a ring buffer. Each second,
65 * it uses a different element in the buffer. The timestamp
66 * in the element indicates whether the element contains the
67 * exploded time for the current second (vs the time
68 * 'now - AP_TIME_RECENT_THRESHOLD' seconds ago). If the
69 * cached value is for the current time, we use it. Otherwise,
70 * we compute the apr_time_exp_t and store it in this
71 * cache element. Note that the timestamp in the cache
72 * element is updated only after the exploded time. Thus
73 * if two threads hit this cache element simultaneously
74 * at the start of a new second, they'll both explode the
75 * time and store it. I.e., the writers will collide, but
76 * they'll be writing the same value.
77 */
78 if (cache_element->t >= seconds) {
79 /* There is an intentional race condition in this design:
80 * in a multithreaded app, one thread might be reading
81 * from this cache_element to resolve a timestamp from
82 * TIME_CACHE_SIZE seconds ago at the same time that
83 * another thread is copying the exploded form of the
84 * current time into the same cache_element. (I.e., the
85 * first thread might hit this element of the ring buffer
86 * just as the element is being recycled.) This can
87 * also happen at the start of a new second, if a
88 * reader accesses the cache_element after a writer
89 * has updated cache_element.t but before the writer
90 * has finished updating the whole cache_element.
91 *
92 * Rather than trying to prevent this race condition
93 * with locks, we allow it to happen and then detect
94 * and correct it. The detection works like this:
95 * Step 1: Take a "snapshot" of the cache element by
96 * copying it into a temporary buffer.
97 * Step 2: Check whether the snapshot contains consistent
98 * data: the timestamps at the start and end of
99 * the cache_element should both match the 'seconds'
100 * value that we computed from the input time.
101 * If these three don't match, then the snapshot
102 * shows the cache_element in the middle of an
103 * update, and its contents are invalid.
104 * Step 3: If the snapshot is valid, use it. Otherwise,
105 * just give up on the cache and explode the
106 * input time.
107 */
109 sizeof(struct exploded_time_cache_element));
110 if ((seconds != cache_element_snapshot.t) ||
111 (seconds != cache_element_snapshot.t_validate)) {
112 /* Invalid snapshot */
113 if (use_gmt) {
114 return apr_time_exp_gmt(xt, t);
115 }
116 else {
117 return apr_time_exp_lt(xt, t);
118 }
119 }
120 else {
121 /* Valid snapshot */
123 sizeof(apr_time_exp_t));
124 }
125 }
126 else {
128 if (use_gmt) {
130 }
131 else {
132 r = apr_time_exp_lt(xt, t);
133 }
134 if (r != APR_SUCCESS) {
135 return r;
136 }
138 memcpy(&(cache_element->xt), xt, sizeof(apr_time_exp_t));
139 cache_element->t_validate = seconds;
140 }
142 return APR_SUCCESS;
143}
144
145
151
157
163
165 int option, int *len)
166{
167 /* ### This code is a clone of apr_ctime(), except that it
168 * uses ap_explode_recent_localtime() instead of apr_time_exp_lt().
169 */
171 const char *s;
172 int real_year;
173 int needed;
174
175
176 /* Calculate the needed buffer length */
179 else
181
184 }
185
188 }
189
190 /* Check the provided buffer length (note: above AP_CTIME_COMPACT_LEN
191 * and APR_CTIME_LEN include the trailing '\0'; so does 'needed' then).
192 */
193 if (len && *len >= needed) {
194 *len = needed;
195 }
196 else {
197 if (len != NULL) {
198 *len = 0;
199 }
200 return APR_ENOMEM;
201 }
202
203 /* example without options: "Wed Jun 30 21:49:08 1993" */
204 /* example for compact format: "1993-06-30 21:49:08" */
205 /* example for compact+usec+gmtoff format:
206 * "1993-06-30 22:49:08.123456 +0100"
207 */
208
210 real_year = 1900 + xt.tm_year;
212 int real_month = xt.tm_mon + 1;
213 *date_str++ = real_year / 1000 + '0';
214 *date_str++ = real_year % 1000 / 100 + '0';
215 *date_str++ = real_year % 100 / 10 + '0';
216 *date_str++ = real_year % 10 + '0';
217 *date_str++ = '-';
218 *date_str++ = real_month / 10 + '0';
219 *date_str++ = real_month % 10 + '0';
220 *date_str++ = '-';
221 }
222 else {
223 s = &apr_day_snames[xt.tm_wday][0];
224 *date_str++ = *s++;
225 *date_str++ = *s++;
226 *date_str++ = *s++;
227 *date_str++ = ' ';
229 *date_str++ = *s++;
230 *date_str++ = *s++;
231 *date_str++ = *s++;
232 *date_str++ = ' ';
233 }
234 *date_str++ = xt.tm_mday / 10 + '0';
235 *date_str++ = xt.tm_mday % 10 + '0';
236 *date_str++ = ' ';
237 *date_str++ = xt.tm_hour / 10 + '0';
238 *date_str++ = xt.tm_hour % 10 + '0';
239 *date_str++ = ':';
240 *date_str++ = xt.tm_min / 10 + '0';
241 *date_str++ = xt.tm_min % 10 + '0';
242 *date_str++ = ':';
243 *date_str++ = xt.tm_sec / 10 + '0';
244 *date_str++ = xt.tm_sec % 10 + '0';
246 int div;
247 int usec = (int)xt.tm_usec;
248 *date_str++ = '.';
249 for (div=100000; div>0; div=div/10) {
250 *date_str++ = usec / div + '0';
251 usec = usec % div;
252 }
253 }
255 *date_str++ = ' ';
256 *date_str++ = real_year / 1000 + '0';
257 *date_str++ = real_year % 1000 / 100 + '0';
258 *date_str++ = real_year % 100 / 10 + '0';
259 *date_str++ = real_year % 10 + '0';
260 }
262 int off = xt.tm_gmtoff, off_hh, off_mm;
263 char sign = '+';
264 if (off < 0) {
265 off = -off;
266 sign = '-';
267 }
268 off_hh = off / 3600;
269 off_mm = off % 3600 / 60;
270 *date_str++ = ' ';
271 *date_str++ = sign;
272 *date_str++ = off_hh / 10 + '0';
273 *date_str++ = off_hh % 10 + '0';
274 *date_str++ = off_mm / 10 + '0';
275 *date_str++ = off_mm % 10 + '0';
276 }
277 *date_str = 0;
278
279 return APR_SUCCESS;
280}
281
283{
284 /* ### This code is a clone of apr_rfc822_date(), except that it
285 * uses ap_explode_recent_gmt() instead of apr_time_exp_gmt().
286 */
288 const char *s;
289 int real_year;
290
292
293 /* example: "Sat, 08 Jan 2000 18:31:41 GMT" */
294 /* 12345678901234567890123456789 */
295
296 s = &apr_day_snames[xt.tm_wday][0];
297 *date_str++ = *s++;
298 *date_str++ = *s++;
299 *date_str++ = *s++;
300 *date_str++ = ',';
301 *date_str++ = ' ';
302 *date_str++ = xt.tm_mday / 10 + '0';
303 *date_str++ = xt.tm_mday % 10 + '0';
304 *date_str++ = ' ';
306 *date_str++ = *s++;
307 *date_str++ = *s++;
308 *date_str++ = *s++;
309 *date_str++ = ' ';
310 real_year = 1900 + xt.tm_year;
311 /* This routine isn't y10k ready. */
312 *date_str++ = real_year / 1000 + '0';
313 *date_str++ = real_year % 1000 / 100 + '0';
314 *date_str++ = real_year % 100 / 10 + '0';
315 *date_str++ = real_year % 10 + '0';
316 *date_str++ = ' ';
317 *date_str++ = xt.tm_hour / 10 + '0';
318 *date_str++ = xt.tm_hour % 10 + '0';
319 *date_str++ = ':';
320 *date_str++ = xt.tm_min / 10 + '0';
321 *date_str++ = xt.tm_min % 10 + '0';
322 *date_str++ = ':';
323 *date_str++ = xt.tm_sec / 10 + '0';
324 *date_str++ = xt.tm_sec % 10 + '0';
325 *date_str++ = ' ';
326 *date_str++ = 'G';
327 *date_str++ = 'M';
328 *date_str++ = 'T';
329 *date_str++ = 0;
330 return APR_SUCCESS;
331}
#define AP_DECLARE(type)
Definition ap_config.h:67
const char apr_size_t len
Definition ap_regex.h:187
request_rec * r
apr_status_t ap_explode_recent_localtime(apr_time_exp_t *tm, apr_time_t t)
Definition util_time.c:146
#define AP_CTIME_OPTION_GMTOFF
Definition util_time.h:51
apr_status_t ap_recent_ctime_ex(char *date_str, apr_time_t t, int option, int *len)
Definition util_time.c:164
apr_status_t ap_recent_rfc822_date(char *date_str, apr_time_t t)
Definition util_time.c:282
#define AP_CTIME_OPTION_USEC
Definition util_time.h:47
#define AP_CTIME_OPTION_NONE
Definition util_time.h:45
#define AP_CTIME_OPTION_COMPACT
Definition util_time.h:49
apr_status_t ap_recent_ctime(char *date_str, apr_time_t t)
Definition util_time.c:158
apr_status_t ap_explode_recent_gmt(apr_time_exp_t *tm, apr_time_t t)
Definition util_time.c:152
#define APR_ENOMEM
Definition apr_errno.h:683
apr_size_t size
#define APR_SUCCESS
Definition apr_errno.h:225
int apr_status_t
Definition apr_errno.h:44
apr_interval_time_t t
const char * s
Definition apr_strings.h:95
#define APR_CTIME_LEN
Definition apr_time.h:198
apr_size_t apr_size_t const char apr_time_exp_t * tm
Definition apr_time.h:221
APR_DECLARE_DATA const char apr_month_snames[12][4]
Definition timestr.c:33
APR_DECLARE_DATA const char apr_day_snames[7][4]
Definition timestr.c:37
apr_int64_t apr_time_t
Definition apr_time.h:45
#define apr_time_sec(time)
Definition apr_time.h:63
#define apr_time_usec(time)
Definition apr_time.h:66
return NULL
Definition mod_so.c:359
apr_int32_t tm_gmtoff
Definition apr_time.h:119
apr_int32_t tm_sec
Definition apr_time.h:101
apr_int32_t tm_hour
Definition apr_time.h:105
apr_int32_t tm_year
Definition apr_time.h:111
apr_int32_t tm_min
Definition apr_time.h:103
apr_int32_t tm_wday
Definition apr_time.h:113
apr_int32_t tm_mday
Definition apr_time.h:107
apr_int32_t tm_mon
Definition apr_time.h:109
apr_int32_t tm_usec
Definition apr_time.h:99
#define TIME_CACHE_MASK
Definition util_time.c:49
static struct exploded_time_cache_element exploded_cache_gmt[(15+1)]
Definition util_time.c:52
#define AP_CTIME_GMTOFF_LEN
Definition util_time.c:29
static struct exploded_time_cache_element exploded_cache_localtime[(15+1)]
Definition util_time.c:51
#define TIME_CACHE_SIZE
Definition util_time.c:41
static apr_status_t cached_explode(apr_time_exp_t *xt, apr_time_t t, struct exploded_time_cache_element *cache, int use_gmt)
Definition util_time.c:55
#define AP_CTIME_COMPACT_LEN
Definition util_time.c:26
#define AP_CTIME_USEC_LENGTH
Definition util_time.c:23
Apache date-time handling functions.
typedef int(WSAAPI *apr_winapi_fpt_WSAPoll)(IN OUT LPWSAPOLLFD fdArray