simplify time conversion using strptime instead of manual hack, also check for timezone issues if we get unusual time values

This commit is contained in:
Christian Grothoff 2017-09-26 13:46:06 +02:00
parent 7cb48a720b
commit a8de810bf7
No known key found for this signature in database
GPG Key ID: 939E6BE1E29FC3CC

View File

@ -1000,49 +1000,32 @@ static int
parse_date_string (const char *date, parse_date_string (const char *date,
struct GNUNET_TIME_Absolute *at) struct GNUNET_TIME_Absolute *at)
{ {
static const char *const days[] =
{ "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
static const char *const mons[] =
{ "Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
struct tm now; struct tm now;
time_t t; time_t t;
char day[4]; const char *end;
char mon[4];
unsigned int mday;
unsigned int year;
unsigned int h;
unsigned int m;
unsigned int s;
if (7 != sscanf (date, memset (&now,
"%3s, %02u %3s %04u %02u:%02u:%02u GMT", 0,
day, sizeof (now));
&mday, end = strptime (date,
mon, "%a, %d %b %Y %H:%M:%S %Z", /* RFC-1123 standard spec */
&year, &now);
&h, if ( (NULL == end) ||
&m, ( (*end != '\n') &&
&s)) (*end != '\r') ) )
return GNUNET_SYSERR; {
memset (&now, 0, sizeof (now)); GNUNET_break_op (0);
now.tm_year = year - 1900;
now.tm_mday = mday;
now.tm_hour = h;
now.tm_min = m;
now.tm_sec = s;
now.tm_wday = 7;
for (unsigned int i=0;i<7;i++)
if (0 == strcasecmp (days[i], day))
now.tm_wday = i;
now.tm_mon = 12;
for (unsigned int i=0;i<12;i++)
if (0 == strcasecmp (mons[i], mon))
now.tm_mon = i;
if ( (7 == now.tm_wday) ||
(12 == now.tm_mon) )
return GNUNET_SYSERR; return GNUNET_SYSERR;
}
t = mktime (&now); t = mktime (&now);
if (((time_t) -1) == t)
{
GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR,
"mktime");
return GNUNET_SYSERR;
}
if (t < 0)
t = 0; /* can happen due to timezone issues if date was 1.1.1970 */
at->abs_value_us = 1000LL * 1000LL * t; at->abs_value_us = 1000LL * 1000LL * t;
return GNUNET_OK; return GNUNET_OK;
} }
@ -1085,6 +1068,7 @@ header_cb (char *buffer,
"Failed to parse %s-header `%s'\n", "Failed to parse %s-header `%s'\n",
MHD_HTTP_HEADER_EXPIRES, MHD_HTTP_HEADER_EXPIRES,
val); val);
kr->expire = GNUNET_TIME_UNIT_ZERO_ABS;
} }
GNUNET_free (val); GNUNET_free (val);
return total; return total;