comparison src/core/ngx_times.c @ 0:f0b350454894 NGINX_0_1_0

nginx 0.1.0 *) The first public version.
author Igor Sysoev <http://sysoev.ru>
date Mon, 04 Oct 2004 00:00:00 +0400
parents
children 4b2dafa26fe2
comparison
equal deleted inserted replaced
-1:000000000000 0:f0b350454894
1
2 /*
3 * Copyright (C) Igor Sysoev
4 */
5
6
7 #include <ngx_config.h>
8 #include <ngx_core.h>
9
10
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;
14
15 static ngx_tm_t ngx_cached_gmtime;
16 static ngx_int_t ngx_gmtoff;
17
18
19 /*
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.
27 */
28
29 #if (NGX_THREADS)
30
31 #define NGX_TIME_SLOTS 60
32 static ngx_uint_t slot = NGX_TIME_SLOTS;
33
34 static ngx_mutex_t *ngx_time_mutex;
35
36 #else
37
38 #define NGX_TIME_SLOTS 1
39 #define slot 0
40
41 #endif
42
43
44 #if (NGX_THREADS && (TIME_T_SIZE > SIG_ATOMIC_T_SIZE))
45
46 volatile time_t *ngx_cached_time;
47 static time_t cached_time[NGX_TIME_SLOTS];
48
49 #else
50
51 volatile time_t ngx_cached_time;
52
53 #endif
54
55
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;
59
60
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")];
67
68
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" };
72
73
74 void ngx_time_init()
75 {
76 struct timeval tv;
77
78 ngx_memzero(&ngx_cached_gmtime, sizeof(ngx_tm_t));
79 #ifdef ngx_tm_zone
80 ngx_cached_gmtime.ngx_tm_zone = "GMT";
81 #endif
82
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;
86
87 #if (NGX_THREADS && (TIME_T_SIZE > SIG_ATOMIC_T_SIZE))
88 ngx_cached_time = &cached_time[0];
89 #endif
90
91 ngx_gettimeofday(&tv);
92
93 ngx_start_msec = (ngx_epoch_msec_t) tv.tv_sec * 1000 + tv.tv_usec / 1000;
94 ngx_old_elapsed_msec = 0;
95 ngx_elapsed_msec = 0;
96
97 #if !(WIN32)
98 tzset();
99 #endif
100
101 ngx_time_update(tv.tv_sec);
102 }
103
104
105 #if (NGX_THREADS)
106
107 ngx_int_t ngx_time_mutex_init(ngx_log_t *log)
108 {
109 if (!(ngx_time_mutex = ngx_mutex_init(log, NGX_MUTEX_LIGHT))) {
110 return NGX_ERROR;
111 }
112
113 return NGX_OK;
114 }
115
116 #endif
117
118
119 void ngx_time_update(time_t s)
120 {
121 u_char *p;
122 ngx_tm_t tm;
123
124 if (ngx_time() == s) {
125 return;
126 }
127
128 #if (NGX_THREADS)
129
130 if (ngx_mutex_trylock(ngx_time_mutex) != NGX_OK) {
131 return;
132 }
133
134 if (slot == NGX_TIME_SLOTS) {
135 slot = 0;
136 } else {
137 slot++;
138 }
139
140 #if (NGX_THREADS && (TIME_T_SIZE > SIG_ATOMIC_T_SIZE))
141 ngx_cached_time = &cached_time[slot];
142 #endif
143
144 #endif
145
146 ngx_time() = s;
147
148 ngx_gmtime(s, &ngx_cached_gmtime);
149
150
151 p = cached_http_time[slot];
152
153 ngx_snprintf((char *) p, sizeof("Mon, 28 Sep 1970 06:00:00 GMT"),
154 "%s, %02d %s %4d %02d:%02d:%02d GMT",
155 week[ngx_cached_gmtime.ngx_tm_wday],
156 ngx_cached_gmtime.ngx_tm_mday,
157 months[ngx_cached_gmtime.ngx_tm_mon - 1],
158 ngx_cached_gmtime.ngx_tm_year,
159 ngx_cached_gmtime.ngx_tm_hour,
160 ngx_cached_gmtime.ngx_tm_min,
161 ngx_cached_gmtime.ngx_tm_sec);
162
163 ngx_cached_http_time.data = p;
164
165
166 #if (HAVE_GETTIMEZONE)
167
168 ngx_gmtoff = ngx_gettimezone();
169 ngx_gmtime(s + ngx_gmtoff * 60, &tm);
170
171 #elif (HAVE_GMTOFF)
172
173 ngx_localtime(&tm);
174 ngx_gmtoff = tm.ngx_tm_gmtoff / 60;
175
176 #else
177
178 ngx_localtime(&tm);
179 ngx_gmtoff = ngx_timezone(tm.ngx_tm_isdst);
180
181 #endif
182
183
184 p = cached_err_log_time[slot];
185
186 ngx_snprintf((char *) p, sizeof("1970/09/28 12:00:00"),
187 "%4d/%02d/%02d %02d:%02d:%02d",
188 tm.ngx_tm_year, tm.ngx_tm_mon,
189 tm.ngx_tm_mday, tm.ngx_tm_hour,
190 tm.ngx_tm_min, tm.ngx_tm_sec);
191
192 ngx_cached_err_log_time.data = p;
193
194
195 p = cached_http_log_time[slot];
196
197 ngx_snprintf((char *) p, sizeof("28/Sep/1970:12:00:00 +0600"),
198 "%02d/%s/%d:%02d:%02d:%02d %c%02d%02d",
199 tm.ngx_tm_mday, months[tm.ngx_tm_mon - 1],
200 tm.ngx_tm_year, tm.ngx_tm_hour,
201 tm.ngx_tm_min, tm.ngx_tm_sec,
202 ngx_gmtoff < 0 ? '-' : '+',
203 abs(ngx_gmtoff / 60), abs(ngx_gmtoff % 60));
204
205 ngx_cached_http_log_time.data = p;
206
207
208 #if (NGX_THREADS)
209 ngx_mutex_unlock(ngx_time_mutex);
210 #endif
211
212 }
213
214
215 size_t ngx_http_time(u_char *buf, time_t t)
216 {
217 ngx_tm_t tm;
218
219 ngx_gmtime(t, &tm);
220
221 return ngx_snprintf((char *) buf, sizeof("Mon, 28 Sep 1970 06:00:00 GMT"),
222 "%s, %02d %s %4d %02d:%02d:%02d GMT",
223 week[tm.ngx_tm_wday],
224 tm.ngx_tm_mday,
225 months[tm.ngx_tm_mon - 1],
226 tm.ngx_tm_year,
227 tm.ngx_tm_hour,
228 tm.ngx_tm_min,
229 tm.ngx_tm_sec);
230 }
231
232
233 size_t ngx_http_cookie_time(u_char *buf, time_t t)
234 {
235 ngx_tm_t tm;
236
237 ngx_gmtime(t, &tm);
238
239 /*
240 * Netscape 3.x does not understand 4-digit years at all and
241 * 2-digit years more than "37"
242 */
243
244 if (tm.ngx_tm_year > 2037) {
245 return ngx_snprintf((char *) buf,
246 sizeof("Mon, 28-Sep-1970 06:00:00 GMT"),
247 "%s, %02d-%s-%d %02d:%02d:%02d GMT",
248 week[tm.ngx_tm_wday],
249 tm.ngx_tm_mday,
250 months[tm.ngx_tm_mon - 1],
251 tm.ngx_tm_year,
252 tm.ngx_tm_hour,
253 tm.ngx_tm_min,
254 tm.ngx_tm_sec);
255 } else {
256 return ngx_snprintf((char *) buf,
257 sizeof("Mon, 28-Sep-70 06:00:00 GMT"),
258 "%s, %02d-%s-%02d %02d:%02d:%02d GMT",
259 week[tm.ngx_tm_wday],
260 tm.ngx_tm_mday,
261 months[tm.ngx_tm_mon - 1],
262 tm.ngx_tm_year % 100,
263 tm.ngx_tm_hour,
264 tm.ngx_tm_min,
265 tm.ngx_tm_sec);
266 }
267 }
268
269
270 void ngx_gmtime(time_t t, ngx_tm_t *tp)
271 {
272 ngx_int_t sec, min, hour, mday, mon, year, wday, yday, days;
273
274 days = t / 86400;
275
276 /* Jaunary 1, 1970 was Thursday */
277 wday = (4 + days) % 7;
278
279 t %= 86400;
280 hour = t / 3600;
281 t %= 3600;
282 min = t / 60;
283 sec = t % 60;
284
285 /* the algorithm based on Gauss's formula */
286
287 days = days - (31 + 28) + 719527;
288
289 year = days * 400 / (365 * 400 + 100 - 4 + 1);
290 yday = days - (365 * year + year / 4 - year / 100 + year / 400);
291
292 mon = (yday + 31) * 12 / 367;
293 mday = yday - (mon * 367 / 12 - 31);
294
295 mon += 2;
296
297 if (yday >= 306) {
298
299 /*
300 * there is no "yday" in Win32 SYSTEMTIME
301 *
302 * yday -= 306;
303 */
304
305 year++;
306 mon -= 12;
307
308 if (mday == 0) {
309 /* Jaunary 31 */
310 mon = 1;
311 mday = 31;
312
313 } else if (mon == 2) {
314
315 if ((year % 4 == 0) && (year % 100 || (year % 400 == 0))) {
316 if (mday > 29) {
317 mon = 3;
318 mday -= 29;
319 }
320
321 } else if (mday > 28) {
322 mon = 3;
323 mday -= 28;
324 }
325 }
326
327 /*
328 * there is no "yday" in Win32 SYSTEMTIME
329 *
330 * } else {
331 * yday += 31 + 28;
332 *
333 * if ((year % 4 == 0) && (year % 100 || (year % 400 == 0))) {
334 * yday++;
335 * }
336 */
337 }
338
339 tp->ngx_tm_sec = (ngx_tm_sec_t) sec;
340 tp->ngx_tm_min = (ngx_tm_min_t) min;
341 tp->ngx_tm_hour = (ngx_tm_hour_t) hour;
342 tp->ngx_tm_mday = (ngx_tm_mday_t) mday;
343 tp->ngx_tm_mon = (ngx_tm_mon_t) mon;
344 tp->ngx_tm_year = (ngx_tm_year_t) year;
345 tp->ngx_tm_wday = (ngx_tm_wday_t) wday;
346 }