Mercurial > hg > nginx-quic
annotate src/core/ngx_times.c @ 2887:78277989e9ea
bump version
author | Igor Sysoev <igor@sysoev.ru> |
---|---|
date | Mon, 25 May 2009 15:24:20 +0000 |
parents | bf38420c9e25 |
children | d4c4cfdffe30 e1409e56ba7c |
rev | line source |
---|---|
441
da8c5707af39
nginx-0.1.0-2004-09-28-12:34:51 import; set copyright and remove unused files
Igor Sysoev <igor@sysoev.ru>
parents:
433
diff
changeset
|
1 |
da8c5707af39
nginx-0.1.0-2004-09-28-12:34:51 import; set copyright and remove unused files
Igor Sysoev <igor@sysoev.ru>
parents:
433
diff
changeset
|
2 /* |
444
42d11f017717
nginx-0.1.0-2004-09-29-20:00:49 import; remove years from copyright
Igor Sysoev <igor@sysoev.ru>
parents:
441
diff
changeset
|
3 * Copyright (C) Igor Sysoev |
441
da8c5707af39
nginx-0.1.0-2004-09-28-12:34:51 import; set copyright and remove unused files
Igor Sysoev <igor@sysoev.ru>
parents:
433
diff
changeset
|
4 */ |
da8c5707af39
nginx-0.1.0-2004-09-28-12:34:51 import; set copyright and remove unused files
Igor Sysoev <igor@sysoev.ru>
parents:
433
diff
changeset
|
5 |
178
a8ff48d26cca
nginx-0.0.1-2003-11-11-00:09:22 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
6 |
a8ff48d26cca
nginx-0.0.1-2003-11-11-00:09:22 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
7 #include <ngx_config.h> |
a8ff48d26cca
nginx-0.0.1-2003-11-11-00:09:22 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
8 #include <ngx_core.h> |
a8ff48d26cca
nginx-0.0.1-2003-11-11-00:09:22 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
9 |
a8ff48d26cca
nginx-0.0.1-2003-11-11-00:09:22 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
10 |
370
54f76b0b8dca
nginx-0.0.7-2004-06-27-22:01:57 import
Igor Sysoev <igor@sysoev.ru>
parents:
358
diff
changeset
|
11 /* |
563 | 12 * 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. | |
370
54f76b0b8dca
nginx-0.0.7-2004-06-27-22:01:57 import
Igor Sysoev <igor@sysoev.ru>
parents:
358
diff
changeset
|
18 */ |
54f76b0b8dca
nginx-0.0.7-2004-06-27-22:01:57 import
Igor Sysoev <igor@sysoev.ru>
parents:
358
diff
changeset
|
19 |
563 | 20 #define NGX_TIME_SLOTS 64 |
370
54f76b0b8dca
nginx-0.0.7-2004-06-27-22:01:57 import
Igor Sysoev <igor@sysoev.ru>
parents:
358
diff
changeset
|
21 |
932
1b8111df8051
the initial slot is out of array
Igor Sysoev <igor@sysoev.ru>
parents:
891
diff
changeset
|
22 static ngx_uint_t slot; |
563 | 23 static ngx_atomic_t ngx_time_lock; |
370
54f76b0b8dca
nginx-0.0.7-2004-06-27-22:01:57 import
Igor Sysoev <igor@sysoev.ru>
parents:
358
diff
changeset
|
24 |
563 | 25 volatile ngx_msec_t ngx_current_msec; |
26 volatile ngx_time_t *ngx_cached_time; | |
27 volatile ngx_str_t ngx_cached_err_log_time; | |
28 volatile ngx_str_t ngx_cached_http_time; | |
29 volatile ngx_str_t ngx_cached_http_log_time; | |
182
3c49eaf3f522
nginx-0.0.1-2003-11-13-09:14:05 import
Igor Sysoev <igor@sysoev.ru>
parents:
181
diff
changeset
|
30 |
563 | 31 static ngx_time_t cached_time[NGX_TIME_SLOTS]; |
32 static u_char cached_err_log_time[NGX_TIME_SLOTS] | |
33 [sizeof("1970/09/28 12:00:00")]; | |
34 static u_char cached_http_time[NGX_TIME_SLOTS] | |
35 [sizeof("Mon, 28 Sep 1970 06:00:00 GMT")]; | |
36 static u_char cached_http_log_time[NGX_TIME_SLOTS] | |
37 [sizeof("28/Sep/1970:12:00:00 +0600")]; | |
178
a8ff48d26cca
nginx-0.0.1-2003-11-11-00:09:22 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
38 |
a8ff48d26cca
nginx-0.0.1-2003-11-11-00:09:22 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
39 |
326
8733703a37f3
nginx-0.0.3-2004-04-26-00:13:21 import
Igor Sysoev <igor@sysoev.ru>
parents:
324
diff
changeset
|
40 static char *week[] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" }; |
182
3c49eaf3f522
nginx-0.0.1-2003-11-13-09:14:05 import
Igor Sysoev <igor@sysoev.ru>
parents:
181
diff
changeset
|
41 static char *months[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", |
3c49eaf3f522
nginx-0.0.1-2003-11-13-09:14:05 import
Igor Sysoev <igor@sysoev.ru>
parents:
181
diff
changeset
|
42 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" }; |
3c49eaf3f522
nginx-0.0.1-2003-11-13-09:14:05 import
Igor Sysoev <igor@sysoev.ru>
parents:
181
diff
changeset
|
43 |
501 | 44 void |
45 ngx_time_init(void) | |
182
3c49eaf3f522
nginx-0.0.1-2003-11-13-09:14:05 import
Igor Sysoev <igor@sysoev.ru>
parents:
181
diff
changeset
|
46 { |
370
54f76b0b8dca
nginx-0.0.7-2004-06-27-22:01:57 import
Igor Sysoev <igor@sysoev.ru>
parents:
358
diff
changeset
|
47 ngx_cached_err_log_time.len = sizeof("1970/09/28 12:00:00") - 1; |
54f76b0b8dca
nginx-0.0.7-2004-06-27-22:01:57 import
Igor Sysoev <igor@sysoev.ru>
parents:
358
diff
changeset
|
48 ngx_cached_http_time.len = sizeof("Mon, 28 Sep 1970 06:00:00 GMT") - 1; |
54f76b0b8dca
nginx-0.0.7-2004-06-27-22:01:57 import
Igor Sysoev <igor@sysoev.ru>
parents:
358
diff
changeset
|
49 ngx_cached_http_log_time.len = sizeof("28/Sep/1970:12:00:00 +0600") - 1; |
323
ba876b26b76d
nginx-0.0.3-2004-04-21-22:54:33 import
Igor Sysoev <igor@sysoev.ru>
parents:
297
diff
changeset
|
50 |
370
54f76b0b8dca
nginx-0.0.7-2004-06-27-22:01:57 import
Igor Sysoev <igor@sysoev.ru>
parents:
358
diff
changeset
|
51 ngx_cached_time = &cached_time[0]; |
205
4a9a2b1dd6fa
nginx-0.0.1-2003-12-04-17:53:00 import
Igor Sysoev <igor@sysoev.ru>
parents:
195
diff
changeset
|
52 |
563 | 53 ngx_time_update(0, 0); |
270
7bb9562216ce
nginx-0.0.2-2004-02-25-23:16:15 import
Igor Sysoev <igor@sysoev.ru>
parents:
266
diff
changeset
|
54 } |
266
5238e93961a1
nginx-0.0.2-2004-02-23-23:57:12 import
Igor Sysoev <igor@sysoev.ru>
parents:
245
diff
changeset
|
55 |
270
7bb9562216ce
nginx-0.0.2-2004-02-25-23:16:15 import
Igor Sysoev <igor@sysoev.ru>
parents:
266
diff
changeset
|
56 |
563 | 57 void |
58 ngx_time_update(time_t sec, ngx_uint_t msec) | |
270
7bb9562216ce
nginx-0.0.2-2004-02-25-23:16:15 import
Igor Sysoev <igor@sysoev.ru>
parents:
266
diff
changeset
|
59 { |
563 | 60 u_char *p0, *p1, *p2; |
61 ngx_tm_t tm, gmt; | |
62 ngx_time_t *tp; | |
63 struct timeval tv; | |
182
3c49eaf3f522
nginx-0.0.1-2003-11-13-09:14:05 import
Igor Sysoev <igor@sysoev.ru>
parents:
181
diff
changeset
|
64 |
563 | 65 if (!ngx_trylock(&ngx_time_lock)) { |
271
e16dfb9b9afa
nginx-0.0.2-2004-02-26-20:10:01 import
Igor Sysoev <igor@sysoev.ru>
parents:
270
diff
changeset
|
66 return; |
266
5238e93961a1
nginx-0.0.2-2004-02-23-23:57:12 import
Igor Sysoev <igor@sysoev.ru>
parents:
245
diff
changeset
|
67 } |
370
54f76b0b8dca
nginx-0.0.7-2004-06-27-22:01:57 import
Igor Sysoev <igor@sysoev.ru>
parents:
358
diff
changeset
|
68 |
563 | 69 if (sec == 0) { |
70 ngx_gettimeofday(&tv); | |
71 | |
72 sec = tv.tv_sec; | |
73 msec = tv.tv_usec / 1000; | |
74 } | |
75 | |
76 ngx_current_msec = (ngx_msec_t) sec * 1000 + msec; | |
77 | |
78 tp = &cached_time[slot]; | |
266
5238e93961a1
nginx-0.0.2-2004-02-23-23:57:12 import
Igor Sysoev <igor@sysoev.ru>
parents:
245
diff
changeset
|
79 |
563 | 80 if (tp->sec == sec) { |
891
8a3ba663be1a
slot number must be incremented once per second only,
Igor Sysoev <igor@sysoev.ru>
parents:
563
diff
changeset
|
81 tp->msec = msec; |
563 | 82 ngx_unlock(&ngx_time_lock); |
83 return; | |
84 } | |
370
54f76b0b8dca
nginx-0.0.7-2004-06-27-22:01:57 import
Igor Sysoev <igor@sysoev.ru>
parents:
358
diff
changeset
|
85 |
1086
a62d30719ea1
fix segfault if nginx was build with -O2 -fomit-frame-pointer,
Igor Sysoev <igor@sysoev.ru>
parents:
1081
diff
changeset
|
86 if (slot == NGX_TIME_SLOTS - 1) { |
891
8a3ba663be1a
slot number must be incremented once per second only,
Igor Sysoev <igor@sysoev.ru>
parents:
563
diff
changeset
|
87 slot = 0; |
8a3ba663be1a
slot number must be incremented once per second only,
Igor Sysoev <igor@sysoev.ru>
parents:
563
diff
changeset
|
88 } else { |
8a3ba663be1a
slot number must be incremented once per second only,
Igor Sysoev <igor@sysoev.ru>
parents:
563
diff
changeset
|
89 slot++; |
8a3ba663be1a
slot number must be incremented once per second only,
Igor Sysoev <igor@sysoev.ru>
parents:
563
diff
changeset
|
90 } |
8a3ba663be1a
slot number must be incremented once per second only,
Igor Sysoev <igor@sysoev.ru>
parents:
563
diff
changeset
|
91 |
8a3ba663be1a
slot number must be incremented once per second only,
Igor Sysoev <igor@sysoev.ru>
parents:
563
diff
changeset
|
92 tp = &cached_time[slot]; |
8a3ba663be1a
slot number must be incremented once per second only,
Igor Sysoev <igor@sysoev.ru>
parents:
563
diff
changeset
|
93 |
563 | 94 tp->sec = sec; |
891
8a3ba663be1a
slot number must be incremented once per second only,
Igor Sysoev <igor@sysoev.ru>
parents:
563
diff
changeset
|
95 tp->msec = msec; |
563 | 96 |
97 ngx_gmtime(sec, &gmt); | |
181
811bdd2af794
nginx-0.0.1-2003-11-12-20:25:12 import
Igor Sysoev <igor@sysoev.ru>
parents:
180
diff
changeset
|
98 |
323
ba876b26b76d
nginx-0.0.3-2004-04-21-22:54:33 import
Igor Sysoev <igor@sysoev.ru>
parents:
297
diff
changeset
|
99 |
1081
4cf9d9b644f7
fix segfault if nginx was build with -O2 -fomit-frame-pointer
Igor Sysoev <igor@sysoev.ru>
parents:
932
diff
changeset
|
100 p0 = &cached_http_time[slot][0]; |
323
ba876b26b76d
nginx-0.0.3-2004-04-21-22:54:33 import
Igor Sysoev <igor@sysoev.ru>
parents:
297
diff
changeset
|
101 |
563 | 102 (void) ngx_sprintf(p0, "%s, %02d %s %4d %02d:%02d:%02d GMT", |
103 week[gmt.ngx_tm_wday], gmt.ngx_tm_mday, | |
104 months[gmt.ngx_tm_mon - 1], gmt.ngx_tm_year, | |
105 gmt.ngx_tm_hour, gmt.ngx_tm_min, gmt.ngx_tm_sec); | |
323
ba876b26b76d
nginx-0.0.3-2004-04-21-22:54:33 import
Igor Sysoev <igor@sysoev.ru>
parents:
297
diff
changeset
|
106 |
469 | 107 #if (NGX_HAVE_GETTIMEZONE) |
358
0a03c921c81d
nginx-0.0.7-2004-06-17-21:18:53 import
Igor Sysoev <igor@sysoev.ru>
parents:
326
diff
changeset
|
108 |
563 | 109 tp->gmtoff = ngx_gettimezone(); |
110 ngx_gmtime(sec + tp->gmtoff * 60, &tm); | |
323
ba876b26b76d
nginx-0.0.3-2004-04-21-22:54:33 import
Igor Sysoev <igor@sysoev.ru>
parents:
297
diff
changeset
|
111 |
469 | 112 #elif (NGX_HAVE_GMTOFF) |
358
0a03c921c81d
nginx-0.0.7-2004-06-17-21:18:53 import
Igor Sysoev <igor@sysoev.ru>
parents:
326
diff
changeset
|
113 |
563 | 114 ngx_localtime(sec, &tm); |
115 tp->gmtoff = (ngx_int_t) (tm.ngx_tm_gmtoff / 60); | |
323
ba876b26b76d
nginx-0.0.3-2004-04-21-22:54:33 import
Igor Sysoev <igor@sysoev.ru>
parents:
297
diff
changeset
|
116 |
ba876b26b76d
nginx-0.0.3-2004-04-21-22:54:33 import
Igor Sysoev <igor@sysoev.ru>
parents:
297
diff
changeset
|
117 #else |
178
a8ff48d26cca
nginx-0.0.1-2003-11-11-00:09:22 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
118 |
563 | 119 ngx_localtime(sec, &tm); |
120 tp->gmtoff = ngx_timezone(tm.ngx_tm_isdst); | |
178
a8ff48d26cca
nginx-0.0.1-2003-11-11-00:09:22 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
121 |
323
ba876b26b76d
nginx-0.0.3-2004-04-21-22:54:33 import
Igor Sysoev <igor@sysoev.ru>
parents:
297
diff
changeset
|
122 #endif |
ba876b26b76d
nginx-0.0.3-2004-04-21-22:54:33 import
Igor Sysoev <igor@sysoev.ru>
parents:
297
diff
changeset
|
123 |
ba876b26b76d
nginx-0.0.3-2004-04-21-22:54:33 import
Igor Sysoev <igor@sysoev.ru>
parents:
297
diff
changeset
|
124 |
1081
4cf9d9b644f7
fix segfault if nginx was build with -O2 -fomit-frame-pointer
Igor Sysoev <igor@sysoev.ru>
parents:
932
diff
changeset
|
125 p1 = &cached_err_log_time[slot][0]; |
323
ba876b26b76d
nginx-0.0.3-2004-04-21-22:54:33 import
Igor Sysoev <igor@sysoev.ru>
parents:
297
diff
changeset
|
126 |
563 | 127 (void) ngx_sprintf(p1, "%4d/%02d/%02d %02d:%02d:%02d", |
517 | 128 tm.ngx_tm_year, tm.ngx_tm_mon, |
129 tm.ngx_tm_mday, tm.ngx_tm_hour, | |
130 tm.ngx_tm_min, tm.ngx_tm_sec); | |
182
3c49eaf3f522
nginx-0.0.1-2003-11-13-09:14:05 import
Igor Sysoev <igor@sysoev.ru>
parents:
181
diff
changeset
|
131 |
323
ba876b26b76d
nginx-0.0.3-2004-04-21-22:54:33 import
Igor Sysoev <igor@sysoev.ru>
parents:
297
diff
changeset
|
132 |
1081
4cf9d9b644f7
fix segfault if nginx was build with -O2 -fomit-frame-pointer
Igor Sysoev <igor@sysoev.ru>
parents:
932
diff
changeset
|
133 p2 = &cached_http_log_time[slot][0]; |
323
ba876b26b76d
nginx-0.0.3-2004-04-21-22:54:33 import
Igor Sysoev <igor@sysoev.ru>
parents:
297
diff
changeset
|
134 |
563 | 135 (void) ngx_sprintf(p2, "%02d/%s/%d:%02d:%02d:%02d %c%02d%02d", |
517 | 136 tm.ngx_tm_mday, months[tm.ngx_tm_mon - 1], |
137 tm.ngx_tm_year, tm.ngx_tm_hour, | |
138 tm.ngx_tm_min, tm.ngx_tm_sec, | |
563 | 139 tp->gmtoff < 0 ? '-' : '+', |
140 ngx_abs(tp->gmtoff / 60), ngx_abs(tp->gmtoff % 60)); | |
323
ba876b26b76d
nginx-0.0.3-2004-04-21-22:54:33 import
Igor Sysoev <igor@sysoev.ru>
parents:
297
diff
changeset
|
141 |
266
5238e93961a1
nginx-0.0.2-2004-02-23-23:57:12 import
Igor Sysoev <igor@sysoev.ru>
parents:
245
diff
changeset
|
142 |
563 | 143 ngx_memory_barrier(); |
266
5238e93961a1
nginx-0.0.2-2004-02-23-23:57:12 import
Igor Sysoev <igor@sysoev.ru>
parents:
245
diff
changeset
|
144 |
563 | 145 ngx_cached_time = tp; |
146 ngx_cached_http_time.data = p0; | |
147 ngx_cached_err_log_time.data = p1; | |
148 ngx_cached_http_log_time.data = p2; | |
149 | |
150 ngx_unlock(&ngx_time_lock); | |
178
a8ff48d26cca
nginx-0.0.1-2003-11-11-00:09:22 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
151 } |
182
3c49eaf3f522
nginx-0.0.1-2003-11-13-09:14:05 import
Igor Sysoev <igor@sysoev.ru>
parents:
181
diff
changeset
|
152 |
3c49eaf3f522
nginx-0.0.1-2003-11-13-09:14:05 import
Igor Sysoev <igor@sysoev.ru>
parents:
181
diff
changeset
|
153 |
501 | 154 u_char * |
155 ngx_http_time(u_char *buf, time_t t) | |
182
3c49eaf3f522
nginx-0.0.1-2003-11-13-09:14:05 import
Igor Sysoev <igor@sysoev.ru>
parents:
181
diff
changeset
|
156 { |
3c49eaf3f522
nginx-0.0.1-2003-11-13-09:14:05 import
Igor Sysoev <igor@sysoev.ru>
parents:
181
diff
changeset
|
157 ngx_tm_t tm; |
3c49eaf3f522
nginx-0.0.1-2003-11-13-09:14:05 import
Igor Sysoev <igor@sysoev.ru>
parents:
181
diff
changeset
|
158 |
3c49eaf3f522
nginx-0.0.1-2003-11-13-09:14:05 import
Igor Sysoev <igor@sysoev.ru>
parents:
181
diff
changeset
|
159 ngx_gmtime(t, &tm); |
3c49eaf3f522
nginx-0.0.1-2003-11-13-09:14:05 import
Igor Sysoev <igor@sysoev.ru>
parents:
181
diff
changeset
|
160 |
455 | 161 return ngx_sprintf(buf, "%s, %02d %s %4d %02d:%02d:%02d GMT", |
162 week[tm.ngx_tm_wday], | |
163 tm.ngx_tm_mday, | |
164 months[tm.ngx_tm_mon - 1], | |
165 tm.ngx_tm_year, | |
166 tm.ngx_tm_hour, | |
167 tm.ngx_tm_min, | |
168 tm.ngx_tm_sec); | |
409
8ac40cae79f0
nginx-0.0.10-2004-08-29-07:55:41 import
Igor Sysoev <igor@sysoev.ru>
parents:
388
diff
changeset
|
169 } |
8ac40cae79f0
nginx-0.0.10-2004-08-29-07:55:41 import
Igor Sysoev <igor@sysoev.ru>
parents:
388
diff
changeset
|
170 |
8ac40cae79f0
nginx-0.0.10-2004-08-29-07:55:41 import
Igor Sysoev <igor@sysoev.ru>
parents:
388
diff
changeset
|
171 |
501 | 172 u_char * |
173 ngx_http_cookie_time(u_char *buf, time_t t) | |
409
8ac40cae79f0
nginx-0.0.10-2004-08-29-07:55:41 import
Igor Sysoev <igor@sysoev.ru>
parents:
388
diff
changeset
|
174 { |
8ac40cae79f0
nginx-0.0.10-2004-08-29-07:55:41 import
Igor Sysoev <igor@sysoev.ru>
parents:
388
diff
changeset
|
175 ngx_tm_t tm; |
8ac40cae79f0
nginx-0.0.10-2004-08-29-07:55:41 import
Igor Sysoev <igor@sysoev.ru>
parents:
388
diff
changeset
|
176 |
8ac40cae79f0
nginx-0.0.10-2004-08-29-07:55:41 import
Igor Sysoev <igor@sysoev.ru>
parents:
388
diff
changeset
|
177 ngx_gmtime(t, &tm); |
8ac40cae79f0
nginx-0.0.10-2004-08-29-07:55:41 import
Igor Sysoev <igor@sysoev.ru>
parents:
388
diff
changeset
|
178 |
8ac40cae79f0
nginx-0.0.10-2004-08-29-07:55:41 import
Igor Sysoev <igor@sysoev.ru>
parents:
388
diff
changeset
|
179 /* |
8ac40cae79f0
nginx-0.0.10-2004-08-29-07:55:41 import
Igor Sysoev <igor@sysoev.ru>
parents:
388
diff
changeset
|
180 * Netscape 3.x does not understand 4-digit years at all and |
8ac40cae79f0
nginx-0.0.10-2004-08-29-07:55:41 import
Igor Sysoev <igor@sysoev.ru>
parents:
388
diff
changeset
|
181 * 2-digit years more than "37" |
8ac40cae79f0
nginx-0.0.10-2004-08-29-07:55:41 import
Igor Sysoev <igor@sysoev.ru>
parents:
388
diff
changeset
|
182 */ |
8ac40cae79f0
nginx-0.0.10-2004-08-29-07:55:41 import
Igor Sysoev <igor@sysoev.ru>
parents:
388
diff
changeset
|
183 |
455 | 184 return ngx_sprintf(buf, |
185 (tm.ngx_tm_year > 2037) ? | |
186 "%s, %02d-%s-%d %02d:%02d:%02d GMT": | |
187 "%s, %02d-%s-%02d %02d:%02d:%02d GMT", | |
188 week[tm.ngx_tm_wday], | |
189 tm.ngx_tm_mday, | |
190 months[tm.ngx_tm_mon - 1], | |
191 (tm.ngx_tm_year > 2037) ? tm.ngx_tm_year: | |
192 tm.ngx_tm_year % 100, | |
193 tm.ngx_tm_hour, | |
194 tm.ngx_tm_min, | |
195 tm.ngx_tm_sec); | |
182
3c49eaf3f522
nginx-0.0.1-2003-11-13-09:14:05 import
Igor Sysoev <igor@sysoev.ru>
parents:
181
diff
changeset
|
196 } |
3c49eaf3f522
nginx-0.0.1-2003-11-13-09:14:05 import
Igor Sysoev <igor@sysoev.ru>
parents:
181
diff
changeset
|
197 |
3c49eaf3f522
nginx-0.0.1-2003-11-13-09:14:05 import
Igor Sysoev <igor@sysoev.ru>
parents:
181
diff
changeset
|
198 |
501 | 199 void |
200 ngx_gmtime(time_t t, ngx_tm_t *tp) | |
182
3c49eaf3f522
nginx-0.0.1-2003-11-13-09:14:05 import
Igor Sysoev <igor@sysoev.ru>
parents:
181
diff
changeset
|
201 { |
1970
5bb2c374cab2
use more straightforward ngx_gmtime() implementation
Igor Sysoev <igor@sysoev.ru>
parents:
1874
diff
changeset
|
202 ngx_int_t yday; |
5bb2c374cab2
use more straightforward ngx_gmtime() implementation
Igor Sysoev <igor@sysoev.ru>
parents:
1874
diff
changeset
|
203 ngx_uint_t n, sec, min, hour, mday, mon, year, wday, days, leap; |
182
3c49eaf3f522
nginx-0.0.1-2003-11-13-09:14:05 import
Igor Sysoev <igor@sysoev.ru>
parents:
181
diff
changeset
|
204 |
1874
085e536d856d
treat time_t as unsigned time
Igor Sysoev <igor@sysoev.ru>
parents:
1640
diff
changeset
|
205 /* the calculation is valid for positive time_t only */ |
1970
5bb2c374cab2
use more straightforward ngx_gmtime() implementation
Igor Sysoev <igor@sysoev.ru>
parents:
1874
diff
changeset
|
206 |
1874
085e536d856d
treat time_t as unsigned time
Igor Sysoev <igor@sysoev.ru>
parents:
1640
diff
changeset
|
207 n = (ngx_uint_t) t; |
085e536d856d
treat time_t as unsigned time
Igor Sysoev <igor@sysoev.ru>
parents:
1640
diff
changeset
|
208 |
085e536d856d
treat time_t as unsigned time
Igor Sysoev <igor@sysoev.ru>
parents:
1640
diff
changeset
|
209 days = n / 86400; |
182
3c49eaf3f522
nginx-0.0.1-2003-11-13-09:14:05 import
Igor Sysoev <igor@sysoev.ru>
parents:
181
diff
changeset
|
210 |
3c49eaf3f522
nginx-0.0.1-2003-11-13-09:14:05 import
Igor Sysoev <igor@sysoev.ru>
parents:
181
diff
changeset
|
211 /* Jaunary 1, 1970 was Thursday */ |
1970
5bb2c374cab2
use more straightforward ngx_gmtime() implementation
Igor Sysoev <igor@sysoev.ru>
parents:
1874
diff
changeset
|
212 |
182
3c49eaf3f522
nginx-0.0.1-2003-11-13-09:14:05 import
Igor Sysoev <igor@sysoev.ru>
parents:
181
diff
changeset
|
213 wday = (4 + days) % 7; |
3c49eaf3f522
nginx-0.0.1-2003-11-13-09:14:05 import
Igor Sysoev <igor@sysoev.ru>
parents:
181
diff
changeset
|
214 |
1874
085e536d856d
treat time_t as unsigned time
Igor Sysoev <igor@sysoev.ru>
parents:
1640
diff
changeset
|
215 n %= 86400; |
085e536d856d
treat time_t as unsigned time
Igor Sysoev <igor@sysoev.ru>
parents:
1640
diff
changeset
|
216 hour = n / 3600; |
085e536d856d
treat time_t as unsigned time
Igor Sysoev <igor@sysoev.ru>
parents:
1640
diff
changeset
|
217 n %= 3600; |
085e536d856d
treat time_t as unsigned time
Igor Sysoev <igor@sysoev.ru>
parents:
1640
diff
changeset
|
218 min = n / 60; |
085e536d856d
treat time_t as unsigned time
Igor Sysoev <igor@sysoev.ru>
parents:
1640
diff
changeset
|
219 sec = n % 60; |
182
3c49eaf3f522
nginx-0.0.1-2003-11-13-09:14:05 import
Igor Sysoev <igor@sysoev.ru>
parents:
181
diff
changeset
|
220 |
1970
5bb2c374cab2
use more straightforward ngx_gmtime() implementation
Igor Sysoev <igor@sysoev.ru>
parents:
1874
diff
changeset
|
221 /* |
5bb2c374cab2
use more straightforward ngx_gmtime() implementation
Igor Sysoev <igor@sysoev.ru>
parents:
1874
diff
changeset
|
222 * the algorithm based on Gauss' formula, |
5bb2c374cab2
use more straightforward ngx_gmtime() implementation
Igor Sysoev <igor@sysoev.ru>
parents:
1874
diff
changeset
|
223 * see src/http/ngx_http_parse_time.c |
5bb2c374cab2
use more straightforward ngx_gmtime() implementation
Igor Sysoev <igor@sysoev.ru>
parents:
1874
diff
changeset
|
224 */ |
208
0b67be7d4489
nginx-0.0.1-2003-12-08-23:48:12 import
Igor Sysoev <igor@sysoev.ru>
parents:
207
diff
changeset
|
225 |
1970
5bb2c374cab2
use more straightforward ngx_gmtime() implementation
Igor Sysoev <igor@sysoev.ru>
parents:
1874
diff
changeset
|
226 /* days since March 1, 1 BC */ |
182
3c49eaf3f522
nginx-0.0.1-2003-11-13-09:14:05 import
Igor Sysoev <igor@sysoev.ru>
parents:
181
diff
changeset
|
227 days = days - (31 + 28) + 719527; |
3c49eaf3f522
nginx-0.0.1-2003-11-13-09:14:05 import
Igor Sysoev <igor@sysoev.ru>
parents:
181
diff
changeset
|
228 |
1970
5bb2c374cab2
use more straightforward ngx_gmtime() implementation
Igor Sysoev <igor@sysoev.ru>
parents:
1874
diff
changeset
|
229 /* |
5bb2c374cab2
use more straightforward ngx_gmtime() implementation
Igor Sysoev <igor@sysoev.ru>
parents:
1874
diff
changeset
|
230 * The "days" should be adjusted to 1 only, however, some March 1st's go |
5bb2c374cab2
use more straightforward ngx_gmtime() implementation
Igor Sysoev <igor@sysoev.ru>
parents:
1874
diff
changeset
|
231 * to previous year, so we adjust them to 2. This causes also shift of the |
5bb2c374cab2
use more straightforward ngx_gmtime() implementation
Igor Sysoev <igor@sysoev.ru>
parents:
1874
diff
changeset
|
232 * last Feburary days to next year, but we catch the case when "yday" |
5bb2c374cab2
use more straightforward ngx_gmtime() implementation
Igor Sysoev <igor@sysoev.ru>
parents:
1874
diff
changeset
|
233 * becomes negative. |
5bb2c374cab2
use more straightforward ngx_gmtime() implementation
Igor Sysoev <igor@sysoev.ru>
parents:
1874
diff
changeset
|
234 */ |
5bb2c374cab2
use more straightforward ngx_gmtime() implementation
Igor Sysoev <igor@sysoev.ru>
parents:
1874
diff
changeset
|
235 |
5bb2c374cab2
use more straightforward ngx_gmtime() implementation
Igor Sysoev <igor@sysoev.ru>
parents:
1874
diff
changeset
|
236 year = (days + 2) * 400 / (365 * 400 + 100 - 4 + 1); |
5bb2c374cab2
use more straightforward ngx_gmtime() implementation
Igor Sysoev <igor@sysoev.ru>
parents:
1874
diff
changeset
|
237 |
182
3c49eaf3f522
nginx-0.0.1-2003-11-13-09:14:05 import
Igor Sysoev <igor@sysoev.ru>
parents:
181
diff
changeset
|
238 yday = days - (365 * year + year / 4 - year / 100 + year / 400); |
3c49eaf3f522
nginx-0.0.1-2003-11-13-09:14:05 import
Igor Sysoev <igor@sysoev.ru>
parents:
181
diff
changeset
|
239 |
1970
5bb2c374cab2
use more straightforward ngx_gmtime() implementation
Igor Sysoev <igor@sysoev.ru>
parents:
1874
diff
changeset
|
240 if (yday < 0) { |
5bb2c374cab2
use more straightforward ngx_gmtime() implementation
Igor Sysoev <igor@sysoev.ru>
parents:
1874
diff
changeset
|
241 leap = (year % 4 == 0) && (year % 100 || (year % 400 == 0)); |
5bb2c374cab2
use more straightforward ngx_gmtime() implementation
Igor Sysoev <igor@sysoev.ru>
parents:
1874
diff
changeset
|
242 yday = 365 + leap + yday; |
5bb2c374cab2
use more straightforward ngx_gmtime() implementation
Igor Sysoev <igor@sysoev.ru>
parents:
1874
diff
changeset
|
243 year--; |
5bb2c374cab2
use more straightforward ngx_gmtime() implementation
Igor Sysoev <igor@sysoev.ru>
parents:
1874
diff
changeset
|
244 } |
182
3c49eaf3f522
nginx-0.0.1-2003-11-13-09:14:05 import
Igor Sysoev <igor@sysoev.ru>
parents:
181
diff
changeset
|
245 |
1970
5bb2c374cab2
use more straightforward ngx_gmtime() implementation
Igor Sysoev <igor@sysoev.ru>
parents:
1874
diff
changeset
|
246 /* |
5bb2c374cab2
use more straightforward ngx_gmtime() implementation
Igor Sysoev <igor@sysoev.ru>
parents:
1874
diff
changeset
|
247 * The empirical formula that maps "yday" to month. |
5bb2c374cab2
use more straightforward ngx_gmtime() implementation
Igor Sysoev <igor@sysoev.ru>
parents:
1874
diff
changeset
|
248 * There are at least 10 variants, some of them are: |
5bb2c374cab2
use more straightforward ngx_gmtime() implementation
Igor Sysoev <igor@sysoev.ru>
parents:
1874
diff
changeset
|
249 * mon = (yday + 31) * 15 / 459 |
5bb2c374cab2
use more straightforward ngx_gmtime() implementation
Igor Sysoev <igor@sysoev.ru>
parents:
1874
diff
changeset
|
250 * mon = (yday + 31) * 17 / 520 |
5bb2c374cab2
use more straightforward ngx_gmtime() implementation
Igor Sysoev <igor@sysoev.ru>
parents:
1874
diff
changeset
|
251 * mon = (yday + 31) * 20 / 612 |
5bb2c374cab2
use more straightforward ngx_gmtime() implementation
Igor Sysoev <igor@sysoev.ru>
parents:
1874
diff
changeset
|
252 */ |
5bb2c374cab2
use more straightforward ngx_gmtime() implementation
Igor Sysoev <igor@sysoev.ru>
parents:
1874
diff
changeset
|
253 |
5bb2c374cab2
use more straightforward ngx_gmtime() implementation
Igor Sysoev <igor@sysoev.ru>
parents:
1874
diff
changeset
|
254 mon = (yday + 31) * 10 / 306; |
5bb2c374cab2
use more straightforward ngx_gmtime() implementation
Igor Sysoev <igor@sysoev.ru>
parents:
1874
diff
changeset
|
255 |
5bb2c374cab2
use more straightforward ngx_gmtime() implementation
Igor Sysoev <igor@sysoev.ru>
parents:
1874
diff
changeset
|
256 /* the Gauss' formula that evaluates days before the month */ |
5bb2c374cab2
use more straightforward ngx_gmtime() implementation
Igor Sysoev <igor@sysoev.ru>
parents:
1874
diff
changeset
|
257 |
5bb2c374cab2
use more straightforward ngx_gmtime() implementation
Igor Sysoev <igor@sysoev.ru>
parents:
1874
diff
changeset
|
258 mday = yday - (367 * mon / 12 - 30) + 1; |
182
3c49eaf3f522
nginx-0.0.1-2003-11-13-09:14:05 import
Igor Sysoev <igor@sysoev.ru>
parents:
181
diff
changeset
|
259 |
3c49eaf3f522
nginx-0.0.1-2003-11-13-09:14:05 import
Igor Sysoev <igor@sysoev.ru>
parents:
181
diff
changeset
|
260 if (yday >= 306) { |
441
da8c5707af39
nginx-0.1.0-2004-09-28-12:34:51 import; set copyright and remove unused files
Igor Sysoev <igor@sysoev.ru>
parents:
433
diff
changeset
|
261 |
1970
5bb2c374cab2
use more straightforward ngx_gmtime() implementation
Igor Sysoev <igor@sysoev.ru>
parents:
1874
diff
changeset
|
262 year++; |
5bb2c374cab2
use more straightforward ngx_gmtime() implementation
Igor Sysoev <igor@sysoev.ru>
parents:
1874
diff
changeset
|
263 mon -= 10; |
5bb2c374cab2
use more straightforward ngx_gmtime() implementation
Igor Sysoev <igor@sysoev.ru>
parents:
1874
diff
changeset
|
264 |
297
ee394e997c77
nginx-0.0.3-2004-03-29-21:43:58 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
265 /* |
324
0ab66f4b6c4d
nginx-0.0.3-2004-04-22-00:13:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
323
diff
changeset
|
266 * there is no "yday" in Win32 SYSTEMTIME |
297
ee394e997c77
nginx-0.0.3-2004-03-29-21:43:58 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
267 * |
ee394e997c77
nginx-0.0.3-2004-03-29-21:43:58 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
268 * yday -= 306; |
ee394e997c77
nginx-0.0.3-2004-03-29-21:43:58 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
269 */ |
ee394e997c77
nginx-0.0.3-2004-03-29-21:43:58 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
270 |
1970
5bb2c374cab2
use more straightforward ngx_gmtime() implementation
Igor Sysoev <igor@sysoev.ru>
parents:
1874
diff
changeset
|
271 } else { |
182
3c49eaf3f522
nginx-0.0.1-2003-11-13-09:14:05 import
Igor Sysoev <igor@sysoev.ru>
parents:
181
diff
changeset
|
272 |
1970
5bb2c374cab2
use more straightforward ngx_gmtime() implementation
Igor Sysoev <igor@sysoev.ru>
parents:
1874
diff
changeset
|
273 mon += 2; |
182
3c49eaf3f522
nginx-0.0.1-2003-11-13-09:14:05 import
Igor Sysoev <igor@sysoev.ru>
parents:
181
diff
changeset
|
274 |
1970
5bb2c374cab2
use more straightforward ngx_gmtime() implementation
Igor Sysoev <igor@sysoev.ru>
parents:
1874
diff
changeset
|
275 /* |
5bb2c374cab2
use more straightforward ngx_gmtime() implementation
Igor Sysoev <igor@sysoev.ru>
parents:
1874
diff
changeset
|
276 * there is no "yday" in Win32 SYSTEMTIME |
5bb2c374cab2
use more straightforward ngx_gmtime() implementation
Igor Sysoev <igor@sysoev.ru>
parents:
1874
diff
changeset
|
277 * |
5bb2c374cab2
use more straightforward ngx_gmtime() implementation
Igor Sysoev <igor@sysoev.ru>
parents:
1874
diff
changeset
|
278 * yday += 31 + 28 + leap; |
5bb2c374cab2
use more straightforward ngx_gmtime() implementation
Igor Sysoev <igor@sysoev.ru>
parents:
1874
diff
changeset
|
279 */ |
182
3c49eaf3f522
nginx-0.0.1-2003-11-13-09:14:05 import
Igor Sysoev <igor@sysoev.ru>
parents:
181
diff
changeset
|
280 } |
3c49eaf3f522
nginx-0.0.1-2003-11-13-09:14:05 import
Igor Sysoev <igor@sysoev.ru>
parents:
181
diff
changeset
|
281 |
290
87e73f067470
nginx-0.0.2-2004-03-16-10:10:12 import
Igor Sysoev <igor@sysoev.ru>
parents:
271
diff
changeset
|
282 tp->ngx_tm_sec = (ngx_tm_sec_t) sec; |
87e73f067470
nginx-0.0.2-2004-03-16-10:10:12 import
Igor Sysoev <igor@sysoev.ru>
parents:
271
diff
changeset
|
283 tp->ngx_tm_min = (ngx_tm_min_t) min; |
87e73f067470
nginx-0.0.2-2004-03-16-10:10:12 import
Igor Sysoev <igor@sysoev.ru>
parents:
271
diff
changeset
|
284 tp->ngx_tm_hour = (ngx_tm_hour_t) hour; |
87e73f067470
nginx-0.0.2-2004-03-16-10:10:12 import
Igor Sysoev <igor@sysoev.ru>
parents:
271
diff
changeset
|
285 tp->ngx_tm_mday = (ngx_tm_mday_t) mday; |
87e73f067470
nginx-0.0.2-2004-03-16-10:10:12 import
Igor Sysoev <igor@sysoev.ru>
parents:
271
diff
changeset
|
286 tp->ngx_tm_mon = (ngx_tm_mon_t) mon; |
87e73f067470
nginx-0.0.2-2004-03-16-10:10:12 import
Igor Sysoev <igor@sysoev.ru>
parents:
271
diff
changeset
|
287 tp->ngx_tm_year = (ngx_tm_year_t) year; |
87e73f067470
nginx-0.0.2-2004-03-16-10:10:12 import
Igor Sysoev <igor@sysoev.ru>
parents:
271
diff
changeset
|
288 tp->ngx_tm_wday = (ngx_tm_wday_t) wday; |
182
3c49eaf3f522
nginx-0.0.1-2003-11-13-09:14:05 import
Igor Sysoev <igor@sysoev.ru>
parents:
181
diff
changeset
|
289 } |
2162 | 290 |
291 | |
292 time_t | |
293 ngx_next_time(time_t when) | |
294 { | |
295 time_t now, next; | |
296 struct tm tm; | |
297 | |
298 now = ngx_time(); | |
299 | |
300 ngx_libc_localtime(now, &tm); | |
301 | |
302 tm.tm_hour = (int) (when / 3600); | |
303 when %= 3600; | |
304 tm.tm_min = (int) (when / 60); | |
305 tm.tm_sec = (int) (when % 60); | |
306 | |
307 next = mktime(&tm); | |
308 | |
309 if (next == -1) { | |
310 return -1; | |
311 } | |
312 | |
313 if (next - now > 0) { | |
314 return next; | |
315 } | |
316 | |
317 tm.tm_mday++; | |
318 | |
319 /* mktime() should normalize a date (Jan 32, etc) */ | |
320 | |
321 next = mktime(&tm); | |
322 | |
323 if (next != -1) { | |
324 return next; | |
325 } | |
326 | |
327 return -1; | |
328 } |