3 * Copyright (C) Igor Sysoev
7 #include <ngx_config.h>
11 ngx_epoch_msec_t ngx_elapsed_msec
;
12 ngx_epoch_msec_t ngx_old_elapsed_msec
;
13 ngx_epoch_msec_t ngx_start_msec
;
15 static ngx_tm_t ngx_cached_gmtime
;
16 static ngx_int_t ngx_gmtoff
;
20 * In the threaded mode only one thread updates the cached time and strings
21 * and these operations are protected by the mutex. The reading of the cached
22 * time and strings is not protected by the mutex. To avoid the race
23 * conditions for non-atomic values we use the NGX_TIME_SLOTS slots to store
24 * time value and strings. Thus thread may get the corrupted values only
25 * if it is preempted while copying and then it is not scheduled to run
26 * more than NGX_TIME_SLOTS seconds.
31 #define NGX_TIME_SLOTS 60
32 static ngx_uint_t slot
= NGX_TIME_SLOTS
;
34 static ngx_mutex_t
*ngx_time_mutex
;
38 #define NGX_TIME_SLOTS 1
44 #if (NGX_THREADS && (NGX_TIME_T_SIZE > NGX_SIG_ATOMIC_T_SIZE))
46 volatile time_t *ngx_cached_time
;
47 static time_t cached_time
[NGX_TIME_SLOTS
];
51 volatile time_t ngx_cached_time
;
56 ngx_thread_volatile ngx_str_t ngx_cached_err_log_time
;
57 ngx_thread_volatile ngx_str_t ngx_cached_http_time
;
58 ngx_thread_volatile ngx_str_t ngx_cached_http_log_time
;
61 static u_char cached_err_log_time
[NGX_TIME_SLOTS
]
62 [sizeof("1970/09/28 12:00:00")];
63 static u_char cached_http_time
[NGX_TIME_SLOTS
]
64 [sizeof("Mon, 28 Sep 1970 06:00:00 GMT")];
65 static u_char cached_http_log_time
[NGX_TIME_SLOTS
]
66 [sizeof("28/Sep/1970:12:00:00 +0600")];
69 static char *week
[] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
70 static char *months
[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun",
71 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
78 ngx_memzero(&ngx_cached_gmtime
, sizeof(ngx_tm_t
));
80 ngx_cached_gmtime
.ngx_tm_zone
= "GMT";
83 ngx_cached_err_log_time
.len
= sizeof("1970/09/28 12:00:00") - 1;
84 ngx_cached_http_time
.len
= sizeof("Mon, 28 Sep 1970 06:00:00 GMT") - 1;
85 ngx_cached_http_log_time
.len
= sizeof("28/Sep/1970:12:00:00 +0600") - 1;
87 #if (NGX_THREADS && (NGX_TIME_T_SIZE > NGX_SIG_ATOMIC_T_SIZE))
88 ngx_cached_time
= &cached_time
[0];
91 ngx_gettimeofday(&tv
);
93 ngx_start_msec
= (ngx_epoch_msec_t
) tv
.tv_sec
* 1000 + tv
.tv_usec
/ 1000;
94 ngx_old_elapsed_msec
= 0;
101 ngx_time_update(tv
.tv_sec
);
107 ngx_int_t
ngx_time_mutex_init(ngx_log_t
*log
)
109 if (!(ngx_time_mutex
= ngx_mutex_init(log
, NGX_MUTEX_LIGHT
))) {
119 void ngx_time_update(time_t s
)
124 if (ngx_time() == s
) {
130 if (ngx_mutex_trylock(ngx_time_mutex
) != NGX_OK
) {
134 if (slot
== NGX_TIME_SLOTS
) {
140 #if (NGX_THREADS && (NGX_TIME_T_SIZE > NGX_SIG_ATOMIC_T_SIZE))
141 ngx_cached_time
= &cached_time
[slot
];
148 ngx_gmtime(s
, &ngx_cached_gmtime
);
151 p
= cached_http_time
[slot
];
153 ngx_sprintf(p
, "%s, %02d %s %4d %02d:%02d:%02d GMT",
154 week
[ngx_cached_gmtime
.ngx_tm_wday
],
155 ngx_cached_gmtime
.ngx_tm_mday
,
156 months
[ngx_cached_gmtime
.ngx_tm_mon
- 1],
157 ngx_cached_gmtime
.ngx_tm_year
,
158 ngx_cached_gmtime
.ngx_tm_hour
,
159 ngx_cached_gmtime
.ngx_tm_min
,
160 ngx_cached_gmtime
.ngx_tm_sec
);
162 ngx_cached_http_time
.data
= p
;
165 #if (NGX_HAVE_GETTIMEZONE)
167 ngx_gmtoff
= ngx_gettimezone();
168 ngx_gmtime(s
+ ngx_gmtoff
* 60, &tm
);
170 #elif (NGX_HAVE_GMTOFF)
173 ngx_gmtoff
= tm
.ngx_tm_gmtoff
/ 60;
178 ngx_gmtoff
= ngx_timezone(tm
.ngx_tm_isdst
);
183 p
= cached_err_log_time
[slot
];
185 ngx_sprintf(p
, "%4d/%02d/%02d %02d:%02d:%02d",
186 tm
.ngx_tm_year
, tm
.ngx_tm_mon
,
187 tm
.ngx_tm_mday
, tm
.ngx_tm_hour
,
188 tm
.ngx_tm_min
, tm
.ngx_tm_sec
);
190 ngx_cached_err_log_time
.data
= p
;
193 p
= cached_http_log_time
[slot
];
195 ngx_sprintf(p
, "%02d/%s/%d:%02d:%02d:%02d %c%02d%02d",
196 tm
.ngx_tm_mday
, months
[tm
.ngx_tm_mon
- 1],
197 tm
.ngx_tm_year
, tm
.ngx_tm_hour
,
198 tm
.ngx_tm_min
, tm
.ngx_tm_sec
,
199 ngx_gmtoff
< 0 ? '-' : '+',
200 abs(ngx_gmtoff
/ 60), abs(ngx_gmtoff
% 60));
202 ngx_cached_http_log_time
.data
= p
;
206 ngx_mutex_unlock(ngx_time_mutex
);
212 u_char
*ngx_http_time(u_char
*buf
, time_t t
)
218 return ngx_sprintf(buf
, "%s, %02d %s %4d %02d:%02d:%02d GMT",
219 week
[tm
.ngx_tm_wday
],
221 months
[tm
.ngx_tm_mon
- 1],
229 u_char
*ngx_http_cookie_time(u_char
*buf
, time_t t
)
236 * Netscape 3.x does not understand 4-digit years at all and
237 * 2-digit years more than "37"
240 return ngx_sprintf(buf
,
241 (tm
.ngx_tm_year
> 2037) ?
242 "%s, %02d-%s-%d %02d:%02d:%02d GMT":
243 "%s, %02d-%s-%02d %02d:%02d:%02d GMT",
244 week
[tm
.ngx_tm_wday
],
246 months
[tm
.ngx_tm_mon
- 1],
247 (tm
.ngx_tm_year
> 2037) ? tm
.ngx_tm_year
:
248 tm
.ngx_tm_year
% 100,
255 void ngx_gmtime(time_t t
, ngx_tm_t
*tp
)
257 ngx_int_t sec
, min
, hour
, mday
, mon
, year
, wday
, yday
, days
;
261 /* Jaunary 1, 1970 was Thursday */
262 wday
= (4 + days
) % 7;
270 /* the algorithm based on Gauss's formula */
272 days
= days
- (31 + 28) + 719527;
274 year
= days
* 400 / (365 * 400 + 100 - 4 + 1);
275 yday
= days
- (365 * year
+ year
/ 4 - year
/ 100 + year
/ 400);
277 mon
= (yday
+ 31) * 12 / 367;
278 mday
= yday
- (mon
* 367 / 12 - 31);
285 * there is no "yday" in Win32 SYSTEMTIME
298 } else if (mon
== 2) {
300 if ((year
% 4 == 0) && (year
% 100 || (year
% 400 == 0))) {
306 } else if (mday
> 28) {
312 * there is no "yday" in Win32 SYSTEMTIME
317 * if ((year % 4 == 0) && (year % 100 || (year % 400 == 0))) {
323 tp
->ngx_tm_sec
= (ngx_tm_sec_t
) sec
;
324 tp
->ngx_tm_min
= (ngx_tm_min_t
) min
;
325 tp
->ngx_tm_hour
= (ngx_tm_hour_t
) hour
;
326 tp
->ngx_tm_mday
= (ngx_tm_mday_t
) mday
;
327 tp
->ngx_tm_mon
= (ngx_tm_mon_t
) mon
;
328 tp
->ngx_tm_year
= (ngx_tm_year_t
) year
;
329 tp
->ngx_tm_wday
= (ngx_tm_wday_t
) wday
;