blob: 7964b008f72ec835397725a7b9e64f269bd1e9ef [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
Maxim Konovalovf8d59e32012-01-18 15:07:43 +00004 * Copyright (C) Nginx, Inc.
Igor Sysoevd90282d2004-09-28 08:34:51 +00005 */
6
7
Igor Sysoevd59a0472003-11-10 21:09:22 +00008#include <ngx_config.h>
9#include <ngx_core.h>
10
11
Maxim Dounin992318f2018-03-01 20:25:50 +030012static ngx_msec_t ngx_monotonic_time(time_t sec, ngx_uint_t msec);
13
14
Igor Sysoevc0247302004-06-27 18:01:57 +000015/*
Igor Sysoevc2068d02005-10-19 12:33:58 +000016 * The time may be updated by signal handler or by several threads.
17 * The time update operations are rare and require to hold the ngx_time_lock.
18 * The time read operations are frequent, so they are lock-free and get time
19 * values and strings from the current slot. Thus thread may get the corrupted
20 * values only if it is preempted while copying and then it is not scheduled
21 * to run more than NGX_TIME_SLOTS seconds.
Igor Sysoevc0247302004-06-27 18:01:57 +000022 */
23
Igor Sysoevc2068d02005-10-19 12:33:58 +000024#define NGX_TIME_SLOTS 64
Igor Sysoevc0247302004-06-27 18:01:57 +000025
Igor Sysoevf4b34c42006-12-19 12:38:20 +000026static ngx_uint_t slot;
Igor Sysoevc2068d02005-10-19 12:33:58 +000027static ngx_atomic_t ngx_time_lock;
Igor Sysoevc0247302004-06-27 18:01:57 +000028
Igor Sysoevc2068d02005-10-19 12:33:58 +000029volatile ngx_msec_t ngx_current_msec;
30volatile ngx_time_t *ngx_cached_time;
31volatile ngx_str_t ngx_cached_err_log_time;
32volatile ngx_str_t ngx_cached_http_time;
33volatile ngx_str_t ngx_cached_http_log_time;
Igor Sysoevc9d671c2011-03-16 15:46:57 +000034volatile ngx_str_t ngx_cached_http_log_iso8601;
Vladimir Homutov493b8982014-05-12 16:34:15 +040035volatile ngx_str_t ngx_cached_syslog_time;
Igor Sysoevc0247302004-06-27 18:01:57 +000036
Igor Sysoev6d45d8a2010-03-25 09:10:10 +000037#if !(NGX_WIN32)
Igor Sysoev2f916a92010-03-13 18:08:07 +000038
39/*
Ruslan Ermilov47a04aa2012-04-03 07:37:31 +000040 * localtime() and localtime_r() are not Async-Signal-Safe functions, therefore,
Igor Sysoev6d45d8a2010-03-25 09:10:10 +000041 * they must not be called by a signal handler, so we use the cached
Igor Sysoev2f916a92010-03-13 18:08:07 +000042 * GMT offset value. Fortunately the value is changed only two times a year.
43 */
44
45static ngx_int_t cached_gmtoff;
46#endif
47
Igor Sysoevc2068d02005-10-19 12:33:58 +000048static ngx_time_t cached_time[NGX_TIME_SLOTS];
49static u_char cached_err_log_time[NGX_TIME_SLOTS]
50 [sizeof("1970/09/28 12:00:00")];
51static u_char cached_http_time[NGX_TIME_SLOTS]
52 [sizeof("Mon, 28 Sep 1970 06:00:00 GMT")];
53static u_char cached_http_log_time[NGX_TIME_SLOTS]
54 [sizeof("28/Sep/1970:12:00:00 +0600")];
Igor Sysoevc9d671c2011-03-16 15:46:57 +000055static u_char cached_http_log_iso8601[NGX_TIME_SLOTS]
56 [sizeof("1970-09-28T12:00:00+06:00")];
Vladimir Homutov493b8982014-05-12 16:34:15 +040057static u_char cached_syslog_time[NGX_TIME_SLOTS]
58 [sizeof("Sep 28 12:00:00")];
Igor Sysoevd59a0472003-11-10 21:09:22 +000059
60
Igor Sysoev3f4685f2004-04-25 20:13:21 +000061static char *week[] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
Igor Sysoev562e53e2003-11-13 06:14:05 +000062static char *months[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun",
63 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
64
Igor Sysoevc1571722005-03-19 12:38:37 +000065void
66ngx_time_init(void)
Igor Sysoev562e53e2003-11-13 06:14:05 +000067{
Igor Sysoevc0247302004-06-27 18:01:57 +000068 ngx_cached_err_log_time.len = sizeof("1970/09/28 12:00:00") - 1;
69 ngx_cached_http_time.len = sizeof("Mon, 28 Sep 1970 06:00:00 GMT") - 1;
70 ngx_cached_http_log_time.len = sizeof("28/Sep/1970:12:00:00 +0600") - 1;
Igor Sysoevc9d671c2011-03-16 15:46:57 +000071 ngx_cached_http_log_iso8601.len = sizeof("1970-09-28T12:00:00+06:00") - 1;
Vladimir Homutov493b8982014-05-12 16:34:15 +040072 ngx_cached_syslog_time.len = sizeof("Sep 28 12:00:00") - 1;
Igor Sysoev732a2712004-04-21 18:54:33 +000073
Igor Sysoevc0247302004-06-27 18:01:57 +000074 ngx_cached_time = &cached_time[0];
Igor Sysoevf5003d82003-12-04 14:53:00 +000075
Igor Sysoev6d45d8a2010-03-25 09:10:10 +000076 ngx_time_update();
Igor Sysoev562e53e2003-11-13 06:14:05 +000077}
78
79
Igor Sysoevc1571722005-03-19 12:38:37 +000080void
Igor Sysoev6d45d8a2010-03-25 09:10:10 +000081ngx_time_update(void)
Igor Sysoevd59a0472003-11-10 21:09:22 +000082{
Vladimir Homutov493b8982014-05-12 16:34:15 +040083 u_char *p0, *p1, *p2, *p3, *p4;
Igor Sysoevc2068d02005-10-19 12:33:58 +000084 ngx_tm_t tm, gmt;
Igor Sysoev2f916a92010-03-13 18:08:07 +000085 time_t sec;
86 ngx_uint_t msec;
Igor Sysoevc2068d02005-10-19 12:33:58 +000087 ngx_time_t *tp;
88 struct timeval tv;
Igor Sysoevd59a0472003-11-10 21:09:22 +000089
Igor Sysoevc2068d02005-10-19 12:33:58 +000090 if (!ngx_trylock(&ngx_time_lock)) {
Igor Sysoev898446c2004-02-26 17:10:01 +000091 return;
Igor Sysoevb54698b2004-02-23 20:57:12 +000092 }
Igor Sysoevc0247302004-06-27 18:01:57 +000093
Igor Sysoev2f916a92010-03-13 18:08:07 +000094 ngx_gettimeofday(&tv);
Igor Sysoevb54698b2004-02-23 20:57:12 +000095
Igor Sysoev2f916a92010-03-13 18:08:07 +000096 sec = tv.tv_sec;
97 msec = tv.tv_usec / 1000;
Igor Sysoev3c3ca172004-01-05 20:55:48 +000098
Maxim Dounin992318f2018-03-01 20:25:50 +030099 ngx_current_msec = ngx_monotonic_time(sec, msec);
Igor Sysoevc0247302004-06-27 18:01:57 +0000100
Igor Sysoevc2068d02005-10-19 12:33:58 +0000101 tp = &cached_time[slot];
102
Igor Sysoevc2068d02005-10-19 12:33:58 +0000103 if (tp->sec == sec) {
Igor Sysoevc3d106a2006-12-06 14:25:20 +0000104 tp->msec = msec;
Igor Sysoevc2068d02005-10-19 12:33:58 +0000105 ngx_unlock(&ngx_time_lock);
106 return;
107 }
108
Igor Sysoevf51d4fe2007-01-29 11:52:25 +0000109 if (slot == NGX_TIME_SLOTS - 1) {
Igor Sysoevc3d106a2006-12-06 14:25:20 +0000110 slot = 0;
111 } else {
112 slot++;
113 }
114
115 tp = &cached_time[slot];
116
Igor Sysoevc2068d02005-10-19 12:33:58 +0000117 tp->sec = sec;
Igor Sysoevc3d106a2006-12-06 14:25:20 +0000118 tp->msec = msec;
Igor Sysoevc2068d02005-10-19 12:33:58 +0000119
120 ngx_gmtime(sec, &gmt);
Igor Sysoevd59a0472003-11-10 21:09:22 +0000121
Igor Sysoev732a2712004-04-21 18:54:33 +0000122
Igor Sysoev80a29012007-01-25 22:01:23 +0000123 p0 = &cached_http_time[slot][0];
Igor Sysoev732a2712004-04-21 18:54:33 +0000124
Igor Sysoevc2068d02005-10-19 12:33:58 +0000125 (void) ngx_sprintf(p0, "%s, %02d %s %4d %02d:%02d:%02d GMT",
126 week[gmt.ngx_tm_wday], gmt.ngx_tm_mday,
127 months[gmt.ngx_tm_mon - 1], gmt.ngx_tm_year,
128 gmt.ngx_tm_hour, gmt.ngx_tm_min, gmt.ngx_tm_sec);
Igor Sysoev732a2712004-04-21 18:54:33 +0000129
Igor Sysoevf6906042004-11-25 16:17:31 +0000130#if (NGX_HAVE_GETTIMEZONE)
Igor Sysoev732a2712004-04-21 18:54:33 +0000131
Igor Sysoevc2068d02005-10-19 12:33:58 +0000132 tp->gmtoff = ngx_gettimezone();
133 ngx_gmtime(sec + tp->gmtoff * 60, &tm);
Igor Sysoev732a2712004-04-21 18:54:33 +0000134
Igor Sysoev6d45d8a2010-03-25 09:10:10 +0000135#elif (NGX_HAVE_GMTOFF)
136
137 ngx_localtime(sec, &tm);
138 cached_gmtoff = (ngx_int_t) (tm.ngx_tm_gmtoff / 60);
139 tp->gmtoff = cached_gmtoff;
140
Igor Sysoev732a2712004-04-21 18:54:33 +0000141#else
Igor Sysoevd59a0472003-11-10 21:09:22 +0000142
Igor Sysoev6d45d8a2010-03-25 09:10:10 +0000143 ngx_localtime(sec, &tm);
144 cached_gmtoff = ngx_timezone(tm.ngx_tm_isdst);
Igor Sysoev2f916a92010-03-13 18:08:07 +0000145 tp->gmtoff = cached_gmtoff;
Igor Sysoevd59a0472003-11-10 21:09:22 +0000146
Igor Sysoev732a2712004-04-21 18:54:33 +0000147#endif
Igor Sysoev562e53e2003-11-13 06:14:05 +0000148
Igor Sysoev732a2712004-04-21 18:54:33 +0000149
Igor Sysoev80a29012007-01-25 22:01:23 +0000150 p1 = &cached_err_log_time[slot][0];
Igor Sysoev732a2712004-04-21 18:54:33 +0000151
Igor Sysoevc2068d02005-10-19 12:33:58 +0000152 (void) ngx_sprintf(p1, "%4d/%02d/%02d %02d:%02d:%02d",
Igor Sysoev4959ec42005-05-23 12:07:45 +0000153 tm.ngx_tm_year, tm.ngx_tm_mon,
154 tm.ngx_tm_mday, tm.ngx_tm_hour,
155 tm.ngx_tm_min, tm.ngx_tm_sec);
Igor Sysoev732a2712004-04-21 18:54:33 +0000156
Igor Sysoev732a2712004-04-21 18:54:33 +0000157
Igor Sysoev80a29012007-01-25 22:01:23 +0000158 p2 = &cached_http_log_time[slot][0];
Igor Sysoev732a2712004-04-21 18:54:33 +0000159
Sergey Kandaurov6c789502016-03-31 02:34:00 +0300160 (void) ngx_sprintf(p2, "%02d/%s/%d:%02d:%02d:%02d %c%02i%02i",
Igor Sysoev4959ec42005-05-23 12:07:45 +0000161 tm.ngx_tm_mday, months[tm.ngx_tm_mon - 1],
162 tm.ngx_tm_year, tm.ngx_tm_hour,
163 tm.ngx_tm_min, tm.ngx_tm_sec,
Igor Sysoevc2068d02005-10-19 12:33:58 +0000164 tp->gmtoff < 0 ? '-' : '+',
165 ngx_abs(tp->gmtoff / 60), ngx_abs(tp->gmtoff % 60));
Igor Sysoev732a2712004-04-21 18:54:33 +0000166
Igor Sysoevc9d671c2011-03-16 15:46:57 +0000167 p3 = &cached_http_log_iso8601[slot][0];
168
Sergey Kandaurov6c789502016-03-31 02:34:00 +0300169 (void) ngx_sprintf(p3, "%4d-%02d-%02dT%02d:%02d:%02d%c%02i:%02i",
Igor Sysoevc9d671c2011-03-16 15:46:57 +0000170 tm.ngx_tm_year, tm.ngx_tm_mon,
171 tm.ngx_tm_mday, tm.ngx_tm_hour,
172 tm.ngx_tm_min, tm.ngx_tm_sec,
173 tp->gmtoff < 0 ? '-' : '+',
174 ngx_abs(tp->gmtoff / 60), ngx_abs(tp->gmtoff % 60));
175
Vladimir Homutov493b8982014-05-12 16:34:15 +0400176 p4 = &cached_syslog_time[slot][0];
177
178 (void) ngx_sprintf(p4, "%s %2d %02d:%02d:%02d",
179 months[tm.ngx_tm_mon - 1], tm.ngx_tm_mday,
180 tm.ngx_tm_hour, tm.ngx_tm_min, tm.ngx_tm_sec);
Igor Sysoevb54698b2004-02-23 20:57:12 +0000181
Igor Sysoevc2068d02005-10-19 12:33:58 +0000182 ngx_memory_barrier();
Igor Sysoevb54698b2004-02-23 20:57:12 +0000183
Igor Sysoevc2068d02005-10-19 12:33:58 +0000184 ngx_cached_time = tp;
185 ngx_cached_http_time.data = p0;
186 ngx_cached_err_log_time.data = p1;
187 ngx_cached_http_log_time.data = p2;
Igor Sysoevc9d671c2011-03-16 15:46:57 +0000188 ngx_cached_http_log_iso8601.data = p3;
Vladimir Homutov493b8982014-05-12 16:34:15 +0400189 ngx_cached_syslog_time.data = p4;
Igor Sysoevc2068d02005-10-19 12:33:58 +0000190
191 ngx_unlock(&ngx_time_lock);
Igor Sysoevd59a0472003-11-10 21:09:22 +0000192}
Igor Sysoev562e53e2003-11-13 06:14:05 +0000193
194
Maxim Dounin992318f2018-03-01 20:25:50 +0300195static ngx_msec_t
196ngx_monotonic_time(time_t sec, ngx_uint_t msec)
197{
198#if (NGX_HAVE_CLOCK_MONOTONIC)
199 struct timespec ts;
200
201#if defined(CLOCK_MONOTONIC_FAST)
202 clock_gettime(CLOCK_MONOTONIC_FAST, &ts);
203
204#elif defined(CLOCK_MONOTONIC_COARSE)
205 clock_gettime(CLOCK_MONOTONIC_COARSE, &ts);
206
207#else
208 clock_gettime(CLOCK_MONOTONIC, &ts);
209#endif
210
211 sec = ts.tv_sec;
212 msec = ts.tv_nsec / 1000000;
213
214#endif
215
216 return (ngx_msec_t) sec * 1000 + msec;
217}
218
219
Igor Sysoev6d45d8a2010-03-25 09:10:10 +0000220#if !(NGX_WIN32)
221
222void
223ngx_time_sigsafe_update(void)
224{
Vladimir Homutov493b8982014-05-12 16:34:15 +0400225 u_char *p, *p2;
Igor Sysoev6d45d8a2010-03-25 09:10:10 +0000226 ngx_tm_t tm;
227 time_t sec;
Igor Sysoev6d45d8a2010-03-25 09:10:10 +0000228 ngx_time_t *tp;
229 struct timeval tv;
230
231 if (!ngx_trylock(&ngx_time_lock)) {
232 return;
233 }
234
235 ngx_gettimeofday(&tv);
236
237 sec = tv.tv_sec;
Igor Sysoev6d45d8a2010-03-25 09:10:10 +0000238
239 tp = &cached_time[slot];
240
241 if (tp->sec == sec) {
242 ngx_unlock(&ngx_time_lock);
243 return;
244 }
245
246 if (slot == NGX_TIME_SLOTS - 1) {
247 slot = 0;
248 } else {
249 slot++;
250 }
251
Maxim Dounin25197b32012-08-03 09:10:39 +0000252 tp = &cached_time[slot];
253
254 tp->sec = 0;
255
Igor Sysoev6d45d8a2010-03-25 09:10:10 +0000256 ngx_gmtime(sec + cached_gmtoff * 60, &tm);
257
258 p = &cached_err_log_time[slot][0];
259
260 (void) ngx_sprintf(p, "%4d/%02d/%02d %02d:%02d:%02d",
261 tm.ngx_tm_year, tm.ngx_tm_mon,
262 tm.ngx_tm_mday, tm.ngx_tm_hour,
263 tm.ngx_tm_min, tm.ngx_tm_sec);
264
Vladimir Homutov493b8982014-05-12 16:34:15 +0400265 p2 = &cached_syslog_time[slot][0];
266
267 (void) ngx_sprintf(p2, "%s %2d %02d:%02d:%02d",
268 months[tm.ngx_tm_mon - 1], tm.ngx_tm_mday,
269 tm.ngx_tm_hour, tm.ngx_tm_min, tm.ngx_tm_sec);
270
Igor Sysoev6d45d8a2010-03-25 09:10:10 +0000271 ngx_memory_barrier();
272
273 ngx_cached_err_log_time.data = p;
Vladimir Homutov493b8982014-05-12 16:34:15 +0400274 ngx_cached_syslog_time.data = p2;
Igor Sysoev6d45d8a2010-03-25 09:10:10 +0000275
276 ngx_unlock(&ngx_time_lock);
277}
278
279#endif
280
281
Igor Sysoevc1571722005-03-19 12:38:37 +0000282u_char *
283ngx_http_time(u_char *buf, time_t t)
Igor Sysoev562e53e2003-11-13 06:14:05 +0000284{
285 ngx_tm_t tm;
286
287 ngx_gmtime(t, &tm);
288
Igor Sysoevc0edbcc2004-10-21 15:34:38 +0000289 return ngx_sprintf(buf, "%s, %02d %s %4d %02d:%02d:%02d GMT",
290 week[tm.ngx_tm_wday],
291 tm.ngx_tm_mday,
292 months[tm.ngx_tm_mon - 1],
293 tm.ngx_tm_year,
294 tm.ngx_tm_hour,
295 tm.ngx_tm_min,
296 tm.ngx_tm_sec);
Igor Sysoeva7c4a2a2004-08-29 03:55:41 +0000297}
298
299
Igor Sysoevc1571722005-03-19 12:38:37 +0000300u_char *
301ngx_http_cookie_time(u_char *buf, time_t t)
Igor Sysoeva7c4a2a2004-08-29 03:55:41 +0000302{
303 ngx_tm_t tm;
304
305 ngx_gmtime(t, &tm);
306
307 /*
308 * Netscape 3.x does not understand 4-digit years at all and
309 * 2-digit years more than "37"
310 */
311
Igor Sysoevc0edbcc2004-10-21 15:34:38 +0000312 return ngx_sprintf(buf,
313 (tm.ngx_tm_year > 2037) ?
314 "%s, %02d-%s-%d %02d:%02d:%02d GMT":
315 "%s, %02d-%s-%02d %02d:%02d:%02d GMT",
316 week[tm.ngx_tm_wday],
317 tm.ngx_tm_mday,
318 months[tm.ngx_tm_mon - 1],
319 (tm.ngx_tm_year > 2037) ? tm.ngx_tm_year:
320 tm.ngx_tm_year % 100,
321 tm.ngx_tm_hour,
322 tm.ngx_tm_min,
323 tm.ngx_tm_sec);
Igor Sysoev562e53e2003-11-13 06:14:05 +0000324}
325
326
Igor Sysoevc1571722005-03-19 12:38:37 +0000327void
328ngx_gmtime(time_t t, ngx_tm_t *tp)
Igor Sysoev562e53e2003-11-13 06:14:05 +0000329{
Igor Sysoev4611ad32008-04-13 13:33:12 +0000330 ngx_int_t yday;
Maxim Dounin18b5d682017-09-13 15:52:01 +0300331 ngx_uint_t sec, min, hour, mday, mon, year, wday, days, leap;
Igor Sysoev562e53e2003-11-13 06:14:05 +0000332
Igor Sysoevac5deaa2008-01-31 15:14:31 +0000333 /* the calculation is valid for positive time_t only */
Igor Sysoev4611ad32008-04-13 13:33:12 +0000334
Maxim Dounin18b5d682017-09-13 15:52:01 +0300335 if (t < 0) {
336 t = 0;
337 }
Igor Sysoevac5deaa2008-01-31 15:14:31 +0000338
Maxim Dounin18b5d682017-09-13 15:52:01 +0300339 days = t / 86400;
340 sec = t % 86400;
Igor Sysoev562e53e2003-11-13 06:14:05 +0000341
Maxim Dounin6a30b1b2017-09-13 15:53:19 +0300342 /*
343 * no more than 4 year digits supported,
344 * truncate to December 31, 9999, 23:59:59
345 */
346
347 if (days > 2932896) {
348 days = 2932896;
349 sec = 86399;
350 }
351
Ruslan Ermilovb74f8ff2012-02-28 11:31:05 +0000352 /* January 1, 1970 was Thursday */
Igor Sysoev4611ad32008-04-13 13:33:12 +0000353
Igor Sysoev562e53e2003-11-13 06:14:05 +0000354 wday = (4 + days) % 7;
355
Maxim Dounin18b5d682017-09-13 15:52:01 +0300356 hour = sec / 3600;
357 sec %= 3600;
358 min = sec / 60;
359 sec %= 60;
Igor Sysoev562e53e2003-11-13 06:14:05 +0000360
Igor Sysoev4611ad32008-04-13 13:33:12 +0000361 /*
362 * the algorithm based on Gauss' formula,
Maxim Douninbfa93662017-09-13 15:51:58 +0300363 * see src/core/ngx_parse_time.c
Igor Sysoev4611ad32008-04-13 13:33:12 +0000364 */
Igor Sysoev5f800782003-12-08 20:48:12 +0000365
Igor Sysoev4611ad32008-04-13 13:33:12 +0000366 /* days since March 1, 1 BC */
Igor Sysoev562e53e2003-11-13 06:14:05 +0000367 days = days - (31 + 28) + 719527;
368
Igor Sysoev4611ad32008-04-13 13:33:12 +0000369 /*
370 * The "days" should be adjusted to 1 only, however, some March 1st's go
371 * to previous year, so we adjust them to 2. This causes also shift of the
Ruslan Ermilov47a04aa2012-04-03 07:37:31 +0000372 * last February days to next year, but we catch the case when "yday"
Igor Sysoev4611ad32008-04-13 13:33:12 +0000373 * becomes negative.
374 */
375
376 year = (days + 2) * 400 / (365 * 400 + 100 - 4 + 1);
377
Igor Sysoev562e53e2003-11-13 06:14:05 +0000378 yday = days - (365 * year + year / 4 - year / 100 + year / 400);
379
Igor Sysoev4611ad32008-04-13 13:33:12 +0000380 if (yday < 0) {
381 leap = (year % 4 == 0) && (year % 100 || (year % 400 == 0));
382 yday = 365 + leap + yday;
383 year--;
384 }
Igor Sysoev562e53e2003-11-13 06:14:05 +0000385
Igor Sysoev4611ad32008-04-13 13:33:12 +0000386 /*
387 * The empirical formula that maps "yday" to month.
388 * There are at least 10 variants, some of them are:
389 * mon = (yday + 31) * 15 / 459
390 * mon = (yday + 31) * 17 / 520
391 * mon = (yday + 31) * 20 / 612
392 */
393
394 mon = (yday + 31) * 10 / 306;
395
396 /* the Gauss' formula that evaluates days before the month */
397
398 mday = yday - (367 * mon / 12 - 30) + 1;
Igor Sysoev562e53e2003-11-13 06:14:05 +0000399
400 if (yday >= 306) {
Igor Sysoevd90282d2004-09-28 08:34:51 +0000401
Igor Sysoev4611ad32008-04-13 13:33:12 +0000402 year++;
403 mon -= 10;
404
Igor Sysoev11dbe972004-03-29 17:43:58 +0000405 /*
Igor Sysoeve772e8f2004-04-21 20:13:48 +0000406 * there is no "yday" in Win32 SYSTEMTIME
Igor Sysoev11dbe972004-03-29 17:43:58 +0000407 *
408 * yday -= 306;
409 */
410
Igor Sysoev4611ad32008-04-13 13:33:12 +0000411 } else {
Igor Sysoev562e53e2003-11-13 06:14:05 +0000412
Igor Sysoev4611ad32008-04-13 13:33:12 +0000413 mon += 2;
Igor Sysoev562e53e2003-11-13 06:14:05 +0000414
Igor Sysoev4611ad32008-04-13 13:33:12 +0000415 /*
416 * there is no "yday" in Win32 SYSTEMTIME
417 *
418 * yday += 31 + 28 + leap;
419 */
Igor Sysoev562e53e2003-11-13 06:14:05 +0000420 }
421
Igor Sysoev10a543a2004-03-16 07:10:12 +0000422 tp->ngx_tm_sec = (ngx_tm_sec_t) sec;
423 tp->ngx_tm_min = (ngx_tm_min_t) min;
424 tp->ngx_tm_hour = (ngx_tm_hour_t) hour;
425 tp->ngx_tm_mday = (ngx_tm_mday_t) mday;
426 tp->ngx_tm_mon = (ngx_tm_mon_t) mon;
427 tp->ngx_tm_year = (ngx_tm_year_t) year;
428 tp->ngx_tm_wday = (ngx_tm_wday_t) wday;
Igor Sysoev562e53e2003-11-13 06:14:05 +0000429}
Igor Sysoevc2717752008-08-11 15:28:15 +0000430
431
432time_t
433ngx_next_time(time_t when)
434{
435 time_t now, next;
436 struct tm tm;
437
438 now = ngx_time();
439
440 ngx_libc_localtime(now, &tm);
441
442 tm.tm_hour = (int) (when / 3600);
443 when %= 3600;
444 tm.tm_min = (int) (when / 60);
445 tm.tm_sec = (int) (when % 60);
446
447 next = mktime(&tm);
448
449 if (next == -1) {
450 return -1;
451 }
452
453 if (next - now > 0) {
454 return next;
455 }
456
457 tm.tm_mday++;
458
459 /* mktime() should normalize a date (Jan 32, etc) */
460
461 next = mktime(&tm);
462
463 if (next != -1) {
464 return next;
465 }
466
467 return -1;
468}