Mercurial > hg > nginx-quic
comparison src/core/ngx_times.c @ 182:3c49eaf3f522
nginx-0.0.1-2003-11-13-09:14:05 import
author | Igor Sysoev <igor@sysoev.ru> |
---|---|
date | Thu, 13 Nov 2003 06:14:05 +0000 |
parents | 811bdd2af794 |
children | 02a715e85df1 |
comparison
equal
deleted
inserted
replaced
181:811bdd2af794 | 182:3c49eaf3f522 |
---|---|
2 #include <ngx_config.h> | 2 #include <ngx_config.h> |
3 #include <ngx_core.h> | 3 #include <ngx_core.h> |
4 | 4 |
5 | 5 |
6 time_t ngx_cached_time; | 6 time_t ngx_cached_time; |
7 | |
8 ngx_tm_t ngx_cached_gmtime; | |
9 | |
10 static char cached_err_log_time[] = "1970/09/28 12:00:00"; | |
11 ngx_str_t ngx_cached_err_log_time; | |
7 | 12 |
8 static char cached_http_time[] = "Mon, 28 Sep 1970 06:00:00 GMT"; | 13 static char cached_http_time[] = "Mon, 28 Sep 1970 06:00:00 GMT"; |
9 ngx_str_t ngx_cached_http_time; | 14 ngx_str_t ngx_cached_http_time; |
10 | 15 |
11 static char cached_http_log_time[] = "28/Sep/1970:12:00:00"; | 16 static char cached_http_log_time[] = "28/Sep/1970:12:00:00"; |
12 ngx_str_t ngx_cached_http_log_time; | 17 ngx_str_t ngx_cached_http_log_time; |
13 | 18 |
14 | 19 |
20 static char *week[] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fir", "Sat" }; | |
21 static char *months[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", | |
22 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" }; | |
23 | |
24 | |
25 void ngx_init_time() | |
26 { | |
27 struct timeval tv; | |
28 | |
29 ngx_memzero(&ngx_cached_gmtime, sizeof(ngx_tm_t)); | |
30 #ifdef ngx_tm_zone | |
31 ngx_cached_gmtime.ngx_tm_zone = "GMT"; | |
32 #endif | |
33 | |
34 ngx_cached_err_log_time.data = cached_err_log_time; | |
35 ngx_cached_http_time.data = cached_http_time; | |
36 ngx_cached_http_log_time.data = cached_http_log_time; | |
37 | |
38 ngx_gettimeofday(&tv); | |
39 ngx_cached_time = tv.tv_sec; | |
40 ngx_time_update(); | |
41 } | |
42 | |
43 | |
15 time_t ngx_time() | 44 time_t ngx_time() |
16 { | 45 { |
17 return ngx_cached_time; | 46 return ngx_cached_time; |
18 } | 47 } |
19 | 48 |
20 | 49 |
21 /* TODO: | |
22 * cache ngx_tm_t | |
23 * write own gmtime() | |
24 * remove strftime() | |
25 * we can remove localtime_r | |
26 */ | |
27 | |
28 void ngx_time_update() | 50 void ngx_time_update() |
29 { | 51 { |
30 ngx_tm_t *tp, tm; | 52 ngx_tm_t tm; |
31 static char *months[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", | |
32 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" }; | |
33 | 53 |
34 /* STUB: need to move to ngx_init_time() */ | 54 ngx_gmtime(ngx_cached_time, &ngx_cached_gmtime); |
35 ngx_cached_http_time.data = cached_http_time; | |
36 ngx_cached_http_log_time.data = cached_http_log_time; | |
37 | 55 |
38 #if 0 | 56 ngx_cached_http_time.len = ngx_snprintf(ngx_cached_http_time.data, |
39 | 57 sizeof("Mon, 28 Sep 1970 06:00:00 GMT"), |
40 days = sec / 86400; | 58 "%s, %02d %s %4d %02d:%02d:%02d GMT", |
41 days = days - 31 - 28 + 719527; | 59 week[ngx_cached_gmtime.ngx_tm_wday], |
42 | 60 ngx_cached_gmtime.ngx_tm_mday, |
43 year = days * 400 / (365 * 400 + 100 - 4 + 1); | 61 months[ngx_cached_gmtime.ngx_tm_mon - 1], |
44 yday = days - (365 * year + year / 4 - year / 100 + year / 400); | 62 ngx_cached_gmtime.ngx_tm_year, |
45 | 63 ngx_cached_gmtime.ngx_tm_hour, |
46 month = (yday + 30) * 12 / 367; | 64 ngx_cached_gmtime.ngx_tm_min, |
47 mday = yday - (month * 367 / 12 - 31); | 65 ngx_cached_gmtime.ngx_tm_sec); |
48 | |
49 if (++month > 11) { | |
50 month -= 12; | |
51 year++; | |
52 } | |
53 | |
54 #endif | |
55 | |
56 tp = gmtime(&ngx_cached_time); | |
57 | |
58 ngx_cached_http_time.len = strftime(ngx_cached_http_time.data, | |
59 sizeof("Mon, 28 Sep 1970 06:00:00 GMT"), | |
60 "%a, %d %b %Y %H:%M:%S GMT", tp); | |
61 | |
62 | 66 |
63 ngx_localtime(&tm); | 67 ngx_localtime(&tm); |
68 | |
69 ngx_cached_err_log_time.len = ngx_snprintf(ngx_cached_err_log_time.data, | |
70 sizeof("1970/09/28 12:00:00"), | |
71 "%4d/%02d/%02d %02d:%02d:%02d", | |
72 tm.ngx_tm_year, tm.ngx_tm_mon, | |
73 tm.ngx_tm_mday, tm.ngx_tm_hour, | |
74 tm.ngx_tm_min, tm.ngx_tm_sec); | |
64 | 75 |
65 ngx_cached_http_log_time.len = ngx_snprintf(ngx_cached_http_log_time.data, | 76 ngx_cached_http_log_time.len = ngx_snprintf(ngx_cached_http_log_time.data, |
66 sizeof("28/Sep/1970:12:00:00"), | 77 sizeof("28/Sep/1970:12:00:00"), |
67 "%02d/%s/%d:%02d:%02d:%02d", | 78 "%02d/%s/%d:%02d:%02d:%02d", |
68 tm.ngx_tm_mday, | 79 tm.ngx_tm_mday, |
70 tm.ngx_tm_year, | 81 tm.ngx_tm_year, |
71 tm.ngx_tm_hour, | 82 tm.ngx_tm_hour, |
72 tm.ngx_tm_min, | 83 tm.ngx_tm_min, |
73 tm.ngx_tm_sec); | 84 tm.ngx_tm_sec); |
74 } | 85 } |
86 | |
87 | |
88 size_t ngx_http_time(char *buf, time_t t) | |
89 { | |
90 ngx_tm_t tm; | |
91 | |
92 ngx_gmtime(t, &tm); | |
93 | |
94 return ngx_snprintf(buf, sizeof("Mon, 28 Sep 1970 06:00:00 GMT"), | |
95 "%s, %02d %s %4d %02d:%02d:%02d GMT", | |
96 week[tm.ngx_tm_wday], | |
97 tm.ngx_tm_mday, | |
98 months[tm.ngx_tm_mon - 1], | |
99 tm.ngx_tm_year, | |
100 tm.ngx_tm_hour, | |
101 tm.ngx_tm_min, | |
102 tm.ngx_tm_sec); | |
103 } | |
104 | |
105 | |
106 void ngx_gmtime(time_t t, ngx_tm_t *tp) | |
107 { | |
108 int sec, min, hour, mday, mon, year, wday, yday, days; | |
109 | |
110 days = t / 86400; | |
111 | |
112 /* Jaunary 1, 1970 was Thursday */ | |
113 wday = (4 + days) % 7; | |
114 | |
115 t %= 86400; | |
116 hour = t / 3600; | |
117 t %= 3600; | |
118 min = t / 60; | |
119 sec = t % 60; | |
120 | |
121 /* the algorithm based on Gauss's formula */ | |
122 | |
123 days = days - (31 + 28) + 719527; | |
124 | |
125 year = days * 400 / (365 * 400 + 100 - 4 + 1); | |
126 yday = days - (365 * year + year / 4 - year / 100 + year / 400); | |
127 | |
128 mon = (yday + 31) * 12 / 367; | |
129 mday = yday - (mon * 367 / 12 - 31); | |
130 | |
131 mon += 2; | |
132 | |
133 if (yday >= 306) { | |
134 yday -= 306; | |
135 year++; | |
136 mon -= 12; | |
137 | |
138 if (mday == 0) { | |
139 /* Jaunary 31 */ | |
140 mon = 1; | |
141 mday = 31; | |
142 | |
143 } else if (mon == 2) { | |
144 | |
145 if ((year % 4 == 0) && (year % 100 || (year % 400 == 0))) { | |
146 if (mday > 29) { | |
147 mon = 3; | |
148 mday -= 29; | |
149 } | |
150 | |
151 } else if (mday > 28) { | |
152 mon = 3; | |
153 mday -= 28; | |
154 } | |
155 } | |
156 | |
157 } else { | |
158 yday += 31 + 28; | |
159 | |
160 if ((year % 4 == 0) && (year % 100 || (year % 400 == 0))) { | |
161 yday++; | |
162 } | |
163 } | |
164 | |
165 tp->ngx_tm_sec = sec; | |
166 tp->ngx_tm_min = min; | |
167 tp->ngx_tm_hour = hour; | |
168 tp->ngx_tm_mday = mday; | |
169 tp->ngx_tm_mon = mon; | |
170 tp->ngx_tm_year = year; | |
171 tp->ngx_tm_wday = wday; | |
172 } |