Merge commit 'v0.8.40'
[nginx-catap.git] / src / core / ngx_times.c
blob741acc3a7730b17352cb7a254cb2c84a9f0959ed
2 /*
3 * Copyright (C) Igor Sysoev
4 */
7 #include <ngx_config.h>
8 #include <ngx_core.h>
12 * The time may be updated by signal handler or by several threads.
13 * The time update operations are rare and require to hold the ngx_time_lock.
14 * The time read operations are frequent, so they are lock-free and get time
15 * values and strings from the current slot. Thus thread may get the corrupted
16 * values only if it is preempted while copying and then it is not scheduled
17 * to run more than NGX_TIME_SLOTS seconds.
20 #define NGX_TIME_SLOTS 64
22 static ngx_uint_t slot;
23 static ngx_atomic_t ngx_time_lock;
25 volatile ngx_msec_t ngx_current_msec;
26 volatile ngx_time_t *ngx_cached_time;
27 volatile ngx_str_t ngx_cached_err_log_time;
28 volatile ngx_str_t ngx_cached_http_time;
29 volatile ngx_str_t ngx_cached_http_log_time;
31 #if !(NGX_WIN32)
34 * locatime() and localtime_r() are not Async-Signal-Safe functions, therefore,
35 * they must not be called by a signal handler, so we use the cached
36 * GMT offset value. Fortunately the value is changed only two times a year.
39 static ngx_int_t cached_gmtoff;
40 #endif
42 static ngx_time_t cached_time[NGX_TIME_SLOTS];
43 static u_char cached_err_log_time[NGX_TIME_SLOTS]
44 [sizeof("1970/09/28 12:00:00")];
45 static u_char cached_http_time[NGX_TIME_SLOTS]
46 [sizeof("Mon, 28 Sep 1970 06:00:00 GMT")];
47 static u_char cached_http_log_time[NGX_TIME_SLOTS]
48 [sizeof("28/Sep/1970:12:00:00 +0600")];
51 static char *week[] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
52 static char *months[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun",
53 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
55 void
56 ngx_time_init(void)
58 ngx_cached_err_log_time.len = sizeof("1970/09/28 12:00:00") - 1;
59 ngx_cached_http_time.len = sizeof("Mon, 28 Sep 1970 06:00:00 GMT") - 1;
60 ngx_cached_http_log_time.len = sizeof("28/Sep/1970:12:00:00 +0600") - 1;
62 ngx_cached_time = &cached_time[0];
64 ngx_time_update();
68 void
69 ngx_time_update(void)
71 u_char *p0, *p1, *p2;
72 ngx_tm_t tm, gmt;
73 time_t sec;
74 ngx_uint_t msec;
75 ngx_time_t *tp;
76 ngx_timeval_t tv;
78 if (!ngx_trylock(&ngx_time_lock)) {
79 return;
82 ngx_gettimeofday(&tv);
84 sec = tv.tv_sec;
85 msec = tv.tv_usec / 1000;
87 ngx_current_msec = (ngx_msec_t) sec * 1000 + msec;
89 tp = &cached_time[slot];
91 if (tp->sec == sec) {
92 tp->msec = msec;
93 ngx_unlock(&ngx_time_lock);
94 return;
97 if (slot == NGX_TIME_SLOTS - 1) {
98 slot = 0;
99 } else {
100 slot++;
103 tp = &cached_time[slot];
105 tp->sec = sec;
106 tp->msec = msec;
108 ngx_gmtime(sec, &gmt);
111 p0 = &cached_http_time[slot][0];
113 (void) ngx_sprintf(p0, "%s, %02d %s %4d %02d:%02d:%02d GMT",
114 week[gmt.ngx_tm_wday], gmt.ngx_tm_mday,
115 months[gmt.ngx_tm_mon - 1], gmt.ngx_tm_year,
116 gmt.ngx_tm_hour, gmt.ngx_tm_min, gmt.ngx_tm_sec);
118 #if (NGX_HAVE_GETTIMEZONE)
120 tp->gmtoff = ngx_gettimezone();
121 ngx_gmtime(sec + tp->gmtoff * 60, &tm);
123 #elif (NGX_HAVE_GMTOFF)
125 ngx_localtime(sec, &tm);
126 cached_gmtoff = (ngx_int_t) (tm.ngx_tm_gmtoff / 60);
127 tp->gmtoff = cached_gmtoff;
129 #else
131 ngx_localtime(sec, &tm);
132 cached_gmtoff = ngx_timezone(tm.ngx_tm_isdst);
133 tp->gmtoff = cached_gmtoff;
135 #endif
138 p1 = &cached_err_log_time[slot][0];
140 (void) ngx_sprintf(p1, "%4d/%02d/%02d %02d:%02d:%02d",
141 tm.ngx_tm_year, tm.ngx_tm_mon,
142 tm.ngx_tm_mday, tm.ngx_tm_hour,
143 tm.ngx_tm_min, tm.ngx_tm_sec);
146 p2 = &cached_http_log_time[slot][0];
148 (void) ngx_sprintf(p2, "%02d/%s/%d:%02d:%02d:%02d %c%02d%02d",
149 tm.ngx_tm_mday, months[tm.ngx_tm_mon - 1],
150 tm.ngx_tm_year, tm.ngx_tm_hour,
151 tm.ngx_tm_min, tm.ngx_tm_sec,
152 tp->gmtoff < 0 ? '-' : '+',
153 ngx_abs(tp->gmtoff / 60), ngx_abs(tp->gmtoff % 60));
156 ngx_memory_barrier();
158 ngx_cached_time = tp;
159 ngx_cached_http_time.data = p0;
160 ngx_cached_err_log_time.data = p1;
161 ngx_cached_http_log_time.data = p2;
163 ngx_unlock(&ngx_time_lock);
167 #if !(NGX_WIN32)
169 void
170 ngx_time_sigsafe_update(void)
172 u_char *p;
173 ngx_tm_t tm;
174 time_t sec;
175 ngx_time_t *tp;
176 struct timeval tv;
178 if (!ngx_trylock(&ngx_time_lock)) {
179 return;
182 ngx_gettimeofday(&tv);
184 sec = tv.tv_sec;
186 tp = &cached_time[slot];
188 if (tp->sec == sec) {
189 ngx_unlock(&ngx_time_lock);
190 return;
193 if (slot == NGX_TIME_SLOTS - 1) {
194 slot = 0;
195 } else {
196 slot++;
199 ngx_gmtime(sec + cached_gmtoff * 60, &tm);
201 p = &cached_err_log_time[slot][0];
203 (void) ngx_sprintf(p, "%4d/%02d/%02d %02d:%02d:%02d",
204 tm.ngx_tm_year, tm.ngx_tm_mon,
205 tm.ngx_tm_mday, tm.ngx_tm_hour,
206 tm.ngx_tm_min, tm.ngx_tm_sec);
208 ngx_memory_barrier();
210 ngx_cached_err_log_time.data = p;
212 ngx_unlock(&ngx_time_lock);
215 #endif
218 u_char *
219 ngx_http_time(u_char *buf, time_t t)
221 ngx_tm_t tm;
223 ngx_gmtime(t, &tm);
225 return ngx_sprintf(buf, "%s, %02d %s %4d %02d:%02d:%02d GMT",
226 week[tm.ngx_tm_wday],
227 tm.ngx_tm_mday,
228 months[tm.ngx_tm_mon - 1],
229 tm.ngx_tm_year,
230 tm.ngx_tm_hour,
231 tm.ngx_tm_min,
232 tm.ngx_tm_sec);
236 u_char *
237 ngx_http_cookie_time(u_char *buf, time_t t)
239 ngx_tm_t tm;
241 ngx_gmtime(t, &tm);
244 * Netscape 3.x does not understand 4-digit years at all and
245 * 2-digit years more than "37"
248 return ngx_sprintf(buf,
249 (tm.ngx_tm_year > 2037) ?
250 "%s, %02d-%s-%d %02d:%02d:%02d GMT":
251 "%s, %02d-%s-%02d %02d:%02d:%02d GMT",
252 week[tm.ngx_tm_wday],
253 tm.ngx_tm_mday,
254 months[tm.ngx_tm_mon - 1],
255 (tm.ngx_tm_year > 2037) ? tm.ngx_tm_year:
256 tm.ngx_tm_year % 100,
257 tm.ngx_tm_hour,
258 tm.ngx_tm_min,
259 tm.ngx_tm_sec);
263 void
264 ngx_gmtime(time_t t, ngx_tm_t *tp)
266 ngx_int_t yday;
267 ngx_uint_t n, sec, min, hour, mday, mon, year, wday, days, leap;
269 /* the calculation is valid for positive time_t only */
271 n = (ngx_uint_t) t;
273 days = n / 86400;
275 /* Jaunary 1, 1970 was Thursday */
277 wday = (4 + days) % 7;
279 n %= 86400;
280 hour = n / 3600;
281 n %= 3600;
282 min = n / 60;
283 sec = n % 60;
286 * the algorithm based on Gauss' formula,
287 * see src/http/ngx_http_parse_time.c
290 /* days since March 1, 1 BC */
291 days = days - (31 + 28) + 719527;
294 * The "days" should be adjusted to 1 only, however, some March 1st's go
295 * to previous year, so we adjust them to 2. This causes also shift of the
296 * last Feburary days to next year, but we catch the case when "yday"
297 * becomes negative.
300 year = (days + 2) * 400 / (365 * 400 + 100 - 4 + 1);
302 yday = days - (365 * year + year / 4 - year / 100 + year / 400);
304 if (yday < 0) {
305 leap = (year % 4 == 0) && (year % 100 || (year % 400 == 0));
306 yday = 365 + leap + yday;
307 year--;
311 * The empirical formula that maps "yday" to month.
312 * There are at least 10 variants, some of them are:
313 * mon = (yday + 31) * 15 / 459
314 * mon = (yday + 31) * 17 / 520
315 * mon = (yday + 31) * 20 / 612
318 mon = (yday + 31) * 10 / 306;
320 /* the Gauss' formula that evaluates days before the month */
322 mday = yday - (367 * mon / 12 - 30) + 1;
324 if (yday >= 306) {
326 year++;
327 mon -= 10;
330 * there is no "yday" in Win32 SYSTEMTIME
332 * yday -= 306;
335 } else {
337 mon += 2;
340 * there is no "yday" in Win32 SYSTEMTIME
342 * yday += 31 + 28 + leap;
346 tp->ngx_tm_sec = (ngx_tm_sec_t) sec;
347 tp->ngx_tm_min = (ngx_tm_min_t) min;
348 tp->ngx_tm_hour = (ngx_tm_hour_t) hour;
349 tp->ngx_tm_mday = (ngx_tm_mday_t) mday;
350 tp->ngx_tm_mon = (ngx_tm_mon_t) mon;
351 tp->ngx_tm_year = (ngx_tm_year_t) year;
352 tp->ngx_tm_wday = (ngx_tm_wday_t) wday;
356 time_t
357 ngx_next_time(time_t when)
359 time_t now, next;
360 struct tm tm;
362 now = ngx_time();
364 ngx_libc_localtime(now, &tm);
366 tm.tm_hour = (int) (when / 3600);
367 when %= 3600;
368 tm.tm_min = (int) (when / 60);
369 tm.tm_sec = (int) (when % 60);
371 next = mktime(&tm);
373 if (next == -1) {
374 return -1;
377 if (next - now > 0) {
378 return next;
381 tm.tm_mday++;
383 /* mktime() should normalize a date (Jan 32, etc) */
385 next = mktime(&tm);
387 if (next != -1) {
388 return next;
391 return -1;