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
;
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
;
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" };
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];
78 if (!ngx_trylock(&ngx_time_lock
)) {
82 ngx_gettimeofday(&tv
);
85 msec
= tv
.tv_usec
/ 1000;
87 ngx_current_msec
= (ngx_msec_t
) sec
* 1000 + msec
;
89 tp
= &cached_time
[slot
];
93 ngx_unlock(&ngx_time_lock
);
97 if (slot
== NGX_TIME_SLOTS
- 1) {
103 tp
= &cached_time
[slot
];
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
;
131 ngx_localtime(sec
, &tm
);
132 cached_gmtoff
= ngx_timezone(tm
.ngx_tm_isdst
);
133 tp
->gmtoff
= cached_gmtoff
;
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
);
170 ngx_time_sigsafe_update(void)
178 if (!ngx_trylock(&ngx_time_lock
)) {
182 ngx_gettimeofday(&tv
);
186 tp
= &cached_time
[slot
];
188 if (tp
->sec
== sec
) {
189 ngx_unlock(&ngx_time_lock
);
193 if (slot
== NGX_TIME_SLOTS
- 1) {
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
);
219 ngx_http_time(u_char
*buf
, time_t t
)
225 return ngx_sprintf(buf
, "%s, %02d %s %4d %02d:%02d:%02d GMT",
226 week
[tm
.ngx_tm_wday
],
228 months
[tm
.ngx_tm_mon
- 1],
237 ngx_http_cookie_time(u_char
*buf
, time_t t
)
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
],
254 months
[tm
.ngx_tm_mon
- 1],
255 (tm
.ngx_tm_year
> 2037) ? tm
.ngx_tm_year
:
256 tm
.ngx_tm_year
% 100,
264 ngx_gmtime(time_t t
, ngx_tm_t
*tp
)
267 ngx_uint_t n
, sec
, min
, hour
, mday
, mon
, year
, wday
, days
, leap
;
269 /* the calculation is valid for positive time_t only */
275 /* Jaunary 1, 1970 was Thursday */
277 wday
= (4 + days
) % 7;
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"
300 year
= (days
+ 2) * 400 / (365 * 400 + 100 - 4 + 1);
302 yday
= days
- (365 * year
+ year
/ 4 - year
/ 100 + year
/ 400);
305 leap
= (year
% 4 == 0) && (year
% 100 || (year
% 400 == 0));
306 yday
= 365 + leap
+ yday
;
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;
330 * there is no "yday" in Win32 SYSTEMTIME
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
;
357 ngx_next_time(time_t when
)
364 ngx_libc_localtime(now
, &tm
);
366 tm
.tm_hour
= (int) (when
/ 3600);
368 tm
.tm_min
= (int) (when
/ 60);
369 tm
.tm_sec
= (int) (when
% 60);
377 if (next
- now
> 0) {
383 /* mktime() should normalize a date (Jan 32, etc) */