📄 apr_date.c
字号:
ds.tm_sec = ((sec10 - '0') * 10) + (sec1 - '0'); \
}
#define TIMEPARSE_STD(ds,timstr) \
{ \
TIMEPARSE(ds, timstr[0],timstr[1], \
timstr[3],timstr[4], \
timstr[6],timstr[7]); \
}
APU_DECLARE(apr_time_t) apr_date_parse_rfc(const char *date)
{
apr_time_exp_t ds;
apr_time_t result;
int mint, mon;
const char *monstr, *timstr, *gmtstr;
static const int months[12] =
{
('J' << 16) | ('a' << 8) | 'n', ('F' << 16) | ('e' << 8) | 'b',
('M' << 16) | ('a' << 8) | 'r', ('A' << 16) | ('p' << 8) | 'r',
('M' << 16) | ('a' << 8) | 'y', ('J' << 16) | ('u' << 8) | 'n',
('J' << 16) | ('u' << 8) | 'l', ('A' << 16) | ('u' << 8) | 'g',
('S' << 16) | ('e' << 8) | 'p', ('O' << 16) | ('c' << 8) | 't',
('N' << 16) | ('o' << 8) | 'v', ('D' << 16) | ('e' << 8) | 'c' };
if (!date)
return APR_DATE_BAD;
/* Not all dates have text months at the beginning. */
if (!apr_isdigit(date[0]))
{
while (*date && apr_isspace(*date)) /* Find first non-whitespace char */
++date;
if (*date == '\0')
return APR_DATE_BAD;
if ((date = strchr(date, ' ')) == NULL) /* Find space after weekday */
return APR_DATE_BAD;
++date; /* Now pointing to first char after space, which should be */ }
/* start of the actual date information for all 11 formats. */
if (apr_date_checkmask(date, "## @$$ #### ##:##:## *")) { /* RFC 1123 format */
ds.tm_year = ((date[7] - '0') * 10 + (date[8] - '0') - 19) * 100;
if (ds.tm_year < 0)
return APR_DATE_BAD;
ds.tm_year += ((date[9] - '0') * 10) + (date[10] - '0');
ds.tm_mday = ((date[0] - '0') * 10) + (date[1] - '0');
monstr = date + 3;
timstr = date + 12;
gmtstr = date + 20;
TIMEPARSE_STD(ds, timstr);
}
else if (apr_date_checkmask(date, "##-@$$-## ##:##:## *")) {/* RFC 850 format */
ds.tm_year = ((date[7] - '0') * 10) + (date[8] - '0');
if (ds.tm_year < 70)
ds.tm_year += 100;
ds.tm_mday = ((date[0] - '0') * 10) + (date[1] - '0');
monstr = date + 3;
timstr = date + 10;
gmtstr = date + 19;
TIMEPARSE_STD(ds, timstr);
}
else if (apr_date_checkmask(date, "@$$ ~# ##:##:## ####*")) {
/* asctime format */
ds.tm_year = ((date[16] - '0') * 10 + (date[17] - '0') - 19) * 100;
if (ds.tm_year < 0)
return APR_DATE_BAD;
ds.tm_year += ((date[18] - '0') * 10) + (date[19] - '0');
if (date[4] == ' ')
ds.tm_mday = 0;
else
ds.tm_mday = (date[4] - '0') * 10;
ds.tm_mday += (date[5] - '0');
monstr = date;
timstr = date + 7;
gmtstr = NULL;
TIMEPARSE_STD(ds, timstr);
}
else if (apr_date_checkmask(date, "# @$$ #### ##:##:## *")) {
/* RFC 1123 format*/
ds.tm_year = ((date[6] - '0') * 10 + (date[7] - '0') - 19) * 100;
if (ds.tm_year < 0)
return APR_DATE_BAD;
ds.tm_year += ((date[8] - '0') * 10) + (date[9] - '0');
ds.tm_mday = (date[0] - '0');
monstr = date + 2;
timstr = date + 11;
gmtstr = date + 20;
TIMEPARSE_STD(ds, timstr);
}
else if (apr_date_checkmask(date, "## @$$ ## ##:##:## *")) {
/* This is the old RFC 1123 date format - many many years ago, people
* used two-digit years. Oh, how foolish. */
ds.tm_year = ((date[7] - '0') * 10) + (date[8] - '0');
if (ds.tm_year < 70)
ds.tm_year += 100;
ds.tm_mday = ((date[0] - '0') * 10) + (date[1] - '0');
monstr = date + 3;
timstr = date + 10;
gmtstr = date + 19;
TIMEPARSE_STD(ds, timstr);
}
else if (apr_date_checkmask(date, "# @$$ ## ##:##:## *")) {
/* This is the old RFC 1123 date format - many many years ago, people
* used two-digit years. Oh, how foolish. */
ds.tm_year = ((date[6] - '0') * 10) + (date[7] - '0');
if (ds.tm_year < 70)
ds.tm_year += 100;
ds.tm_mday = (date[0] - '0');
monstr = date + 2;
timstr = date + 9;
gmtstr = date + 18;
TIMEPARSE_STD(ds, timstr);
}
else if (apr_date_checkmask(date, "## @$$ ## ##:## *")) {
/* Loser format. This is quite bogus. */
ds.tm_year = ((date[7] - '0') * 10) + (date[8] - '0');
if (ds.tm_year < 70)
ds.tm_year += 100;
ds.tm_mday = ((date[0] - '0') * 10) + (date[1] - '0');
monstr = date + 3;
timstr = date + 10;
gmtstr = NULL;
TIMEPARSE(ds, timstr[0],timstr[1], timstr[3],timstr[4], '0','0');
}
else if (apr_date_checkmask(date, "# @$$ ## ##:## *")) {
/* Loser format. This is quite bogus. */
ds.tm_year = ((date[6] - '0') * 10) + (date[7] - '0');
if (ds.tm_year < 70)
ds.tm_year += 100;
ds.tm_mday = (date[0] - '0');
monstr = date + 2;
timstr = date + 9;
gmtstr = NULL;
TIMEPARSE(ds, timstr[0],timstr[1], timstr[3],timstr[4], '0','0');
}
else if (apr_date_checkmask(date, "## @$$ ## #:##:## *")) {
/* Loser format. This is quite bogus. */
ds.tm_year = ((date[7] - '0') * 10) + (date[8] - '0');
if (ds.tm_year < 70)
ds.tm_year += 100;
ds.tm_mday = ((date[0] - '0') * 10) + (date[1] - '0');
monstr = date + 3;
timstr = date + 9;
gmtstr = date + 18;
TIMEPARSE(ds, '0',timstr[1], timstr[3],timstr[4], timstr[6],timstr[7]);
}
else if (apr_date_checkmask(date, "# @$$ ## #:##:## *")) {
/* Loser format. This is quite bogus. */
ds.tm_year = ((date[6] - '0') * 10) + (date[7] - '0');
if (ds.tm_year < 70)
ds.tm_year += 100;
ds.tm_mday = (date[0] - '0');
monstr = date + 2;
timstr = date + 8;
gmtstr = date + 17;
TIMEPARSE(ds, '0',timstr[1], timstr[3],timstr[4], timstr[6],timstr[7]);
}
else if (apr_date_checkmask(date, " # @$$ #### ##:##:## *")) {
/* RFC 1123 format with a space instead of a leading zero. */
ds.tm_year = ((date[7] - '0') * 10 + (date[8] - '0') - 19) * 100;
if (ds.tm_year < 0)
return APR_DATE_BAD;
ds.tm_year += ((date[9] - '0') * 10) + (date[10] - '0');
ds.tm_mday = (date[1] - '0');
monstr = date + 3;
timstr = date + 12;
gmtstr = date + 20;
TIMEPARSE_STD(ds, timstr);
}
else if (apr_date_checkmask(date, "##-@$$-#### ##:##:## *")) {
/* RFC 1123 with dashes instead of spaces between date/month/year
* This also looks like RFC 850 with four digit years.
*/
ds.tm_year = ((date[7] - '0') * 10 + (date[8] - '0') - 19) * 100;
if (ds.tm_year < 0)
return APR_DATE_BAD;
ds.tm_year += ((date[9] - '0') * 10) + (date[10] - '0');
ds.tm_mday = ((date[0] - '0') * 10) + (date[1] - '0');
monstr = date + 3;
timstr = date + 12;
gmtstr = date + 21;
TIMEPARSE_STD(ds, timstr);
}
else
return APR_DATE_BAD;
if (ds.tm_mday <= 0 || ds.tm_mday > 31)
return APR_DATE_BAD;
if ((ds.tm_hour > 23) || (ds.tm_min > 59) || (ds.tm_sec > 61))
return APR_DATE_BAD;
mint = (monstr[0] << 16) | (monstr[1] << 8) | monstr[2];
for (mon = 0; mon < 12; mon++)
if (mint == months[mon])
break;
if (mon == 12)
return APR_DATE_BAD;
if ((ds.tm_mday == 31) && (mon == 3 || mon == 5 || mon == 8 || mon == 10))
return APR_DATE_BAD;
/* February gets special check for leapyear */
if ((mon == 1) &&
((ds.tm_mday > 29)
|| ((ds.tm_mday == 29)
&& ((ds.tm_year & 3)
|| (((ds.tm_year % 100) == 0)
&& (((ds.tm_year % 400) != 100)))))))
return APR_DATE_BAD;
ds.tm_mon = mon;
/* tm_gmtoff is the number of seconds off of GMT the time is.
*
* We only currently support: [+-]ZZZZ where Z is the offset in
* hours from GMT.
*
* If there is any confusion, tm_gmtoff will remain 0.
*/
ds.tm_gmtoff = 0;
if (gmtstr && *gmtstr != '\0') {
/* Do we have a GMT? */
if (*(++gmtstr) != '\0') {
int offset;
switch (*(gmtstr++)) {
case '-':
offset = atoi(gmtstr);
ds.tm_gmtoff -= (offset / 100) * 60 * 60;
ds.tm_gmtoff -= (offset % 100) * 60;
break;
case '+':
offset = atoi(gmtstr);
ds.tm_gmtoff += (offset / 100) * 60 * 60;
ds.tm_gmtoff += (offset % 100) * 60;
break;
}
}
}
/* apr_time_exp_get uses tm_usec field, but it hasn't been set yet.
* It should be safe to just zero out this value.
* tm_usec is the number of microseconds into the second. HTTP only
* cares about second granularity.
*/
ds.tm_usec = 0;
if (apr_time_exp_gmt_get(&result, &ds) != APR_SUCCESS)
return APR_DATE_BAD;
return result;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -