nginx/src/core/ngx_times.c

215 lines
5.7 KiB
C
Raw Normal View History

2003-11-11 05:09:22 +08:00
#include <ngx_config.h>
#include <ngx_core.h>
2004-02-24 04:57:12 +08:00
#if (NGX_THREADS)
static ngx_mutex_t *ngx_time_mutex;
#endif
2003-12-04 22:53:00 +08:00
time_t ngx_cached_time;
ngx_epoch_msec_t ngx_elapsed_msec;
2003-12-06 01:07:27 +08:00
ngx_epoch_msec_t ngx_old_elapsed_msec;
2003-12-04 22:53:00 +08:00
ngx_epoch_msec_t ngx_start_msec;
2003-11-11 05:09:22 +08:00
2003-12-04 22:53:00 +08:00
ngx_tm_t ngx_cached_gmtime;
2003-11-13 14:14:05 +08:00
2003-12-06 01:07:27 +08:00
static char cached_err_log_time[] = "1970/09/28 12:00:00";
ngx_str_t ngx_cached_err_log_time;
2003-11-13 14:14:05 +08:00
2003-12-06 01:07:27 +08:00
static char cached_http_time[] = "Mon, 28 Sep 1970 06:00:00 GMT";
ngx_str_t ngx_cached_http_time;
2003-11-11 05:09:22 +08:00
2003-12-06 01:07:27 +08:00
static char cached_http_log_time[] = "28/Sep/1970:12:00:00";
ngx_str_t ngx_cached_http_log_time;
2003-11-11 05:09:22 +08:00
2003-11-13 14:14:05 +08:00
static char *week[] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fir", "Sat" };
static char *months[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
2003-11-11 05:09:22 +08:00
2003-11-26 04:44:56 +08:00
void ngx_time_init()
2003-11-11 05:09:22 +08:00
{
2003-11-13 14:14:05 +08:00
struct timeval tv;
2003-11-11 05:09:22 +08:00
2003-11-13 14:14:05 +08:00
ngx_memzero(&ngx_cached_gmtime, sizeof(ngx_tm_t));
#ifdef ngx_tm_zone
ngx_cached_gmtime.ngx_tm_zone = "GMT";
#endif
ngx_cached_err_log_time.data = cached_err_log_time;
2003-11-11 05:09:22 +08:00
ngx_cached_http_time.data = cached_http_time;
ngx_cached_http_log_time.data = cached_http_log_time;
2004-01-30 05:45:01 +08:00
ngx_cached_time = 0;
2003-11-11 05:09:22 +08:00
2003-11-13 14:14:05 +08:00
ngx_gettimeofday(&tv);
2004-01-30 05:45:01 +08:00
2003-12-04 22:53:00 +08:00
ngx_start_msec = tv.tv_sec * 1000 + tv.tv_usec / 1000;
2003-12-06 01:07:27 +08:00
ngx_old_elapsed_msec = 0;
2003-12-04 22:53:00 +08:00
ngx_elapsed_msec = 0;
2004-01-06 04:55:48 +08:00
ngx_time_update(tv.tv_sec);
2004-02-26 04:16:15 +08:00
}
#if (NGX_THREADS)
2004-02-24 04:57:12 +08:00
2004-02-26 04:16:15 +08:00
ngx_int_t ngx_time_mutex_init(ngx_log_t *log)
{
if (!(ngx_time_mutex = ngx_mutex_init(log, NGX_MUTEX_LIGHT))) {
return NGX_ERROR;
2004-02-24 04:57:12 +08:00
}
2004-02-26 04:16:15 +08:00
return NGX_OK;
2003-11-13 14:14:05 +08:00
}
2003-11-13 01:25:12 +08:00
2004-02-26 04:16:15 +08:00
#endif
2003-11-13 01:25:12 +08:00
2004-01-06 04:55:48 +08:00
void ngx_time_update(time_t s)
2003-11-13 14:14:05 +08:00
{
ngx_tm_t tm;
2003-11-11 05:09:22 +08:00
2004-01-06 04:55:48 +08:00
if (ngx_cached_time == s) {
return;
}
2004-02-26 04:16:15 +08:00
#if (NGX_THREADS)
if (ngx_time_mutex) {
if (ngx_mutex_trylock(ngx_time_mutex) != NGX_OK) {
return;
}
2004-02-24 04:57:12 +08:00
}
#endif
2004-01-06 04:55:48 +08:00
ngx_cached_time = s;
2003-11-13 14:14:05 +08:00
ngx_gmtime(ngx_cached_time, &ngx_cached_gmtime);
2003-11-11 05:09:22 +08:00
2003-11-13 14:14:05 +08:00
ngx_cached_http_time.len = ngx_snprintf(ngx_cached_http_time.data,
sizeof("Mon, 28 Sep 1970 06:00:00 GMT"),
"%s, %02d %s %4d %02d:%02d:%02d GMT",
week[ngx_cached_gmtime.ngx_tm_wday],
ngx_cached_gmtime.ngx_tm_mday,
months[ngx_cached_gmtime.ngx_tm_mon - 1],
ngx_cached_gmtime.ngx_tm_year,
ngx_cached_gmtime.ngx_tm_hour,
ngx_cached_gmtime.ngx_tm_min,
ngx_cached_gmtime.ngx_tm_sec);
2003-11-11 05:09:22 +08:00
ngx_localtime(&tm);
2003-11-13 14:14:05 +08:00
ngx_cached_err_log_time.len = ngx_snprintf(ngx_cached_err_log_time.data,
sizeof("1970/09/28 12:00:00"),
"%4d/%02d/%02d %02d:%02d:%02d",
tm.ngx_tm_year, tm.ngx_tm_mon,
tm.ngx_tm_mday, tm.ngx_tm_hour,
tm.ngx_tm_min, tm.ngx_tm_sec);
2003-11-11 05:09:22 +08:00
ngx_cached_http_log_time.len = ngx_snprintf(ngx_cached_http_log_time.data,
sizeof("28/Sep/1970:12:00:00"),
"%02d/%s/%d:%02d:%02d:%02d",
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-02-24 04:57:12 +08:00
2004-02-26 04:16:15 +08:00
#if (NGX_THREADS)
if (ngx_time_mutex) {
ngx_mutex_unlock(ngx_time_mutex);
}
2004-02-24 04:57:12 +08:00
#endif
2003-11-11 05:09:22 +08:00
}
2003-11-13 14:14:05 +08:00
size_t ngx_http_time(char *buf, time_t t)
{
ngx_tm_t tm;
ngx_gmtime(t, &tm);
return ngx_snprintf(buf, sizeof("Mon, 28 Sep 1970 06:00:00 GMT"),
"%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);
}
void ngx_gmtime(time_t t, ngx_tm_t *tp)
{
int sec, min, hour, mday, mon, year, wday, yday, days;
days = t / 86400;
/* Jaunary 1, 1970 was Thursday */
wday = (4 + days) % 7;
t %= 86400;
hour = t / 3600;
t %= 3600;
2003-12-09 04:48:12 +08:00
min = t / 60;
2003-11-13 14:14:05 +08:00
sec = t % 60;
/* the algorithm based on Gauss's formula */
2003-12-09 04:48:12 +08:00
2003-11-13 14:14:05 +08:00
days = days - (31 + 28) + 719527;
year = days * 400 / (365 * 400 + 100 - 4 + 1);
yday = days - (365 * year + year / 4 - year / 100 + year / 400);
mon = (yday + 31) * 12 / 367;
mday = yday - (mon * 367 / 12 - 31);
mon += 2;
if (yday >= 306) {
yday -= 306;
year++;
mon -= 12;
if (mday == 0) {
/* Jaunary 31 */
mon = 1;
mday = 31;
} else if (mon == 2) {
if ((year % 4 == 0) && (year % 100 || (year % 400 == 0))) {
if (mday > 29) {
mon = 3;
mday -= 29;
}
} else if (mday > 28) {
mon = 3;
mday -= 28;
}
}
} else {
yday += 31 + 28;
if ((year % 4 == 0) && (year % 100 || (year % 400 == 0))) {
yday++;
}
}
tp->ngx_tm_sec = sec;
tp->ngx_tm_min = min;
tp->ngx_tm_hour = hour;
tp->ngx_tm_mday = mday;
tp->ngx_tm_mon = mon;
tp->ngx_tm_year = year;
tp->ngx_tm_wday = wday;
}