blob: 551e10258aa014ee12a5ae50158d5313abf35d66 [file] [log] [blame]
Igor Sysoevd59a0472003-11-10 21:09:22 +00001
Igor Sysoevd90282d2004-09-28 08:34:51 +00002/*
Igor Sysoevff8da912004-09-29 16:00:49 +00003 * Copyright (C) Igor Sysoev
Igor Sysoevd90282d2004-09-28 08:34:51 +00004 */
5
6
Igor Sysoevd59a0472003-11-10 21:09:22 +00007#include <ngx_config.h>
8#include <ngx_core.h>
9
10
Igor Sysoevc0247302004-06-27 18:01:57 +000011/*
Igor Sysoevc2068d02005-10-19 12:33:58 +000012 * 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.
Igor Sysoevc0247302004-06-27 18:01:57 +000018 */
19
Igor Sysoevc2068d02005-10-19 12:33:58 +000020#define NGX_TIME_SLOTS 64
Igor Sysoevc0247302004-06-27 18:01:57 +000021
Igor Sysoevf4b34c42006-12-19 12:38:20 +000022static ngx_uint_t slot;
Igor Sysoevc2068d02005-10-19 12:33:58 +000023static ngx_atomic_t ngx_time_lock;
Igor Sysoevc0247302004-06-27 18:01:57 +000024
Igor Sysoevc2068d02005-10-19 12:33:58 +000025volatile ngx_msec_t ngx_current_msec;
26volatile ngx_time_t *ngx_cached_time;
27volatile ngx_str_t ngx_cached_err_log_time;
28volatile ngx_str_t ngx_cached_http_time;
29volatile ngx_str_t ngx_cached_http_log_time;
Igor Sysoevc0247302004-06-27 18:01:57 +000030
Igor Sysoevc2068d02005-10-19 12:33:58 +000031static ngx_time_t cached_time[NGX_TIME_SLOTS];
32static u_char cached_err_log_time[NGX_TIME_SLOTS]
33 [sizeof("1970/09/28 12:00:00")];
34static u_char cached_http_time[NGX_TIME_SLOTS]
35 [sizeof("Mon, 28 Sep 1970 06:00:00 GMT")];
36static u_char cached_http_log_time[NGX_TIME_SLOTS]
37 [sizeof("28/Sep/1970:12:00:00 +0600")];
Igor Sysoevd59a0472003-11-10 21:09:22 +000038
39
Igor Sysoev3f4685f2004-04-25 20:13:21 +000040static char *week[] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
Igor Sysoev562e53e2003-11-13 06:14:05 +000041static char *months[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun",
42 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
43
Igor Sysoevc1571722005-03-19 12:38:37 +000044void
45ngx_time_init(void)
Igor Sysoev562e53e2003-11-13 06:14:05 +000046{
Igor Sysoevc0247302004-06-27 18:01:57 +000047 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;
49 ngx_cached_http_log_time.len = sizeof("28/Sep/1970:12:00:00 +0600") - 1;
Igor Sysoev732a2712004-04-21 18:54:33 +000050
Igor Sysoevc0247302004-06-27 18:01:57 +000051 ngx_cached_time = &cached_time[0];
Igor Sysoevf5003d82003-12-04 14:53:00 +000052
Igor Sysoev1b735832004-11-11 14:07:14 +000053#if !(NGX_WIN32)
Igor Sysoev732a2712004-04-21 18:54:33 +000054 tzset();
55#endif
56
Igor Sysoevc2068d02005-10-19 12:33:58 +000057 ngx_time_update(0, 0);
Igor Sysoev562e53e2003-11-13 06:14:05 +000058}
59
60
Igor Sysoevc1571722005-03-19 12:38:37 +000061void
Igor Sysoevc2068d02005-10-19 12:33:58 +000062ngx_time_update(time_t sec, ngx_uint_t msec)
Igor Sysoevd59a0472003-11-10 21:09:22 +000063{
Igor Sysoevc2068d02005-10-19 12:33:58 +000064 u_char *p0, *p1, *p2;
65 ngx_tm_t tm, gmt;
66 ngx_time_t *tp;
67 struct timeval tv;
Igor Sysoevd59a0472003-11-10 21:09:22 +000068
Igor Sysoevc2068d02005-10-19 12:33:58 +000069 if (!ngx_trylock(&ngx_time_lock)) {
Igor Sysoev898446c2004-02-26 17:10:01 +000070 return;
Igor Sysoevb54698b2004-02-23 20:57:12 +000071 }
Igor Sysoevc0247302004-06-27 18:01:57 +000072
Igor Sysoevc2068d02005-10-19 12:33:58 +000073 if (sec == 0) {
74 ngx_gettimeofday(&tv);
Igor Sysoevb54698b2004-02-23 20:57:12 +000075
Igor Sysoevc2068d02005-10-19 12:33:58 +000076 sec = tv.tv_sec;
77 msec = tv.tv_usec / 1000;
78 }
Igor Sysoev3c3ca172004-01-05 20:55:48 +000079
Igor Sysoevc2068d02005-10-19 12:33:58 +000080 ngx_current_msec = (ngx_msec_t) sec * 1000 + msec;
Igor Sysoevc0247302004-06-27 18:01:57 +000081
Igor Sysoevc2068d02005-10-19 12:33:58 +000082 tp = &cached_time[slot];
83
Igor Sysoevc2068d02005-10-19 12:33:58 +000084 if (tp->sec == sec) {
Igor Sysoevc3d106a2006-12-06 14:25:20 +000085 tp->msec = msec;
Igor Sysoevc2068d02005-10-19 12:33:58 +000086 ngx_unlock(&ngx_time_lock);
87 return;
88 }
89
Igor Sysoevf51d4fe2007-01-29 11:52:25 +000090 if (slot == NGX_TIME_SLOTS - 1) {
Igor Sysoevc3d106a2006-12-06 14:25:20 +000091 slot = 0;
92 } else {
93 slot++;
94 }
95
96 tp = &cached_time[slot];
97
Igor Sysoevc2068d02005-10-19 12:33:58 +000098 tp->sec = sec;
Igor Sysoevc3d106a2006-12-06 14:25:20 +000099 tp->msec = msec;
Igor Sysoevc2068d02005-10-19 12:33:58 +0000100
101 ngx_gmtime(sec, &gmt);
Igor Sysoevd59a0472003-11-10 21:09:22 +0000102
Igor Sysoev732a2712004-04-21 18:54:33 +0000103
Igor Sysoev80a29012007-01-25 22:01:23 +0000104 p0 = &cached_http_time[slot][0];
Igor Sysoev732a2712004-04-21 18:54:33 +0000105
Igor Sysoevc2068d02005-10-19 12:33:58 +0000106 (void) ngx_sprintf(p0, "%s, %02d %s %4d %02d:%02d:%02d GMT",
107 week[gmt.ngx_tm_wday], gmt.ngx_tm_mday,
108 months[gmt.ngx_tm_mon - 1], gmt.ngx_tm_year,
109 gmt.ngx_tm_hour, gmt.ngx_tm_min, gmt.ngx_tm_sec);
Igor Sysoev732a2712004-04-21 18:54:33 +0000110
Igor Sysoevf6906042004-11-25 16:17:31 +0000111#if (NGX_HAVE_GETTIMEZONE)
Igor Sysoev732a2712004-04-21 18:54:33 +0000112
Igor Sysoevc2068d02005-10-19 12:33:58 +0000113 tp->gmtoff = ngx_gettimezone();
114 ngx_gmtime(sec + tp->gmtoff * 60, &tm);
Igor Sysoev732a2712004-04-21 18:54:33 +0000115
Igor Sysoevf6906042004-11-25 16:17:31 +0000116#elif (NGX_HAVE_GMTOFF)
Igor Sysoev415b1ce2004-06-17 17:18:53 +0000117
Igor Sysoevc2068d02005-10-19 12:33:58 +0000118 ngx_localtime(sec, &tm);
119 tp->gmtoff = (ngx_int_t) (tm.ngx_tm_gmtoff / 60);
Igor Sysoev415b1ce2004-06-17 17:18:53 +0000120
Igor Sysoev732a2712004-04-21 18:54:33 +0000121#else
Igor Sysoevd59a0472003-11-10 21:09:22 +0000122
Igor Sysoevc2068d02005-10-19 12:33:58 +0000123 ngx_localtime(sec, &tm);
124 tp->gmtoff = ngx_timezone(tm.ngx_tm_isdst);
Igor Sysoevd59a0472003-11-10 21:09:22 +0000125
Igor Sysoev732a2712004-04-21 18:54:33 +0000126#endif
Igor Sysoev562e53e2003-11-13 06:14:05 +0000127
Igor Sysoev732a2712004-04-21 18:54:33 +0000128
Igor Sysoev80a29012007-01-25 22:01:23 +0000129 p1 = &cached_err_log_time[slot][0];
Igor Sysoev732a2712004-04-21 18:54:33 +0000130
Igor Sysoevc2068d02005-10-19 12:33:58 +0000131 (void) ngx_sprintf(p1, "%4d/%02d/%02d %02d:%02d:%02d",
Igor Sysoev4959ec42005-05-23 12:07:45 +0000132 tm.ngx_tm_year, tm.ngx_tm_mon,
133 tm.ngx_tm_mday, tm.ngx_tm_hour,
134 tm.ngx_tm_min, tm.ngx_tm_sec);
Igor Sysoev732a2712004-04-21 18:54:33 +0000135
Igor Sysoev732a2712004-04-21 18:54:33 +0000136
Igor Sysoev80a29012007-01-25 22:01:23 +0000137 p2 = &cached_http_log_time[slot][0];
Igor Sysoev732a2712004-04-21 18:54:33 +0000138
Igor Sysoevc2068d02005-10-19 12:33:58 +0000139 (void) ngx_sprintf(p2, "%02d/%s/%d:%02d:%02d:%02d %c%02d%02d",
Igor Sysoev4959ec42005-05-23 12:07:45 +0000140 tm.ngx_tm_mday, months[tm.ngx_tm_mon - 1],
141 tm.ngx_tm_year, tm.ngx_tm_hour,
142 tm.ngx_tm_min, tm.ngx_tm_sec,
Igor Sysoevc2068d02005-10-19 12:33:58 +0000143 tp->gmtoff < 0 ? '-' : '+',
144 ngx_abs(tp->gmtoff / 60), ngx_abs(tp->gmtoff % 60));
Igor Sysoev732a2712004-04-21 18:54:33 +0000145
Igor Sysoevb54698b2004-02-23 20:57:12 +0000146
Igor Sysoevc2068d02005-10-19 12:33:58 +0000147 ngx_memory_barrier();
Igor Sysoevb54698b2004-02-23 20:57:12 +0000148
Igor Sysoevc2068d02005-10-19 12:33:58 +0000149 ngx_cached_time = tp;
150 ngx_cached_http_time.data = p0;
151 ngx_cached_err_log_time.data = p1;
152 ngx_cached_http_log_time.data = p2;
153
154 ngx_unlock(&ngx_time_lock);
Igor Sysoevd59a0472003-11-10 21:09:22 +0000155}
Igor Sysoev562e53e2003-11-13 06:14:05 +0000156
157
Igor Sysoevc1571722005-03-19 12:38:37 +0000158u_char *
159ngx_http_time(u_char *buf, time_t t)
Igor Sysoev562e53e2003-11-13 06:14:05 +0000160{
161 ngx_tm_t tm;
162
163 ngx_gmtime(t, &tm);
164
Igor Sysoevc0edbcc2004-10-21 15:34:38 +0000165 return ngx_sprintf(buf, "%s, %02d %s %4d %02d:%02d:%02d GMT",
166 week[tm.ngx_tm_wday],
167 tm.ngx_tm_mday,
168 months[tm.ngx_tm_mon - 1],
169 tm.ngx_tm_year,
170 tm.ngx_tm_hour,
171 tm.ngx_tm_min,
172 tm.ngx_tm_sec);
Igor Sysoeva7c4a2a2004-08-29 03:55:41 +0000173}
174
175
Igor Sysoevc1571722005-03-19 12:38:37 +0000176u_char *
177ngx_http_cookie_time(u_char *buf, time_t t)
Igor Sysoeva7c4a2a2004-08-29 03:55:41 +0000178{
179 ngx_tm_t tm;
180
181 ngx_gmtime(t, &tm);
182
183 /*
184 * Netscape 3.x does not understand 4-digit years at all and
185 * 2-digit years more than "37"
186 */
187
Igor Sysoevc0edbcc2004-10-21 15:34:38 +0000188 return ngx_sprintf(buf,
189 (tm.ngx_tm_year > 2037) ?
190 "%s, %02d-%s-%d %02d:%02d:%02d GMT":
191 "%s, %02d-%s-%02d %02d:%02d:%02d GMT",
192 week[tm.ngx_tm_wday],
193 tm.ngx_tm_mday,
194 months[tm.ngx_tm_mon - 1],
195 (tm.ngx_tm_year > 2037) ? tm.ngx_tm_year:
196 tm.ngx_tm_year % 100,
197 tm.ngx_tm_hour,
198 tm.ngx_tm_min,
199 tm.ngx_tm_sec);
Igor Sysoev562e53e2003-11-13 06:14:05 +0000200}
201
202
Igor Sysoevc1571722005-03-19 12:38:37 +0000203void
204ngx_gmtime(time_t t, ngx_tm_t *tp)
Igor Sysoev562e53e2003-11-13 06:14:05 +0000205{
Igor Sysoev10a543a2004-03-16 07:10:12 +0000206 ngx_int_t sec, min, hour, mday, mon, year, wday, yday, days;
Igor Sysoev562e53e2003-11-13 06:14:05 +0000207
208 days = t / 86400;
209
210 /* Jaunary 1, 1970 was Thursday */
211 wday = (4 + days) % 7;
212
213 t %= 86400;
214 hour = t / 3600;
215 t %= 3600;
Igor Sysoev5f800782003-12-08 20:48:12 +0000216 min = t / 60;
Igor Sysoev562e53e2003-11-13 06:14:05 +0000217 sec = t % 60;
218
219 /* the algorithm based on Gauss's formula */
Igor Sysoev5f800782003-12-08 20:48:12 +0000220
Igor Sysoev562e53e2003-11-13 06:14:05 +0000221 days = days - (31 + 28) + 719527;
222
223 year = days * 400 / (365 * 400 + 100 - 4 + 1);
224 yday = days - (365 * year + year / 4 - year / 100 + year / 400);
225
226 mon = (yday + 31) * 12 / 367;
227 mday = yday - (mon * 367 / 12 - 31);
228
229 mon += 2;
230
231 if (yday >= 306) {
Igor Sysoevd90282d2004-09-28 08:34:51 +0000232
Igor Sysoev11dbe972004-03-29 17:43:58 +0000233 /*
Igor Sysoeve772e8f2004-04-21 20:13:48 +0000234 * there is no "yday" in Win32 SYSTEMTIME
Igor Sysoev11dbe972004-03-29 17:43:58 +0000235 *
236 * yday -= 306;
237 */
238
Igor Sysoev562e53e2003-11-13 06:14:05 +0000239 year++;
240 mon -= 12;
241
242 if (mday == 0) {
243 /* Jaunary 31 */
244 mon = 1;
245 mday = 31;
246
247 } else if (mon == 2) {
248
249 if ((year % 4 == 0) && (year % 100 || (year % 400 == 0))) {
250 if (mday > 29) {
251 mon = 3;
252 mday -= 29;
253 }
254
255 } else if (mday > 28) {
256 mon = 3;
257 mday -= 28;
258 }
259 }
Igor Sysoev11dbe972004-03-29 17:43:58 +0000260/*
Igor Sysoeve772e8f2004-04-21 20:13:48 +0000261 * there is no "yday" in Win32 SYSTEMTIME
Igor Sysoev11dbe972004-03-29 17:43:58 +0000262 *
263 * } else {
264 * yday += 31 + 28;
265 *
266 * if ((year % 4 == 0) && (year % 100 || (year % 400 == 0))) {
Igor Sysoev1ebfead2005-02-16 13:40:36 +0000267 * yday++;
Igor Sysoev11dbe972004-03-29 17:43:58 +0000268 * }
269 */
Igor Sysoev562e53e2003-11-13 06:14:05 +0000270 }
271
Igor Sysoev10a543a2004-03-16 07:10:12 +0000272 tp->ngx_tm_sec = (ngx_tm_sec_t) sec;
273 tp->ngx_tm_min = (ngx_tm_min_t) min;
274 tp->ngx_tm_hour = (ngx_tm_hour_t) hour;
275 tp->ngx_tm_mday = (ngx_tm_mday_t) mday;
276 tp->ngx_tm_mon = (ngx_tm_mon_t) mon;
277 tp->ngx_tm_year = (ngx_tm_year_t) year;
278 tp->ngx_tm_wday = (ngx_tm_wday_t) wday;
Igor Sysoev562e53e2003-11-13 06:14:05 +0000279}