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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include "util_time.h"
20 /* Number of characters needed to format the microsecond part of a timestamp.
21 * Microseconds have 6 digits plus one separator character makes 7.
23 #define AP_CTIME_USEC_LENGTH 7
25 /* Length of ISO 8601 date/time */
26 #define AP_CTIME_COMPACT_LEN 20
29 /* Cache for exploded values of recent timestamps
32 struct exploded_time_cache_element {
35 apr_int64_t t_validate; /* please see comments in cached_explode() */
38 /* the "+ 1" is for the current second: */
39 #define TIME_CACHE_SIZE (AP_TIME_RECENT_THRESHOLD + 1)
41 /* Note that AP_TIME_RECENT_THRESHOLD is defined to
42 * be a power of two minus one in util_time.h, so that
43 * we can replace a modulo operation with a bitwise AND
44 * when hashing items into a cache of size
45 * AP_TIME_RECENT_THRESHOLD+1
47 #define TIME_CACHE_MASK (AP_TIME_RECENT_THRESHOLD)
49 static struct exploded_time_cache_element exploded_cache_localtime[TIME_CACHE_SIZE];
50 static struct exploded_time_cache_element exploded_cache_gmt[TIME_CACHE_SIZE];
53 static apr_status_t cached_explode(apr_time_exp_t *xt, apr_time_t t,
54 struct exploded_time_cache_element *cache,
57 apr_int64_t seconds = apr_time_sec(t);
58 struct exploded_time_cache_element *cache_element =
59 &(cache[seconds & TIME_CACHE_MASK]);
60 struct exploded_time_cache_element cache_element_snapshot;
62 /* The cache is implemented as a ring buffer. Each second,
63 * it uses a different element in the buffer. The timestamp
64 * in the element indicates whether the element contains the
65 * exploded time for the current second (vs the time
66 * 'now - AP_TIME_RECENT_THRESHOLD' seconds ago). If the
67 * cached value is for the current time, we use it. Otherwise,
68 * we compute the apr_time_exp_t and store it in this
69 * cache element. Note that the timestamp in the cache
70 * element is updated only after the exploded time. Thus
71 * if two threads hit this cache element simultaneously
72 * at the start of a new second, they'll both explode the
73 * time and store it. I.e., the writers will collide, but
74 * they'll be writing the same value.
76 if (cache_element->t >= seconds) {
77 /* There is an intentional race condition in this design:
78 * in a multithreaded app, one thread might be reading
79 * from this cache_element to resolve a timestamp from
80 * TIME_CACHE_SIZE seconds ago at the same time that
81 * another thread is copying the exploded form of the
82 * current time into the same cache_element. (I.e., the
83 * first thread might hit this element of the ring buffer
84 * just as the element is being recycled.) This can
85 * also happen at the start of a new second, if a
86 * reader accesses the cache_element after a writer
87 * has updated cache_element.t but before the writer
88 * has finished updating the whole cache_element.
90 * Rather than trying to prevent this race condition
91 * with locks, we allow it to happen and then detect
92 * and correct it. The detection works like this:
93 * Step 1: Take a "snapshot" of the cache element by
94 * copying it into a temporary buffer.
95 * Step 2: Check whether the snapshot contains consistent
96 * data: the timestamps at the start and end of
97 * the cache_element should both match the 'seconds'
98 * value that we computed from the input time.
99 * If these three don't match, then the snapshot
100 * shows the cache_element in the middle of an
101 * update, and its contents are invalid.
102 * Step 3: If the snapshot is valid, use it. Otherwise,
103 * just give up on the cache and explode the
106 memcpy(&cache_element_snapshot, cache_element,
107 sizeof(struct exploded_time_cache_element));
108 if ((seconds != cache_element_snapshot.t) ||
109 (seconds != cache_element_snapshot.t_validate)) {
110 /* Invalid snapshot */
112 return apr_time_exp_gmt(xt, t);
115 return apr_time_exp_lt(xt, t);
120 memcpy(xt, &(cache_element_snapshot.xt),
121 sizeof(apr_time_exp_t));
127 r = apr_time_exp_gmt(xt, t);
130 r = apr_time_exp_lt(xt, t);
132 if (r != APR_SUCCESS) {
135 cache_element->t = seconds;
136 memcpy(&(cache_element->xt), xt, sizeof(apr_time_exp_t));
137 cache_element->t_validate = seconds;
139 xt->tm_usec = (int)apr_time_usec(t);
144 AP_DECLARE(apr_status_t) ap_explode_recent_localtime(apr_time_exp_t * tm,
147 return cached_explode(tm, t, exploded_cache_localtime, 0);
150 AP_DECLARE(apr_status_t) ap_explode_recent_gmt(apr_time_exp_t * tm,
153 return cached_explode(tm, t, exploded_cache_gmt, 1);
156 AP_DECLARE(apr_status_t) ap_recent_ctime(char *date_str, apr_time_t t)
158 int len = APR_CTIME_LEN;
159 return ap_recent_ctime_ex(date_str, t, AP_CTIME_OPTION_NONE, &len);
162 AP_DECLARE(apr_status_t) ap_recent_ctime_ex(char *date_str, apr_time_t t,
163 int option, int *len)
165 /* ### This code is a clone of apr_ctime(), except that it
166 * uses ap_explode_recent_localtime() instead of apr_time_exp_lt().
174 /* Calculate the needed buffer length */
175 if (option & AP_CTIME_OPTION_COMPACT)
176 needed = AP_CTIME_COMPACT_LEN;
178 needed = APR_CTIME_LEN;
180 if (option & AP_CTIME_OPTION_USEC) {
181 needed += AP_CTIME_USEC_LENGTH;
184 /* Check the provided buffer length */
185 if (len && *len >= needed) {
195 /* example without options: "Wed Jun 30 21:49:08 1993" */
196 /* 123456789012345678901234 */
197 /* example for compact format: "1993-06-30 21:49:08" */
198 /* 1234567890123456789 */
200 ap_explode_recent_localtime(&xt, t);
201 real_year = 1900 + xt.tm_year;
202 if (option & AP_CTIME_OPTION_COMPACT) {
203 int real_month = xt.tm_mon + 1;
204 *date_str++ = real_year / 1000 + '0';
205 *date_str++ = real_year % 1000 / 100 + '0';
206 *date_str++ = real_year % 100 / 10 + '0';
207 *date_str++ = real_year % 10 + '0';
209 *date_str++ = real_month / 10 + '0';
210 *date_str++ = real_month % 10 + '0';
214 s = &apr_day_snames[xt.tm_wday][0];
219 s = &apr_month_snames[xt.tm_mon][0];
225 *date_str++ = xt.tm_mday / 10 + '0';
226 *date_str++ = xt.tm_mday % 10 + '0';
228 *date_str++ = xt.tm_hour / 10 + '0';
229 *date_str++ = xt.tm_hour % 10 + '0';
231 *date_str++ = xt.tm_min / 10 + '0';
232 *date_str++ = xt.tm_min % 10 + '0';
234 *date_str++ = xt.tm_sec / 10 + '0';
235 *date_str++ = xt.tm_sec % 10 + '0';
236 if (option & AP_CTIME_OPTION_USEC) {
238 int usec = (int)xt.tm_usec;
240 for (div=100000; div>0; div=div/10) {
241 *date_str++ = usec / div + '0';
245 if (!(option & AP_CTIME_OPTION_COMPACT)) {
247 *date_str++ = real_year / 1000 + '0';
248 *date_str++ = real_year % 1000 / 100 + '0';
249 *date_str++ = real_year % 100 / 10 + '0';
250 *date_str++ = real_year % 10 + '0';
257 AP_DECLARE(apr_status_t) ap_recent_rfc822_date(char *date_str, apr_time_t t)
259 /* ### This code is a clone of apr_rfc822_date(), except that it
260 * uses ap_explode_recent_gmt() instead of apr_time_exp_gmt().
266 ap_explode_recent_gmt(&xt, t);
268 /* example: "Sat, 08 Jan 2000 18:31:41 GMT" */
269 /* 12345678901234567890123456789 */
271 s = &apr_day_snames[xt.tm_wday][0];
277 *date_str++ = xt.tm_mday / 10 + '0';
278 *date_str++ = xt.tm_mday % 10 + '0';
280 s = &apr_month_snames[xt.tm_mon][0];
285 real_year = 1900 + xt.tm_year;
286 /* This routine isn't y10k ready. */
287 *date_str++ = real_year / 1000 + '0';
288 *date_str++ = real_year % 1000 / 100 + '0';
289 *date_str++ = real_year % 100 / 10 + '0';
290 *date_str++ = real_year % 10 + '0';
292 *date_str++ = xt.tm_hour / 10 + '0';
293 *date_str++ = xt.tm_hour % 10 + '0';
295 *date_str++ = xt.tm_min / 10 + '0';
296 *date_str++ = xt.tm_min % 10 + '0';
298 *date_str++ = xt.tm_sec / 10 + '0';
299 *date_str++ = xt.tm_sec % 10 + '0';