3 * Copyright (C) Igor Sysoev
7 #include <ngx_config.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 static ngx_time_t cached_time[NGX_TIME_SLOTS];
32 static u_char cached_err_log_time[NGX_TIME_SLOTS]
33 [sizeof("1970/09/28 12:00:00")];
34 static u_char cached_http_time[NGX_TIME_SLOTS]
35 [sizeof("Mon, 28 Sep 1970 06:00:00 GMT")];
36 static u_char cached_http_log_time[NGX_TIME_SLOTS]
37 [sizeof("28/Sep/1970:12:00:00 +0600")];
40 static char *week[] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
41 static char *months[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun",
42 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
47 ngx_cached_err_log_time.len = sizeof("1970/09/28 12:00:00") - 1;
48 ngx_cached_http_time.len = sizeof("Mon, 28 Sep 1970 06:00:00 GMT") - 1;
49 ngx_cached_http_log_time.len = sizeof("28/Sep/1970:12:00:00 +0600") - 1;
51 ngx_cached_time = &cached_time[0];
53 ngx_time_update(0, 0);
58 ngx_time_update(time_t sec, ngx_uint_t msec)
65 if (!ngx_trylock(&ngx_time_lock)) {
70 ngx_gettimeofday(&tv);
73 msec = tv.tv_usec / 1000;
76 ngx_current_msec = (ngx_msec_t) sec * 1000 + msec;
78 tp = &cached_time[slot];
82 ngx_unlock(&ngx_time_lock);
86 if (slot == NGX_TIME_SLOTS - 1) {
92 tp = &cached_time[slot];
97 ngx_gmtime(sec, &gmt);
100 p0 = &cached_http_time[slot][0];
102 (void) ngx_sprintf(p0, "%s, %02d %s %4d %02d:%02d:%02d GMT",
103 week[gmt.ngx_tm_wday], gmt.ngx_tm_mday,
104 months[gmt.ngx_tm_mon - 1], gmt.ngx_tm_year,
105 gmt.ngx_tm_hour, gmt.ngx_tm_min, gmt.ngx_tm_sec);
107 #if (NGX_HAVE_GETTIMEZONE)
109 tp->gmtoff = ngx_gettimezone();
110 ngx_gmtime(sec + tp->gmtoff * 60, &tm);
112 #elif (NGX_HAVE_GMTOFF)
114 ngx_localtime(sec, &tm);
115 tp->gmtoff = (ngx_int_t) (tm.ngx_tm_gmtoff / 60);
119 ngx_localtime(sec, &tm);
120 tp->gmtoff = ngx_timezone(tm.ngx_tm_isdst);
125 p1 = &cached_err_log_time[slot][0];
127 (void) ngx_sprintf(p1, "%4d/%02d/%02d %02d:%02d:%02d",
128 tm.ngx_tm_year, tm.ngx_tm_mon,
129 tm.ngx_tm_mday, tm.ngx_tm_hour,
130 tm.ngx_tm_min, tm.ngx_tm_sec);
133 p2 = &cached_http_log_time[slot][0];
135 (void) ngx_sprintf(p2, "%02d/%s/%d:%02d:%02d:%02d %c%02d%02d",
136 tm.ngx_tm_mday, months[tm.ngx_tm_mon - 1],
137 tm.ngx_tm_year, tm.ngx_tm_hour,
138 tm.ngx_tm_min, tm.ngx_tm_sec,
139 tp->gmtoff < 0 ? '-' : '+',
140 ngx_abs(tp->gmtoff / 60), ngx_abs(tp->gmtoff % 60));
143 ngx_memory_barrier();
145 ngx_cached_time = tp;
146 ngx_cached_http_time.data = p0;
147 ngx_cached_err_log_time.data = p1;
148 ngx_cached_http_log_time.data = p2;
150 ngx_unlock(&ngx_time_lock);
155 ngx_http_time(u_char *buf, time_t t)
161 return ngx_sprintf(buf, "%s, %02d %s %4d %02d:%02d:%02d GMT",
162 week[tm.ngx_tm_wday],
164 months[tm.ngx_tm_mon - 1],
173 ngx_http_cookie_time(u_char *buf, time_t t)
180 * Netscape 3.x does not understand 4-digit years at all and
181 * 2-digit years more than "37"
184 return ngx_sprintf(buf,
185 (tm.ngx_tm_year > 2037) ?
186 "%s, %02d-%s-%d %02d:%02d:%02d GMT":
187 "%s, %02d-%s-%02d %02d:%02d:%02d GMT",
188 week[tm.ngx_tm_wday],
190 months[tm.ngx_tm_mon - 1],
191 (tm.ngx_tm_year > 2037) ? tm.ngx_tm_year:
192 tm.ngx_tm_year % 100,
200 ngx_gmtime(time_t t, ngx_tm_t *tp)
203 ngx_uint_t n, sec, min, hour, mday, mon, year, wday, days, leap;
205 /* the calculation is valid for positive time_t only */
211 /* Jaunary 1, 1970 was Thursday */
213 wday = (4 + days) % 7;
222 * the algorithm based on Gauss' formula,
223 * see src/http/ngx_http_parse_time.c
226 /* days since March 1, 1 BC */
227 days = days - (31 + 28) + 719527;
230 * The "days" should be adjusted to 1 only, however, some March 1st's go
231 * to previous year, so we adjust them to 2. This causes also shift of the
232 * last Feburary days to next year, but we catch the case when "yday"
236 year = (days + 2) * 400 / (365 * 400 + 100 - 4 + 1);
238 yday = days - (365 * year + year / 4 - year / 100 + year / 400);
241 leap = (year % 4 == 0) && (year % 100 || (year % 400 == 0));
242 yday = 365 + leap + yday;
247 * The empirical formula that maps "yday" to month.
248 * There are at least 10 variants, some of them are:
249 * mon = (yday + 31) * 15 / 459
250 * mon = (yday + 31) * 17 / 520
251 * mon = (yday + 31) * 20 / 612
254 mon = (yday + 31) * 10 / 306;
256 /* the Gauss' formula that evaluates days before the month */
258 mday = yday - (367 * mon / 12 - 30) + 1;
266 * there is no "yday" in Win32 SYSTEMTIME
276 * there is no "yday" in Win32 SYSTEMTIME
278 * yday += 31 + 28 + leap;
282 tp->ngx_tm_sec = (ngx_tm_sec_t) sec;
283 tp->ngx_tm_min = (ngx_tm_min_t) min;
284 tp->ngx_tm_hour = (ngx_tm_hour_t) hour;
285 tp->ngx_tm_mday = (ngx_tm_mday_t) mday;
286 tp->ngx_tm_mon = (ngx_tm_mon_t) mon;
287 tp->ngx_tm_year = (ngx_tm_year_t) year;
288 tp->ngx_tm_wday = (ngx_tm_wday_t) wday;
293 ngx_next_time(time_t when)
300 ngx_libc_localtime(now, &tm);
302 tm.tm_hour = (int) (when / 3600);
304 tm.tm_min = (int) (when / 60);
305 tm.tm_sec = (int) (when % 60);
313 if (next - now > 0) {
319 /* mktime() should normalize a date (Jan 32, etc) */