mirror of https://github.com/aria2/aria2
2008-09-07 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>
Implmented the functions to parse date. * src/TimeA2.cc * src/TimeA2.h * test/TimeTest.ccpull/1/head
parent
9fc8b15736
commit
69b18308d9
|
@ -1,3 +1,10 @@
|
||||||
|
2008-09-07 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>
|
||||||
|
|
||||||
|
Implmented the functions to parse date.
|
||||||
|
* src/TimeA2.cc
|
||||||
|
* src/TimeA2.h
|
||||||
|
* test/TimeTest.cc
|
||||||
|
|
||||||
2008-09-05 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>
|
2008-09-05 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>
|
||||||
|
|
||||||
Added "Firefox3 Cookie" feature to FeatureConfig
|
Added "Firefox3 Cookie" feature to FeatureConfig
|
||||||
|
|
|
@ -35,6 +35,8 @@
|
||||||
|
|
||||||
#include "TimeA2.h"
|
#include "TimeA2.h"
|
||||||
#include "Util.h"
|
#include "Util.h"
|
||||||
|
#include "array_fun.h"
|
||||||
|
#include <cstring>
|
||||||
|
|
||||||
namespace aria2 {
|
namespace aria2 {
|
||||||
|
|
||||||
|
@ -136,4 +138,58 @@ void Time::setTimeInSec(time_t sec) {
|
||||||
tv.tv_usec = 0;
|
tv.tv_usec = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool Time::good() const
|
||||||
|
{
|
||||||
|
return tv.tv_sec >= 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
Time Time::parse(const std::string& datetime, const std::string& format)
|
||||||
|
{
|
||||||
|
struct tm tm;
|
||||||
|
memset(&tm, 0, sizeof(tm));
|
||||||
|
char* r = strptime(datetime.c_str(), format.c_str(), &tm);
|
||||||
|
if(r != datetime.c_str()+datetime.size()) {
|
||||||
|
return Time(-1);
|
||||||
|
}
|
||||||
|
time_t thetime = timegm(&tm);
|
||||||
|
if(thetime == -1) {
|
||||||
|
if(tm.tm_year >= 2038-1900) {
|
||||||
|
thetime = INT32_MAX;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return Time(thetime);
|
||||||
|
}
|
||||||
|
|
||||||
|
Time Time::parseRFC1123(const std::string& datetime)
|
||||||
|
{
|
||||||
|
return parse(datetime, "%a, %d %b %Y %H:%M:%S GMT");
|
||||||
|
}
|
||||||
|
|
||||||
|
Time Time::parseRFC850(const std::string& datetime)
|
||||||
|
{
|
||||||
|
return parse(datetime, "%a, %d-%b-%y %H:%M:%S GMT");
|
||||||
|
}
|
||||||
|
|
||||||
|
Time Time::parseRFC850Ext(const std::string& datetime)
|
||||||
|
{
|
||||||
|
return parse(datetime, "%a, %d-%b-%Y %H:%M:%S GMT");
|
||||||
|
}
|
||||||
|
|
||||||
|
Time Time::parseHTTPDate(const std::string& datetime)
|
||||||
|
{
|
||||||
|
Time (*funcs[])(const std::string&) = {
|
||||||
|
&parseRFC1123,
|
||||||
|
&parseRFC850Ext,
|
||||||
|
&parseRFC850,
|
||||||
|
};
|
||||||
|
for(Time (**funcsp)(const std::string&) = &funcs[0];
|
||||||
|
funcsp != &funcs[arrayLength(funcs)]; ++funcsp) {
|
||||||
|
Time t = (*funcsp)(datetime);
|
||||||
|
if(t.good()) {
|
||||||
|
return t;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return Time(-1);
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace aria2
|
} // namespace aria2
|
||||||
|
|
20
src/TimeA2.h
20
src/TimeA2.h
|
@ -38,6 +38,7 @@
|
||||||
#include "common.h"
|
#include "common.h"
|
||||||
#include "a2time.h"
|
#include "a2time.h"
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
namespace aria2 {
|
namespace aria2 {
|
||||||
|
|
||||||
|
@ -85,6 +86,25 @@ public:
|
||||||
void setTimeInSec(time_t sec);
|
void setTimeInSec(time_t sec);
|
||||||
|
|
||||||
bool isNewer(const Time& time) const;
|
bool isNewer(const Time& time) const;
|
||||||
|
|
||||||
|
bool good() const;
|
||||||
|
|
||||||
|
// Currently timezone is assumed as GMT.
|
||||||
|
static Time parse(const std::string& datetime, const std::string& format);
|
||||||
|
|
||||||
|
// Currently timezone is assumed to GMT.
|
||||||
|
static Time parseRFC1123(const std::string& datetime);
|
||||||
|
|
||||||
|
// 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
|
||||||
|
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.
|
||||||
|
static Time parseHTTPDate(const std::string& datetime);
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace aria2
|
} // namespace aria2
|
||||||
|
|
|
@ -60,7 +60,8 @@ aria2c_SOURCES = AllTest.cc\
|
||||||
NsCookieParserTest.cc\
|
NsCookieParserTest.cc\
|
||||||
DirectDiskAdaptorTest.cc\
|
DirectDiskAdaptorTest.cc\
|
||||||
CookieTest.cc\
|
CookieTest.cc\
|
||||||
CookieStorageTest.cc
|
CookieStorageTest.cc\
|
||||||
|
TimeTest.cc
|
||||||
|
|
||||||
if HAVE_LIBZ
|
if HAVE_LIBZ
|
||||||
aria2c_SOURCES += GZipDecoderTest.cc
|
aria2c_SOURCES += GZipDecoderTest.cc
|
||||||
|
|
|
@ -194,7 +194,7 @@ am__aria2c_SOURCES_DIST = AllTest.cc TestUtil.cc TestUtil.h \
|
||||||
ServerStatURISelectorTest.cc InOrderURISelectorTest.cc \
|
ServerStatURISelectorTest.cc InOrderURISelectorTest.cc \
|
||||||
ServerStatTest.cc NsCookieParserTest.cc \
|
ServerStatTest.cc NsCookieParserTest.cc \
|
||||||
DirectDiskAdaptorTest.cc CookieTest.cc CookieStorageTest.cc \
|
DirectDiskAdaptorTest.cc CookieTest.cc CookieStorageTest.cc \
|
||||||
GZipDecoderTest.cc Sqlite3MozCookieParserTest.cc \
|
TimeTest.cc GZipDecoderTest.cc Sqlite3MozCookieParserTest.cc \
|
||||||
MessageDigestHelperTest.cc \
|
MessageDigestHelperTest.cc \
|
||||||
IteratableChunkChecksumValidatorTest.cc \
|
IteratableChunkChecksumValidatorTest.cc \
|
||||||
IteratableChecksumValidatorTest.cc BtAllowedFastMessageTest.cc \
|
IteratableChecksumValidatorTest.cc BtAllowedFastMessageTest.cc \
|
||||||
|
@ -366,8 +366,8 @@ am_aria2c_OBJECTS = AllTest.$(OBJEXT) TestUtil.$(OBJEXT) \
|
||||||
InOrderURISelectorTest.$(OBJEXT) ServerStatTest.$(OBJEXT) \
|
InOrderURISelectorTest.$(OBJEXT) ServerStatTest.$(OBJEXT) \
|
||||||
NsCookieParserTest.$(OBJEXT) DirectDiskAdaptorTest.$(OBJEXT) \
|
NsCookieParserTest.$(OBJEXT) DirectDiskAdaptorTest.$(OBJEXT) \
|
||||||
CookieTest.$(OBJEXT) CookieStorageTest.$(OBJEXT) \
|
CookieTest.$(OBJEXT) CookieStorageTest.$(OBJEXT) \
|
||||||
$(am__objects_1) $(am__objects_2) $(am__objects_3) \
|
TimeTest.$(OBJEXT) $(am__objects_1) $(am__objects_2) \
|
||||||
$(am__objects_4) $(am__objects_5)
|
$(am__objects_3) $(am__objects_4) $(am__objects_5)
|
||||||
aria2c_OBJECTS = $(am_aria2c_OBJECTS)
|
aria2c_OBJECTS = $(am_aria2c_OBJECTS)
|
||||||
am__DEPENDENCIES_1 =
|
am__DEPENDENCIES_1 =
|
||||||
aria2c_DEPENDENCIES = ../src/libaria2c.a $(am__DEPENDENCIES_1)
|
aria2c_DEPENDENCIES = ../src/libaria2c.a $(am__DEPENDENCIES_1)
|
||||||
|
@ -589,7 +589,7 @@ aria2c_SOURCES = AllTest.cc TestUtil.cc TestUtil.h SocketCoreTest.cc \
|
||||||
ServerStatURISelectorTest.cc InOrderURISelectorTest.cc \
|
ServerStatURISelectorTest.cc InOrderURISelectorTest.cc \
|
||||||
ServerStatTest.cc NsCookieParserTest.cc \
|
ServerStatTest.cc NsCookieParserTest.cc \
|
||||||
DirectDiskAdaptorTest.cc CookieTest.cc CookieStorageTest.cc \
|
DirectDiskAdaptorTest.cc CookieTest.cc CookieStorageTest.cc \
|
||||||
$(am__append_1) $(am__append_2) $(am__append_3) \
|
TimeTest.cc $(am__append_1) $(am__append_2) $(am__append_3) \
|
||||||
$(am__append_4) $(am__append_5)
|
$(am__append_4) $(am__append_5)
|
||||||
|
|
||||||
#aria2c_CXXFLAGS = ${CPPUNIT_CFLAGS} -I../src -I../lib -Wall -D_FILE_OFFSET_BITS=64
|
#aria2c_CXXFLAGS = ${CPPUNIT_CFLAGS} -I../src -I../lib -Wall -D_FILE_OFFSET_BITS=64
|
||||||
|
@ -816,6 +816,7 @@ distclean-compile:
|
||||||
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/TaggedItemTest.Po@am__quote@
|
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/TaggedItemTest.Po@am__quote@
|
||||||
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/TestUtil.Po@am__quote@
|
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/TestUtil.Po@am__quote@
|
||||||
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/TimeSeedCriteriaTest.Po@am__quote@
|
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/TimeSeedCriteriaTest.Po@am__quote@
|
||||||
|
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/TimeTest.Po@am__quote@
|
||||||
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/UTPexExtensionMessageTest.Po@am__quote@
|
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/UTPexExtensionMessageTest.Po@am__quote@
|
||||||
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/UriListParserTest.Po@am__quote@
|
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/UriListParserTest.Po@am__quote@
|
||||||
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/UtilTest.Po@am__quote@
|
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/UtilTest.Po@am__quote@
|
||||||
|
|
|
@ -0,0 +1,65 @@
|
||||||
|
#include "TimeA2.h"
|
||||||
|
#include "Exception.h"
|
||||||
|
#include "Util.h"
|
||||||
|
#include <iostream>
|
||||||
|
#include <cppunit/extensions/HelperMacros.h>
|
||||||
|
|
||||||
|
namespace aria2 {
|
||||||
|
|
||||||
|
class TimeTest:public CppUnit::TestFixture {
|
||||||
|
|
||||||
|
CPPUNIT_TEST_SUITE(TimeTest);
|
||||||
|
CPPUNIT_TEST(testParseRFC1123);
|
||||||
|
CPPUNIT_TEST(testParseRFC850);
|
||||||
|
CPPUNIT_TEST(testParseRFC850Ext);
|
||||||
|
CPPUNIT_TEST(testParseHTTPDate);
|
||||||
|
CPPUNIT_TEST_SUITE_END();
|
||||||
|
public:
|
||||||
|
void setUp() {}
|
||||||
|
|
||||||
|
void tearDown() {}
|
||||||
|
|
||||||
|
void testParseRFC1123();
|
||||||
|
void testParseRFC850();
|
||||||
|
void testParseRFC850Ext();
|
||||||
|
void testParseHTTPDate();
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
CPPUNIT_TEST_SUITE_REGISTRATION(TimeTest);
|
||||||
|
|
||||||
|
void TimeTest::testParseRFC1123()
|
||||||
|
{
|
||||||
|
Time t1 = Time::parseRFC1123("Sat, 06 Sep 2008 15:26:33 GMT");
|
||||||
|
CPPUNIT_ASSERT_EQUAL((time_t)1220714793, t1.getTime());
|
||||||
|
}
|
||||||
|
|
||||||
|
void TimeTest::testParseRFC850()
|
||||||
|
{
|
||||||
|
Time t1 = Time::parseRFC850("Saturday, 06-Sep-08 15:26:33 GMT");
|
||||||
|
CPPUNIT_ASSERT_EQUAL((time_t)1220714793, t1.getTime());
|
||||||
|
}
|
||||||
|
|
||||||
|
void TimeTest::testParseRFC850Ext()
|
||||||
|
{
|
||||||
|
Time t1 = Time::parseRFC850Ext("Saturday, 06-Sep-2008 15:26:33 GMT");
|
||||||
|
CPPUNIT_ASSERT_EQUAL((time_t)1220714793, t1.getTime());
|
||||||
|
}
|
||||||
|
|
||||||
|
void TimeTest::testParseHTTPDate()
|
||||||
|
{
|
||||||
|
CPPUNIT_ASSERT_EQUAL((time_t)1220714793,
|
||||||
|
Time::parseHTTPDate
|
||||||
|
("Sat, 06 Sep 2008 15:26:33 GMT").getTime());
|
||||||
|
CPPUNIT_ASSERT_EQUAL((time_t)1220714793,
|
||||||
|
Time::parseHTTPDate
|
||||||
|
("Sat, 06-Sep-2008 15:26:33 GMT").getTime());
|
||||||
|
CPPUNIT_ASSERT_EQUAL((time_t)1220714793,
|
||||||
|
Time::parseHTTPDate
|
||||||
|
("Sat, 06-Sep-08 15:26:33 GMT").getTime());
|
||||||
|
CPPUNIT_ASSERT_EQUAL((time_t)-1,
|
||||||
|
Time::parseHTTPDate
|
||||||
|
("Sat, 2008-09-06 15:26:33 GMT").getTime());
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace aria2
|
Loading…
Reference in New Issue