nginx 0.1.19
[nginx-catap.git] / src / core / ngx_times.c
blob93542326fca9da83bbc3c0373cec72089de04658
2 /*
3 * Copyright (C) Igor Sysoev
4 */
7 #include <ngx_config.h>
8 #include <ngx_core.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.
29 #if (NGX_THREADS)
31 #define NGX_TIME_SLOTS 60
32 static ngx_uint_t slot = NGX_TIME_SLOTS;
34 static ngx_mutex_t *ngx_time_mutex;
36 #else
38 #define NGX_TIME_SLOTS 1
39 #define slot 0
41 #endif
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];
49 #else
51 volatile time_t ngx_cached_time;
53 #endif
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" };
74 void ngx_time_init()
76 struct timeval tv;
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
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];
89 #endif
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;
95 ngx_elapsed_msec = 0;
97 #if !(NGX_WIN32)
98 tzset();
99 #endif
101 ngx_time_update(tv.tv_sec);
105 #if (NGX_THREADS)
107 ngx_int_t ngx_time_mutex_init(ngx_log_t *log)
109 if (!(ngx_time_mutex = ngx_mutex_init(log, NGX_MUTEX_LIGHT))) {
110 return NGX_ERROR;
113 return NGX_OK;
116 #endif
119 void ngx_time_update(time_t s)
121 u_char *p;
122 ngx_tm_t tm;
124 if (ngx_time() == s) {
125 return;
128 #if (NGX_THREADS)
130 if (ngx_mutex_trylock(ngx_time_mutex) != NGX_OK) {
131 return;
134 if (slot == NGX_TIME_SLOTS) {
135 slot = 0;
136 } else {
137 slot++;
140 #if (NGX_THREADS && (NGX_TIME_T_SIZE > NGX_SIG_ATOMIC_T_SIZE))
141 ngx_cached_time = &cached_time[slot];
142 #endif
144 #endif
146 ngx_time() = s;
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)
172 ngx_localtime(&tm);
173 ngx_gmtoff = tm.ngx_tm_gmtoff / 60;
175 #else
177 ngx_localtime(&tm);
178 ngx_gmtoff = ngx_timezone(tm.ngx_tm_isdst);
180 #endif
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;
205 #if (NGX_THREADS)
206 ngx_mutex_unlock(ngx_time_mutex);
207 #endif
212 u_char *ngx_http_time(u_char *buf, time_t t)
214 ngx_tm_t tm;
216 ngx_gmtime(t, &tm);
218 return ngx_sprintf(buf, "%s, %02d %s %4d %02d:%02d:%02d GMT",
219 week[tm.ngx_tm_wday],
220 tm.ngx_tm_mday,
221 months[tm.ngx_tm_mon - 1],
222 tm.ngx_tm_year,
223 tm.ngx_tm_hour,
224 tm.ngx_tm_min,
225 tm.ngx_tm_sec);
229 u_char *ngx_http_cookie_time(u_char *buf, time_t t)
231 ngx_tm_t tm;
233 ngx_gmtime(t, &tm);
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],
245 tm.ngx_tm_mday,
246 months[tm.ngx_tm_mon - 1],
247 (tm.ngx_tm_year > 2037) ? tm.ngx_tm_year:
248 tm.ngx_tm_year % 100,
249 tm.ngx_tm_hour,
250 tm.ngx_tm_min,
251 tm.ngx_tm_sec);
255 void ngx_gmtime(time_t t, ngx_tm_t *tp)
257 ngx_int_t sec, min, hour, mday, mon, year, wday, yday, days;
259 days = t / 86400;
261 /* Jaunary 1, 1970 was Thursday */
262 wday = (4 + days) % 7;
264 t %= 86400;
265 hour = t / 3600;
266 t %= 3600;
267 min = t / 60;
268 sec = t % 60;
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);
280 mon += 2;
282 if (yday >= 306) {
285 * there is no "yday" in Win32 SYSTEMTIME
287 * yday -= 306;
290 year++;
291 mon -= 12;
293 if (mday == 0) {
294 /* Jaunary 31 */
295 mon = 1;
296 mday = 31;
298 } else if (mon == 2) {
300 if ((year % 4 == 0) && (year % 100 || (year % 400 == 0))) {
301 if (mday > 29) {
302 mon = 3;
303 mday -= 29;
306 } else if (mday > 28) {
307 mon = 3;
308 mday -= 28;
312 * there is no "yday" in Win32 SYSTEMTIME
314 * } else {
315 * yday += 31 + 28;
317 * if ((year % 4 == 0) && (year % 100 || (year % 400 == 0))) {
318 * yday++;
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;