2010-10-05 Tatsuhiro Tsujikawa <t-tujikawa@users.sourceforge.net>

Supported ANSI C's asctime() format in Time::parseHTTPDate().
	Added Time::parseAsctime(). Changed order of application of parse
	functions. I could not remember why parseRFC850() was tried first.
	* src/TimeA2.cc
	* src/TimeA2.h
	* test/TimeTest.cc
pull/1/head
Tatsuhiro Tsujikawa 2010-10-04 15:12:27 +00:00
parent 5db7b123b5
commit 9b1280f7a3
4 changed files with 38 additions and 5 deletions

View File

@ -1,3 +1,12 @@
2010-10-05 Tatsuhiro Tsujikawa <t-tujikawa@users.sourceforge.net>
Supported ANSI C's asctime() format in Time::parseHTTPDate().
Added Time::parseAsctime(). Changed order of application of parse
functions. I could not remember why parseRFC850() was tried first.
* src/TimeA2.cc
* src/TimeA2.h
* test/TimeTest.cc
2010-10-04 Tatsuhiro Tsujikawa <t-tujikawa@users.sourceforge.net> 2010-10-04 Tatsuhiro Tsujikawa <t-tujikawa@users.sourceforge.net>
Use inet_ntoa if inet_ntop is not available. Since inet_ntoa does Use inet_ntoa if inet_ntop is not available. Since inet_ntoa does

View File

@ -213,11 +213,17 @@ Time Time::parseRFC850Ext(const std::string& datetime)
return parse(datetime, "%a, %d-%b-%Y %H:%M:%S GMT"); return parse(datetime, "%a, %d-%b-%Y %H:%M:%S GMT");
} }
Time Time::parseAsctime(const std::string& datetime)
{
return parse(datetime, "%a %b %d %H:%M:%S %Y");
}
Time Time::parseHTTPDate(const std::string& datetime) Time Time::parseHTTPDate(const std::string& datetime)
{ {
Time (*funcs[])(const std::string&) = { Time (*funcs[])(const std::string&) = {
&parseRFC850,
&parseRFC1123, &parseRFC1123,
&parseRFC850,
&parseAsctime,
&parseRFC850Ext, &parseRFC850Ext,
}; };
for(Time (**funcsp)(const std::string&) = &funcs[0]; for(Time (**funcsp)(const std::string&) = &funcs[0];

View File

@ -123,12 +123,19 @@ public:
// Currently timezone is assumed to GMT. // Currently timezone is assumed to GMT.
static Time parseRFC850(const std::string& datetime); static Time parseRFC850(const std::string& datetime);
// Currently timezone is assumed to GMT. // Currently timezone is assumed to GMT. Basically the format is
// Basically the format is RFC850, but year part is 4digit, eg 2008 // RFC850, but year part is 4digit, eg 2008 This format appears in
// original Netscape's PERSISTENT CLIENT STATE HTTP COOKIES
// Specification. http://curl.haxx.se/rfc/cookie_spec.html
static Time parseRFC850Ext(const std::string& datetime); static Time parseRFC850Ext(const std::string& datetime);
// Try parseRFC1123, parseRFC850Ex, parseRFC850 in that order and returns // Currently timezone is assumed to GMT.
// the first "good" Time object returned by these functions. // ANSI C's asctime() format
static Time parseAsctime(const std::string& datetime);
// Try parseRFC1123, parseRFC850, parseAsctime, parseRFC850Ext in
// that order and returns the first "good" Time object returned by
// these functions.
static Time parseHTTPDate(const std::string& datetime); static Time parseHTTPDate(const std::string& datetime);
static Time null(); static Time null();

View File

@ -15,6 +15,7 @@ class TimeTest:public CppUnit::TestFixture {
CPPUNIT_TEST(testParseRFC1123); CPPUNIT_TEST(testParseRFC1123);
CPPUNIT_TEST(testParseRFC850); CPPUNIT_TEST(testParseRFC850);
CPPUNIT_TEST(testParseRFC850Ext); CPPUNIT_TEST(testParseRFC850Ext);
CPPUNIT_TEST(testParseAsctime);
CPPUNIT_TEST(testParseHTTPDate); CPPUNIT_TEST(testParseHTTPDate);
CPPUNIT_TEST(testOperatorLess); CPPUNIT_TEST(testOperatorLess);
CPPUNIT_TEST(testElapsed); CPPUNIT_TEST(testElapsed);
@ -28,6 +29,7 @@ public:
void testParseRFC1123(); void testParseRFC1123();
void testParseRFC850(); void testParseRFC850();
void testParseRFC850Ext(); void testParseRFC850Ext();
void testParseAsctime();
void testParseHTTPDate(); void testParseHTTPDate();
void testOperatorLess(); void testOperatorLess();
void testElapsed(); void testElapsed();
@ -55,6 +57,12 @@ void TimeTest::testParseRFC850Ext()
CPPUNIT_ASSERT_EQUAL((time_t)1220714793, t1.getTime()); CPPUNIT_ASSERT_EQUAL((time_t)1220714793, t1.getTime());
} }
void TimeTest::testParseAsctime()
{
Time t1 = Time::parseAsctime("Sun Sep 6 15:26:33 2008");
CPPUNIT_ASSERT_EQUAL((time_t)1220714793, t1.getTime());
}
void TimeTest::testParseHTTPDate() void TimeTest::testParseHTTPDate()
{ {
CPPUNIT_ASSERT_EQUAL((time_t)1220714793, CPPUNIT_ASSERT_EQUAL((time_t)1220714793,
@ -66,6 +74,9 @@ void TimeTest::testParseHTTPDate()
CPPUNIT_ASSERT_EQUAL((time_t)1220714793, CPPUNIT_ASSERT_EQUAL((time_t)1220714793,
Time::parseHTTPDate Time::parseHTTPDate
("Sat, 06-Sep-08 15:26:33 GMT").getTime()); ("Sat, 06-Sep-08 15:26:33 GMT").getTime());
CPPUNIT_ASSERT_EQUAL((time_t)1220714793,
Time::parseHTTPDate
("Sun Sep 6 15:26:33 2008").getTime());
CPPUNIT_ASSERT(Time::parseHTTPDate CPPUNIT_ASSERT(Time::parseHTTPDate
("Sat, 2008-09-06 15:26:33 GMT").bad()); ("Sat, 2008-09-06 15:26:33 GMT").bad());
} }