2003-11-11 05:09:22 +08:00
|
|
|
|
2004-09-28 16:34:51 +08:00
|
|
|
/*
|
2004-09-30 00:00:49 +08:00
|
|
|
* Copyright (C) Igor Sysoev
|
2004-09-28 16:34:51 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
2003-11-11 05:09:22 +08:00
|
|
|
#include <ngx_config.h>
|
|
|
|
#include <ngx_core.h>
|
|
|
|
|
|
|
|
|
2004-06-28 02:01:57 +08:00
|
|
|
/*
|
2005-10-19 20:33:58 +08:00
|
|
|
* The time may be updated by signal handler or by several threads.
|
|
|
|
* The time update operations are rare and require to hold the ngx_time_lock.
|
|
|
|
* The time read operations are frequent, so they are lock-free and get time
|
|
|
|
* values and strings from the current slot. Thus thread may get the corrupted
|
|
|
|
* values only if it is preempted while copying and then it is not scheduled
|
|
|
|
* to run more than NGX_TIME_SLOTS seconds.
|
2004-06-28 02:01:57 +08:00
|
|
|
*/
|
|
|
|
|
2005-10-19 20:33:58 +08:00
|
|
|
#define NGX_TIME_SLOTS 64
|
2004-06-28 02:01:57 +08:00
|
|
|
|
2006-12-19 20:38:20 +08:00
|
|
|
static ngx_uint_t slot;
|
2005-10-19 20:33:58 +08:00
|
|
|
static ngx_atomic_t ngx_time_lock;
|
2004-06-28 02:01:57 +08:00
|
|
|
|
2005-10-19 20:33:58 +08:00
|
|
|
volatile ngx_msec_t ngx_current_msec;
|
|
|
|
volatile ngx_time_t *ngx_cached_time;
|
|
|
|
volatile ngx_str_t ngx_cached_err_log_time;
|
|
|
|
volatile ngx_str_t ngx_cached_http_time;
|
|
|
|
volatile ngx_str_t ngx_cached_http_log_time;
|
2004-06-28 02:01:57 +08:00
|
|
|
|
2005-10-19 20:33:58 +08:00
|
|
|
static ngx_time_t cached_time[NGX_TIME_SLOTS];
|
|
|
|
static u_char cached_err_log_time[NGX_TIME_SLOTS]
|
|
|
|
[sizeof("1970/09/28 12:00:00")];
|
|
|
|
static u_char cached_http_time[NGX_TIME_SLOTS]
|
|
|
|
[sizeof("Mon, 28 Sep 1970 06:00:00 GMT")];
|
|
|
|
static u_char cached_http_log_time[NGX_TIME_SLOTS]
|
|
|
|
[sizeof("28/Sep/1970:12:00:00 +0600")];
|
2003-11-11 05:09:22 +08:00
|
|
|
|
|
|
|
|
2004-04-26 04:13:21 +08:00
|
|
|
static char *week[] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
|
2003-11-13 14:14:05 +08:00
|
|
|
static char *months[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun",
|
|
|
|
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
|
2003-11-11 05:09:22 +08:00
|
|
|
|
2005-03-19 20:38:37 +08:00
|
|
|
void
|
|
|
|
ngx_time_init(void)
|
2003-11-11 05:09:22 +08:00
|
|
|
{
|
2004-06-28 02:01:57 +08:00
|
|
|
ngx_cached_err_log_time.len = sizeof("1970/09/28 12:00:00") - 1;
|
|
|
|
ngx_cached_http_time.len = sizeof("Mon, 28 Sep 1970 06:00:00 GMT") - 1;
|
|
|
|
ngx_cached_http_log_time.len = sizeof("28/Sep/1970:12:00:00 +0600") - 1;
|
2004-04-22 02:54:33 +08:00
|
|
|
|
2004-06-28 02:01:57 +08:00
|
|
|
ngx_cached_time = &cached_time[0];
|
2003-12-04 22:53:00 +08:00
|
|
|
|
2005-10-19 20:33:58 +08:00
|
|
|
ngx_time_update(0, 0);
|
2003-11-13 14:14:05 +08:00
|
|
|
}
|
2003-11-13 01:25:12 +08:00
|
|
|
|
|
|
|
|
2005-03-19 20:38:37 +08:00
|
|
|
void
|
2005-10-19 20:33:58 +08:00
|
|
|
ngx_time_update(time_t sec, ngx_uint_t msec)
|
2003-11-13 14:14:05 +08:00
|
|
|
{
|
2005-10-19 20:33:58 +08:00
|
|
|
u_char *p0, *p1, *p2;
|
|
|
|
ngx_tm_t tm, gmt;
|
|
|
|
ngx_time_t *tp;
|
|
|
|
struct timeval tv;
|
2004-06-28 02:01:57 +08:00
|
|
|
|
2005-10-19 20:33:58 +08:00
|
|
|
if (!ngx_trylock(&ngx_time_lock)) {
|
2004-02-27 01:10:01 +08:00
|
|
|
return;
|
2004-02-24 04:57:12 +08:00
|
|
|
}
|
2004-06-28 02:01:57 +08:00
|
|
|
|
2005-10-19 20:33:58 +08:00
|
|
|
if (sec == 0) {
|
|
|
|
ngx_gettimeofday(&tv);
|
2004-02-24 04:57:12 +08:00
|
|
|
|
2005-10-19 20:33:58 +08:00
|
|
|
sec = tv.tv_sec;
|
|
|
|
msec = tv.tv_usec / 1000;
|
|
|
|
}
|
2004-01-06 04:55:48 +08:00
|
|
|
|
2005-10-19 20:33:58 +08:00
|
|
|
ngx_current_msec = (ngx_msec_t) sec * 1000 + msec;
|
2003-11-11 05:09:22 +08:00
|
|
|
|
2005-10-19 20:33:58 +08:00
|
|
|
tp = &cached_time[slot];
|
2004-04-22 02:54:33 +08:00
|
|
|
|
2005-10-19 20:33:58 +08:00
|
|
|
if (tp->sec == sec) {
|
2006-12-06 22:25:20 +08:00
|
|
|
tp->msec = msec;
|
2005-10-19 20:33:58 +08:00
|
|
|
ngx_unlock(&ngx_time_lock);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2007-01-29 19:52:25 +08:00
|
|
|
if (slot == NGX_TIME_SLOTS - 1) {
|
2006-12-06 22:25:20 +08:00
|
|
|
slot = 0;
|
|
|
|
} else {
|
|
|
|
slot++;
|
|
|
|
}
|
|
|
|
|
|
|
|
tp = &cached_time[slot];
|
|
|
|
|
2005-10-19 20:33:58 +08:00
|
|
|
tp->sec = sec;
|
2006-12-06 22:25:20 +08:00
|
|
|
tp->msec = msec;
|
2004-04-22 02:54:33 +08:00
|
|
|
|
2005-10-19 20:33:58 +08:00
|
|
|
ngx_gmtime(sec, &gmt);
|
2004-04-22 02:54:33 +08:00
|
|
|
|
|
|
|
|
2007-01-26 06:01:23 +08:00
|
|
|
p0 = &cached_http_time[slot][0];
|
2005-10-19 20:33:58 +08:00
|
|
|
|
|
|
|
(void) ngx_sprintf(p0, "%s, %02d %s %4d %02d:%02d:%02d GMT",
|
|
|
|
week[gmt.ngx_tm_wday], gmt.ngx_tm_mday,
|
|
|
|
months[gmt.ngx_tm_mon - 1], gmt.ngx_tm_year,
|
|
|
|
gmt.ngx_tm_hour, gmt.ngx_tm_min, gmt.ngx_tm_sec);
|
2004-04-22 02:54:33 +08:00
|
|
|
|
2004-11-26 00:17:31 +08:00
|
|
|
#if (NGX_HAVE_GETTIMEZONE)
|
2004-04-22 02:54:33 +08:00
|
|
|
|
2005-10-19 20:33:58 +08:00
|
|
|
tp->gmtoff = ngx_gettimezone();
|
|
|
|
ngx_gmtime(sec + tp->gmtoff * 60, &tm);
|
2004-04-22 02:54:33 +08:00
|
|
|
|
2004-11-26 00:17:31 +08:00
|
|
|
#elif (NGX_HAVE_GMTOFF)
|
2003-11-11 05:09:22 +08:00
|
|
|
|
2005-10-19 20:33:58 +08:00
|
|
|
ngx_localtime(sec, &tm);
|
|
|
|
tp->gmtoff = (ngx_int_t) (tm.ngx_tm_gmtoff / 60);
|
2004-04-22 02:54:33 +08:00
|
|
|
|
2004-06-18 01:18:53 +08:00
|
|
|
#else
|
|
|
|
|
2005-10-19 20:33:58 +08:00
|
|
|
ngx_localtime(sec, &tm);
|
|
|
|
tp->gmtoff = ngx_timezone(tm.ngx_tm_isdst);
|
2004-06-18 01:18:53 +08:00
|
|
|
|
2004-04-22 02:54:33 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2007-01-26 06:01:23 +08:00
|
|
|
p1 = &cached_err_log_time[slot][0];
|
2004-04-22 02:54:33 +08:00
|
|
|
|
2005-10-19 20:33:58 +08:00
|
|
|
(void) ngx_sprintf(p1, "%4d/%02d/%02d %02d:%02d:%02d",
|
2005-05-23 20:07:45 +08:00
|
|
|
tm.ngx_tm_year, tm.ngx_tm_mon,
|
|
|
|
tm.ngx_tm_mday, tm.ngx_tm_hour,
|
|
|
|
tm.ngx_tm_min, tm.ngx_tm_sec);
|
2004-04-22 02:54:33 +08:00
|
|
|
|
|
|
|
|
2007-01-26 06:01:23 +08:00
|
|
|
p2 = &cached_http_log_time[slot][0];
|
2004-04-22 02:54:33 +08:00
|
|
|
|
2005-10-19 20:33:58 +08:00
|
|
|
(void) ngx_sprintf(p2, "%02d/%s/%d:%02d:%02d:%02d %c%02d%02d",
|
2005-05-23 20:07:45 +08:00
|
|
|
tm.ngx_tm_mday, months[tm.ngx_tm_mon - 1],
|
|
|
|
tm.ngx_tm_year, tm.ngx_tm_hour,
|
|
|
|
tm.ngx_tm_min, tm.ngx_tm_sec,
|
2005-10-19 20:33:58 +08:00
|
|
|
tp->gmtoff < 0 ? '-' : '+',
|
|
|
|
ngx_abs(tp->gmtoff / 60), ngx_abs(tp->gmtoff % 60));
|
2004-04-22 02:54:33 +08:00
|
|
|
|
2003-11-11 05:09:22 +08:00
|
|
|
|
2005-10-19 20:33:58 +08:00
|
|
|
ngx_memory_barrier();
|
2004-02-24 04:57:12 +08:00
|
|
|
|
2005-10-19 20:33:58 +08:00
|
|
|
ngx_cached_time = tp;
|
|
|
|
ngx_cached_http_time.data = p0;
|
|
|
|
ngx_cached_err_log_time.data = p1;
|
|
|
|
ngx_cached_http_log_time.data = p2;
|
2004-02-24 04:57:12 +08:00
|
|
|
|
2005-10-19 20:33:58 +08:00
|
|
|
ngx_unlock(&ngx_time_lock);
|
2003-11-11 05:09:22 +08:00
|
|
|
}
|
2003-11-13 14:14:05 +08:00
|
|
|
|
|
|
|
|
2005-03-19 20:38:37 +08:00
|
|
|
u_char *
|
|
|
|
ngx_http_time(u_char *buf, time_t t)
|
2003-11-13 14:14:05 +08:00
|
|
|
{
|
|
|
|
ngx_tm_t tm;
|
|
|
|
|
|
|
|
ngx_gmtime(t, &tm);
|
|
|
|
|
2004-10-21 23:34:38 +08:00
|
|
|
return ngx_sprintf(buf, "%s, %02d %s %4d %02d:%02d:%02d GMT",
|
|
|
|
week[tm.ngx_tm_wday],
|
|
|
|
tm.ngx_tm_mday,
|
|
|
|
months[tm.ngx_tm_mon - 1],
|
|
|
|
tm.ngx_tm_year,
|
|
|
|
tm.ngx_tm_hour,
|
|
|
|
tm.ngx_tm_min,
|
|
|
|
tm.ngx_tm_sec);
|
2004-08-29 11:55:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-03-19 20:38:37 +08:00
|
|
|
u_char *
|
|
|
|
ngx_http_cookie_time(u_char *buf, time_t t)
|
2004-08-29 11:55:41 +08:00
|
|
|
{
|
|
|
|
ngx_tm_t tm;
|
|
|
|
|
|
|
|
ngx_gmtime(t, &tm);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Netscape 3.x does not understand 4-digit years at all and
|
|
|
|
* 2-digit years more than "37"
|
|
|
|
*/
|
|
|
|
|
2004-10-21 23:34:38 +08:00
|
|
|
return ngx_sprintf(buf,
|
|
|
|
(tm.ngx_tm_year > 2037) ?
|
|
|
|
"%s, %02d-%s-%d %02d:%02d:%02d GMT":
|
|
|
|
"%s, %02d-%s-%02d %02d:%02d:%02d GMT",
|
|
|
|
week[tm.ngx_tm_wday],
|
|
|
|
tm.ngx_tm_mday,
|
|
|
|
months[tm.ngx_tm_mon - 1],
|
|
|
|
(tm.ngx_tm_year > 2037) ? tm.ngx_tm_year:
|
|
|
|
tm.ngx_tm_year % 100,
|
|
|
|
tm.ngx_tm_hour,
|
|
|
|
tm.ngx_tm_min,
|
|
|
|
tm.ngx_tm_sec);
|
2003-11-13 14:14:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-03-19 20:38:37 +08:00
|
|
|
void
|
|
|
|
ngx_gmtime(time_t t, ngx_tm_t *tp)
|
2003-11-13 14:14:05 +08:00
|
|
|
{
|
2008-04-13 21:33:12 +08:00
|
|
|
ngx_int_t yday;
|
|
|
|
ngx_uint_t n, sec, min, hour, mday, mon, year, wday, days, leap;
|
2003-11-13 14:14:05 +08:00
|
|
|
|
2008-01-31 23:14:31 +08:00
|
|
|
/* the calculation is valid for positive time_t only */
|
2008-04-13 21:33:12 +08:00
|
|
|
|
2008-01-31 23:14:31 +08:00
|
|
|
n = (ngx_uint_t) t;
|
|
|
|
|
|
|
|
days = n / 86400;
|
2003-11-13 14:14:05 +08:00
|
|
|
|
|
|
|
/* Jaunary 1, 1970 was Thursday */
|
2008-04-13 21:33:12 +08:00
|
|
|
|
2003-11-13 14:14:05 +08:00
|
|
|
wday = (4 + days) % 7;
|
|
|
|
|
2008-01-31 23:14:31 +08:00
|
|
|
n %= 86400;
|
|
|
|
hour = n / 3600;
|
|
|
|
n %= 3600;
|
|
|
|
min = n / 60;
|
|
|
|
sec = n % 60;
|
2003-11-13 14:14:05 +08:00
|
|
|
|
2008-04-13 21:33:12 +08:00
|
|
|
/*
|
|
|
|
* the algorithm based on Gauss' formula,
|
|
|
|
* see src/http/ngx_http_parse_time.c
|
|
|
|
*/
|
2003-12-09 04:48:12 +08:00
|
|
|
|
2008-04-13 21:33:12 +08:00
|
|
|
/* days since March 1, 1 BC */
|
2003-11-13 14:14:05 +08:00
|
|
|
days = days - (31 + 28) + 719527;
|
|
|
|
|
2008-04-13 21:33:12 +08:00
|
|
|
/*
|
|
|
|
* The "days" should be adjusted to 1 only, however, some March 1st's go
|
|
|
|
* to previous year, so we adjust them to 2. This causes also shift of the
|
|
|
|
* last Feburary days to next year, but we catch the case when "yday"
|
|
|
|
* becomes negative.
|
|
|
|
*/
|
|
|
|
|
|
|
|
year = (days + 2) * 400 / (365 * 400 + 100 - 4 + 1);
|
|
|
|
|
2003-11-13 14:14:05 +08:00
|
|
|
yday = days - (365 * year + year / 4 - year / 100 + year / 400);
|
|
|
|
|
2008-04-13 21:33:12 +08:00
|
|
|
if (yday < 0) {
|
|
|
|
leap = (year % 4 == 0) && (year % 100 || (year % 400 == 0));
|
|
|
|
yday = 365 + leap + yday;
|
|
|
|
year--;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The empirical formula that maps "yday" to month.
|
|
|
|
* There are at least 10 variants, some of them are:
|
|
|
|
* mon = (yday + 31) * 15 / 459
|
|
|
|
* mon = (yday + 31) * 17 / 520
|
|
|
|
* mon = (yday + 31) * 20 / 612
|
|
|
|
*/
|
|
|
|
|
|
|
|
mon = (yday + 31) * 10 / 306;
|
2003-11-13 14:14:05 +08:00
|
|
|
|
2008-04-13 21:33:12 +08:00
|
|
|
/* the Gauss' formula that evaluates days before the month */
|
|
|
|
|
|
|
|
mday = yday - (367 * mon / 12 - 30) + 1;
|
2003-11-13 14:14:05 +08:00
|
|
|
|
|
|
|
if (yday >= 306) {
|
2004-09-28 16:34:51 +08:00
|
|
|
|
2008-04-13 21:33:12 +08:00
|
|
|
year++;
|
|
|
|
mon -= 10;
|
|
|
|
|
2004-03-30 01:43:58 +08:00
|
|
|
/*
|
2004-04-22 04:13:48 +08:00
|
|
|
* there is no "yday" in Win32 SYSTEMTIME
|
2004-03-30 01:43:58 +08:00
|
|
|
*
|
|
|
|
* yday -= 306;
|
|
|
|
*/
|
|
|
|
|
2008-04-13 21:33:12 +08:00
|
|
|
} else {
|
|
|
|
|
|
|
|
mon += 2;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* there is no "yday" in Win32 SYSTEMTIME
|
|
|
|
*
|
|
|
|
* yday += 31 + 28 + leap;
|
|
|
|
*/
|
2003-11-13 14:14:05 +08:00
|
|
|
}
|
|
|
|
|
2004-03-16 15:10:12 +08:00
|
|
|
tp->ngx_tm_sec = (ngx_tm_sec_t) sec;
|
|
|
|
tp->ngx_tm_min = (ngx_tm_min_t) min;
|
|
|
|
tp->ngx_tm_hour = (ngx_tm_hour_t) hour;
|
|
|
|
tp->ngx_tm_mday = (ngx_tm_mday_t) mday;
|
|
|
|
tp->ngx_tm_mon = (ngx_tm_mon_t) mon;
|
|
|
|
tp->ngx_tm_year = (ngx_tm_year_t) year;
|
|
|
|
tp->ngx_tm_wday = (ngx_tm_wday_t) wday;
|
2003-11-13 14:14:05 +08:00
|
|
|
}
|
2008-08-11 23:28:15 +08:00
|
|
|
|
|
|
|
|
|
|
|
time_t
|
|
|
|
ngx_next_time(time_t when)
|
|
|
|
{
|
|
|
|
time_t now, next;
|
|
|
|
struct tm tm;
|
|
|
|
|
|
|
|
now = ngx_time();
|
|
|
|
|
|
|
|
ngx_libc_localtime(now, &tm);
|
|
|
|
|
|
|
|
tm.tm_hour = (int) (when / 3600);
|
|
|
|
when %= 3600;
|
|
|
|
tm.tm_min = (int) (when / 60);
|
|
|
|
tm.tm_sec = (int) (when % 60);
|
|
|
|
|
|
|
|
next = mktime(&tm);
|
|
|
|
|
|
|
|
if (next == -1) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (next - now > 0) {
|
|
|
|
return next;
|
|
|
|
}
|
|
|
|
|
|
|
|
tm.tm_mday++;
|
|
|
|
|
|
|
|
/* mktime() should normalize a date (Jan 32, etc) */
|
|
|
|
|
|
|
|
next = mktime(&tm);
|
|
|
|
|
|
|
|
if (next != -1) {
|
|
|
|
return next;
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|