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"
22 /* Number of characters needed to format the microsecond part of a timestamp.
23 * Microseconds have 6 digits plus one separator character makes 7.
25 #define AP_CTIME_USEC_LENGTH 7
27 /* Length of ISO 8601 date/time */
28 #define AP_CTIME_COMPACT_LEN 20
31 /* Cache for exploded values of recent timestamps
34 struct exploded_time_cache_element {
37 apr_int64_t t_validate; /* please see comments in cached_explode() */
40 /* the "+ 1" is for the current second: */
41 #define TIME_CACHE_SIZE (AP_TIME_RECENT_THRESHOLD + 1)
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
49 #define TIME_CACHE_MASK (AP_TIME_RECENT_THRESHOLD)
51 static struct exploded_time_cache_element exploded_cache_localtime[TIME_CACHE_SIZE];
52 static struct exploded_time_cache_element exploded_cache_gmt[TIME_CACHE_SIZE];
55 static apr_status_t cached_explode(apr_time_exp_t *xt, apr_time_t t,
56 struct exploded_time_cache_element *cache,
59 apr_int64_t seconds = apr_time_sec(t);
60 struct exploded_time_cache_element *cache_element =
61 &(cache[seconds & TIME_CACHE_MASK]);
62 struct exploded_time_cache_element cache_element_snapshot;
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.
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.
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
108 memcpy(&cache_element_snapshot, cache_element,
109 sizeof(struct exploded_time_cache_element));
110 if ((seconds != cache_element_snapshot.t) ||
111 (seconds != cache_element_snapshot.t_validate)) {
112 /* Invalid snapshot */
114 return apr_time_exp_gmt(xt, t);
117 return apr_time_exp_lt(xt, t);
122 memcpy(xt, &(cache_element_snapshot.xt),
123 sizeof(apr_time_exp_t));
129 r = apr_time_exp_gmt(xt, t);
132 r = apr_time_exp_lt(xt, t);
134 if (r != APR_SUCCESS) {
137 cache_element->t = seconds;
138 memcpy(&(cache_element->xt), xt, sizeof(apr_time_exp_t));
139 cache_element->t_validate = seconds;
141 xt->tm_usec = (int)apr_time_usec(t);
146 AP_DECLARE(apr_status_t) ap_explode_recent_localtime(apr_time_exp_t * tm,
149 return cached_explode(tm, t, exploded_cache_localtime, 0);
152 AP_DECLARE(apr_status_t) ap_explode_recent_gmt(apr_time_exp_t * tm,
155 return cached_explode(tm, t, exploded_cache_gmt, 1);
158 AP_DECLARE(apr_status_t) ap_recent_ctime(char *date_str, apr_time_t t)
160 int len = APR_CTIME_LEN;
161 return ap_recent_ctime_ex(date_str, t, AP_CTIME_OPTION_NONE, &len);
164 AP_DECLARE(apr_status_t) ap_recent_ctime_ex(char *date_str, apr_time_t t,
165 int option, int *len)
167 /* ### This code is a clone of apr_ctime(), except that it
168 * uses ap_explode_recent_localtime() instead of apr_time_exp_lt().
176 /* Calculate the needed buffer length */
177 if (option & AP_CTIME_OPTION_COMPACT)
178 needed = AP_CTIME_COMPACT_LEN;
180 needed = APR_CTIME_LEN;
182 if (option & AP_CTIME_OPTION_USEC) {
183 needed += AP_CTIME_USEC_LENGTH;
186 /* Check the provided buffer length */
187 if (len && *len >= needed) {
197 /* example without options: "Wed Jun 30 21:49:08 1993" */
198 /* 123456789012345678901234 */
199 /* example for compact format: "1993-06-30 21:49:08" */
200 /* 1234567890123456789 */
202 ap_explode_recent_localtime(&xt, t);
203 real_year = 1900 + xt.tm_year;
204 if (option & AP_CTIME_OPTION_COMPACT) {
205 int real_month = xt.tm_mon + 1;
206 *date_str++ = real_year / 1000 + '0';
207 *date_str++ = real_year % 1000 / 100 + '0';
208 *date_str++ = real_year % 100 / 10 + '0';
209 *date_str++ = real_year % 10 + '0';
211 *date_str++ = real_month / 10 + '0';
212 *date_str++ = real_month % 10 + '0';
216 s = &apr_day_snames[xt.tm_wday][0];
221 s = &apr_month_snames[xt.tm_mon][0];
227 *date_str++ = xt.tm_mday / 10 + '0';
228 *date_str++ = xt.tm_mday % 10 + '0';
230 *date_str++ = xt.tm_hour / 10 + '0';
231 *date_str++ = xt.tm_hour % 10 + '0';
233 *date_str++ = xt.tm_min / 10 + '0';
234 *date_str++ = xt.tm_min % 10 + '0';
236 *date_str++ = xt.tm_sec / 10 + '0';
237 *date_str++ = xt.tm_sec % 10 + '0';
238 if (option & AP_CTIME_OPTION_USEC) {
240 int usec = (int)xt.tm_usec;
242 for (div=100000; div>0; div=div/10) {
243 *date_str++ = usec / div + '0';
247 if (!(option & AP_CTIME_OPTION_COMPACT)) {
249 *date_str++ = real_year / 1000 + '0';
250 *date_str++ = real_year % 1000 / 100 + '0';
251 *date_str++ = real_year % 100 / 10 + '0';
252 *date_str++ = real_year % 10 + '0';
259 AP_DECLARE(apr_status_t) ap_recent_rfc822_date(char *date_str, apr_time_t t)
261 /* ### This code is a clone of apr_rfc822_date(), except that it
262 * uses ap_explode_recent_gmt() instead of apr_time_exp_gmt().
268 ap_explode_recent_gmt(&xt, t);
270 /* example: "Sat, 08 Jan 2000 18:31:41 GMT" */
271 /* 12345678901234567890123456789 */
273 s = &apr_day_snames[xt.tm_wday][0];
279 *date_str++ = xt.tm_mday / 10 + '0';
280 *date_str++ = xt.tm_mday % 10 + '0';
282 s = &apr_month_snames[xt.tm_mon][0];
287 real_year = 1900 + xt.tm_year;
288 /* This routine isn't y10k ready. */
289 *date_str++ = real_year / 1000 + '0';
290 *date_str++ = real_year % 1000 / 100 + '0';
291 *date_str++ = real_year % 100 / 10 + '0';
292 *date_str++ = real_year % 10 + '0';
294 *date_str++ = xt.tm_hour / 10 + '0';
295 *date_str++ = xt.tm_hour % 10 + '0';
297 *date_str++ = xt.tm_min / 10 + '0';
298 *date_str++ = xt.tm_min % 10 + '0';
300 *date_str++ = xt.tm_sec / 10 + '0';
301 *date_str++ = xt.tm_sec % 10 + '0';
310 AP_DECLARE(void) ap_force_set_tz(apr_pool_t *p) {
311 /* If the TZ variable is unset, many operating systems,
312 * such as Linux, will at runtime read from /etc/localtime
313 * and call fstat on it.
315 * By forcing the time zone to UTC if it is unset, we gain
316 * about 2% in raw requests/second (since we format log files
317 * in the local time, if present)
319 * For more info, see:
320 * <http://www.gnu.org/s/hello/manual/libc/TZ-Variable.html>
324 if (apr_env_get(&v, "TZ", p) != APR_SUCCESS) {
325 apr_env_set("TZ", "UTC+0", p);