comparison src/core/ngx_times.c @ 635:e67b227c8dbb default tip

Merge with current.
author Maxim Dounin <mdounin@mdounin.ru>
date Mon, 25 Apr 2011 04:07:55 +0400
parents 8214eaef3530
children
comparison
equal deleted inserted replaced
578:f3a9e57d2e17 635:e67b227c8dbb
25 volatile ngx_msec_t ngx_current_msec; 25 volatile ngx_msec_t ngx_current_msec;
26 volatile ngx_time_t *ngx_cached_time; 26 volatile ngx_time_t *ngx_cached_time;
27 volatile ngx_str_t ngx_cached_err_log_time; 27 volatile ngx_str_t ngx_cached_err_log_time;
28 volatile ngx_str_t ngx_cached_http_time; 28 volatile ngx_str_t ngx_cached_http_time;
29 volatile ngx_str_t ngx_cached_http_log_time; 29 volatile ngx_str_t ngx_cached_http_log_time;
30 volatile ngx_str_t ngx_cached_http_log_iso8601;
31
32 #if !(NGX_WIN32)
33
34 /*
35 * locatime() and localtime_r() are not Async-Signal-Safe functions, therefore,
36 * they must not be called by a signal handler, so we use the cached
37 * GMT offset value. Fortunately the value is changed only two times a year.
38 */
39
40 static ngx_int_t cached_gmtoff;
41 #endif
30 42
31 static ngx_time_t cached_time[NGX_TIME_SLOTS]; 43 static ngx_time_t cached_time[NGX_TIME_SLOTS];
32 static u_char cached_err_log_time[NGX_TIME_SLOTS] 44 static u_char cached_err_log_time[NGX_TIME_SLOTS]
33 [sizeof("1970/09/28 12:00:00")]; 45 [sizeof("1970/09/28 12:00:00")];
34 static u_char cached_http_time[NGX_TIME_SLOTS] 46 static u_char cached_http_time[NGX_TIME_SLOTS]
35 [sizeof("Mon, 28 Sep 1970 06:00:00 GMT")]; 47 [sizeof("Mon, 28 Sep 1970 06:00:00 GMT")];
36 static u_char cached_http_log_time[NGX_TIME_SLOTS] 48 static u_char cached_http_log_time[NGX_TIME_SLOTS]
37 [sizeof("28/Sep/1970:12:00:00 +0600")]; 49 [sizeof("28/Sep/1970:12:00:00 +0600")];
50 static u_char cached_http_log_iso8601[NGX_TIME_SLOTS]
51 [sizeof("1970-09-28T12:00:00+06:00")];
38 52
39 53
40 static char *week[] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" }; 54 static char *week[] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
41 static char *months[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", 55 static char *months[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun",
42 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" }; 56 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
45 ngx_time_init(void) 59 ngx_time_init(void)
46 { 60 {
47 ngx_cached_err_log_time.len = sizeof("1970/09/28 12:00:00") - 1; 61 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; 62 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; 63 ngx_cached_http_log_time.len = sizeof("28/Sep/1970:12:00:00 +0600") - 1;
64 ngx_cached_http_log_iso8601.len = sizeof("1970-09-28T12:00:00+06:00") - 1;
50 65
51 ngx_cached_time = &cached_time[0]; 66 ngx_cached_time = &cached_time[0];
52 67
53 ngx_time_update(0, 0); 68 ngx_time_update();
54 } 69 }
55 70
56 71
57 void 72 void
58 ngx_time_update(time_t sec, ngx_uint_t msec) 73 ngx_time_update(void)
59 { 74 {
60 u_char *p0, *p1, *p2; 75 u_char *p0, *p1, *p2, *p3;
61 ngx_tm_t tm, gmt; 76 ngx_tm_t tm, gmt;
77 time_t sec;
78 ngx_uint_t msec;
62 ngx_time_t *tp; 79 ngx_time_t *tp;
63 struct timeval tv; 80 struct timeval tv;
64 81
65 if (!ngx_trylock(&ngx_time_lock)) { 82 if (!ngx_trylock(&ngx_time_lock)) {
66 return; 83 return;
67 } 84 }
68 85
69 if (sec == 0) { 86 ngx_gettimeofday(&tv);
70 ngx_gettimeofday(&tv); 87
71 88 sec = tv.tv_sec;
72 sec = tv.tv_sec; 89 msec = tv.tv_usec / 1000;
73 msec = tv.tv_usec / 1000;
74 }
75 90
76 ngx_current_msec = (ngx_msec_t) sec * 1000 + msec; 91 ngx_current_msec = (ngx_msec_t) sec * 1000 + msec;
77 92
78 tp = &cached_time[slot]; 93 tp = &cached_time[slot];
79 94
110 ngx_gmtime(sec + tp->gmtoff * 60, &tm); 125 ngx_gmtime(sec + tp->gmtoff * 60, &tm);
111 126
112 #elif (NGX_HAVE_GMTOFF) 127 #elif (NGX_HAVE_GMTOFF)
113 128
114 ngx_localtime(sec, &tm); 129 ngx_localtime(sec, &tm);
115 tp->gmtoff = (ngx_int_t) (tm.ngx_tm_gmtoff / 60); 130 cached_gmtoff = (ngx_int_t) (tm.ngx_tm_gmtoff / 60);
131 tp->gmtoff = cached_gmtoff;
116 132
117 #else 133 #else
118 134
119 ngx_localtime(sec, &tm); 135 ngx_localtime(sec, &tm);
120 tp->gmtoff = ngx_timezone(tm.ngx_tm_isdst); 136 cached_gmtoff = ngx_timezone(tm.ngx_tm_isdst);
137 tp->gmtoff = cached_gmtoff;
121 138
122 #endif 139 #endif
123 140
124 141
125 p1 = &cached_err_log_time[slot][0]; 142 p1 = &cached_err_log_time[slot][0];
137 tm.ngx_tm_year, tm.ngx_tm_hour, 154 tm.ngx_tm_year, tm.ngx_tm_hour,
138 tm.ngx_tm_min, tm.ngx_tm_sec, 155 tm.ngx_tm_min, tm.ngx_tm_sec,
139 tp->gmtoff < 0 ? '-' : '+', 156 tp->gmtoff < 0 ? '-' : '+',
140 ngx_abs(tp->gmtoff / 60), ngx_abs(tp->gmtoff % 60)); 157 ngx_abs(tp->gmtoff / 60), ngx_abs(tp->gmtoff % 60));
141 158
159 p3 = &cached_http_log_iso8601[slot][0];
160
161 (void) ngx_sprintf(p3, "%4d-%02d-%02dT%02d:%02d:%02d%c%02d:%02d",
162 tm.ngx_tm_year, tm.ngx_tm_mon,
163 tm.ngx_tm_mday, tm.ngx_tm_hour,
164 tm.ngx_tm_min, tm.ngx_tm_sec,
165 tp->gmtoff < 0 ? '-' : '+',
166 ngx_abs(tp->gmtoff / 60), ngx_abs(tp->gmtoff % 60));
167
142 168
143 ngx_memory_barrier(); 169 ngx_memory_barrier();
144 170
145 ngx_cached_time = tp; 171 ngx_cached_time = tp;
146 ngx_cached_http_time.data = p0; 172 ngx_cached_http_time.data = p0;
147 ngx_cached_err_log_time.data = p1; 173 ngx_cached_err_log_time.data = p1;
148 ngx_cached_http_log_time.data = p2; 174 ngx_cached_http_log_time.data = p2;
175 ngx_cached_http_log_iso8601.data = p3;
149 176
150 ngx_unlock(&ngx_time_lock); 177 ngx_unlock(&ngx_time_lock);
151 } 178 }
179
180
181 #if !(NGX_WIN32)
182
183 void
184 ngx_time_sigsafe_update(void)
185 {
186 u_char *p;
187 ngx_tm_t tm;
188 time_t sec;
189 ngx_time_t *tp;
190 struct timeval tv;
191
192 if (!ngx_trylock(&ngx_time_lock)) {
193 return;
194 }
195
196 ngx_gettimeofday(&tv);
197
198 sec = tv.tv_sec;
199
200 tp = &cached_time[slot];
201
202 if (tp->sec == sec) {
203 ngx_unlock(&ngx_time_lock);
204 return;
205 }
206
207 if (slot == NGX_TIME_SLOTS - 1) {
208 slot = 0;
209 } else {
210 slot++;
211 }
212
213 ngx_gmtime(sec + cached_gmtoff * 60, &tm);
214
215 p = &cached_err_log_time[slot][0];
216
217 (void) ngx_sprintf(p, "%4d/%02d/%02d %02d:%02d:%02d",
218 tm.ngx_tm_year, tm.ngx_tm_mon,
219 tm.ngx_tm_mday, tm.ngx_tm_hour,
220 tm.ngx_tm_min, tm.ngx_tm_sec);
221
222 ngx_memory_barrier();
223
224 ngx_cached_err_log_time.data = p;
225
226 ngx_unlock(&ngx_time_lock);
227 }
228
229 #endif
152 230
153 231
154 u_char * 232 u_char *
155 ngx_http_time(u_char *buf, time_t t) 233 ngx_http_time(u_char *buf, time_t t)
156 { 234 {