3 * Copyright (C) Igor Sysoev
7 #include <ngx_config.h>
11 static int mday[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
14 ngx_http_parse_time(u_char *value, size_t len)
17 int day, month, year, hour, min, sec;
20 rfc822, /* Tue, 10 Nov 2002 23:50:13 */
21 rfc850, /* Tuesday, 10-Dec-02 23:50:13 */
22 isoc /* Tue Dec 10 23:50:13 2002 */
28 #if (NGX_SUPPRESS_WARN)
33 for (p = value; p < end; p++) {
44 for (p++; p < end; p++)
54 if (*p < '0' || *p > '9' || *(p + 1) < '0' || *(p + 1) > '9') {
58 day = (*p - '0') * 10 + *(p + 1) - '0';
67 } else if (*p == '-') {
80 month = *(p + 1) == 'a' ? 0 : *(p + 2) == 'n' ? 5 : 6;
88 month = *(p + 2) == 'r' ? 2 : 4;
92 month = *(p + 1) == 'p' ? 3 : 7;
117 if ((fmt == rfc822 && *p != ' ') || (fmt == rfc850 && *p != '-')) {
124 if (*p < '0' || *p > '9' || *(p + 1) < '0' || *(p + 1) > '9'
125 || *(p + 2) < '0' || *(p + 2) > '9'
126 || *(p + 3) < '0' || *(p + 3) > '9')
131 year = (*p - '0') * 1000 + (*(p + 1) - '0') * 100
132 + (*(p + 2) - '0') * 10 + *(p + 3) - '0';
135 } else if (fmt == rfc850) {
136 if (*p < '0' || *p > '9' || *(p + 1) < '0' || *(p + 1) > '9') {
140 year = (*p - '0') * 10 + *(p + 1) - '0';
141 year += (year < 70) ? 2000 : 1900;
150 if (*p < '0' || *p > '9') {
157 if (*p < '0' || *p > '9') {
161 day = day * 10 + *p++ - '0';
173 if (*p < '0' || *p > '9' || *(p + 1) < '0' || *(p + 1) > '9') {
177 hour = (*p - '0') * 10 + *(p + 1) - '0';
184 if (*p < '0' || *p > '9' || *(p + 1) < '0' || *(p + 1) > '9') {
188 min = (*p - '0') * 10 + *(p + 1) - '0';
195 if (*p < '0' || *p > '9' || *(p + 1) < '0' || *(p + 1) > '9') {
199 sec = (*p - '0') * 10 + *(p + 1) - '0';
208 if (*p < '0' || *p > '9' || *(p + 1) < '0' || *(p + 1) > '9'
209 || *(p + 2) < '0' || *(p + 2) > '9'
210 || *(p + 3) < '0' || *(p + 3) > '9')
215 year = (*p - '0') * 1000 + (*(p + 1) - '0') * 100
216 + (*(p + 2) - '0') * 10 + *(p + 3) - '0';
219 if (hour > 23 || min > 59 || sec > 59) {
223 if (day == 29 && month == 1) {
224 if ((year & 3) || ((year % 100 == 0) && (year % 400) != 0)) {
228 } else if (day > mday[month]) {
232 #if (NGX_TIME_T_SIZE <= 4)
241 * shift new year to March 1 and start months from 1 (not 0),
242 * it is needed for Gauss' formula
250 /* Gauss' formula for Grigorian days since March 1, 1 BC */
253 /* days in years including leap years since March 1, 1 BC */
255 365 * year + year / 4 - year / 100 + year / 400
257 /* days before the month */
259 + 367 * month / 12 - 30
261 /* days before the day */
266 * 719527 days were between March 1, 1 BC and March 1, 1970,
267 * 31 and 28 days were in January and February 1970
270 - 719527 + 31 + 28) * 86400 + hour * 3600 + min * 60 + sec;