From 9b1280f7a3c0d0223ad8bbc20e108b776d9c53af Mon Sep 17 00:00:00 2001 From: Tatsuhiro Tsujikawa Date: Mon, 4 Oct 2010 15:12:27 +0000 Subject: [PATCH] 2010-10-05 Tatsuhiro Tsujikawa 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 --- ChangeLog | 9 +++++++++ src/TimeA2.cc | 8 +++++++- src/TimeA2.h | 15 +++++++++++---- test/TimeTest.cc | 11 +++++++++++ 4 files changed, 38 insertions(+), 5 deletions(-) diff --git a/ChangeLog b/ChangeLog index 0d12217d..0cb255eb 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,12 @@ +2010-10-05 Tatsuhiro Tsujikawa + + 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 Use inet_ntoa if inet_ntop is not available. Since inet_ntoa does diff --git a/src/TimeA2.cc b/src/TimeA2.cc index 9733375b..6d9889b5 100644 --- a/src/TimeA2.cc +++ b/src/TimeA2.cc @@ -213,11 +213,17 @@ Time Time::parseRFC850Ext(const std::string& datetime) 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 (*funcs[])(const std::string&) = { - &parseRFC850, &parseRFC1123, + &parseRFC850, + &parseAsctime, &parseRFC850Ext, }; for(Time (**funcsp)(const std::string&) = &funcs[0]; diff --git a/src/TimeA2.h b/src/TimeA2.h index 0044ee79..5763200d 100644 --- a/src/TimeA2.h +++ b/src/TimeA2.h @@ -123,12 +123,19 @@ public: // Currently timezone is assumed to GMT. static Time parseRFC850(const std::string& datetime); - // Currently timezone is assumed to GMT. - // Basically the format is RFC850, but year part is 4digit, eg 2008 + // Currently timezone is assumed to GMT. Basically the format is + // 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); - // Try parseRFC1123, parseRFC850Ex, parseRFC850 in that order and returns - // the first "good" Time object returned by these functions. + // Currently timezone is assumed to GMT. + // 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 null(); diff --git a/test/TimeTest.cc b/test/TimeTest.cc index 0b1be6cc..c3688d06 100644 --- a/test/TimeTest.cc +++ b/test/TimeTest.cc @@ -15,6 +15,7 @@ class TimeTest:public CppUnit::TestFixture { CPPUNIT_TEST(testParseRFC1123); CPPUNIT_TEST(testParseRFC850); CPPUNIT_TEST(testParseRFC850Ext); + CPPUNIT_TEST(testParseAsctime); CPPUNIT_TEST(testParseHTTPDate); CPPUNIT_TEST(testOperatorLess); CPPUNIT_TEST(testElapsed); @@ -28,6 +29,7 @@ public: void testParseRFC1123(); void testParseRFC850(); void testParseRFC850Ext(); + void testParseAsctime(); void testParseHTTPDate(); void testOperatorLess(); void testElapsed(); @@ -55,6 +57,12 @@ void TimeTest::testParseRFC850Ext() 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() { CPPUNIT_ASSERT_EQUAL((time_t)1220714793, @@ -66,6 +74,9 @@ void TimeTest::testParseHTTPDate() CPPUNIT_ASSERT_EQUAL((time_t)1220714793, Time::parseHTTPDate ("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 ("Sat, 2008-09-06 15:26:33 GMT").bad()); }