Rewrite Time with chrono

pull/251/merge
Tatsuhiro Tsujikawa 2015-06-09 03:04:38 +09:00
parent 99cd73c092
commit b0f440e631
20 changed files with 87 additions and 270 deletions

View File

@ -336,7 +336,7 @@ std::string AdaptiveURISelector::getFirstToTestUri
power = (int)pow(2.0, (float)counter);
/* We test the mirror another time if it has not been
* tested since 2^counter days */
if(ss->getLastUpdated().difference() > power*24*60*60) {
if(ss->getLastUpdated().difference() > std::chrono::hours(power * 24)) {
return u;
}
}

View File

@ -131,12 +131,12 @@ void DHTRoutingTableDeserializer::deserialize(const std::string& filename)
// time
if(version == 2) {
READ_CHECK(fp, &temp32, sizeof(temp32));
serializedTime_.setTimeInSec(ntohl(temp32));
serializedTime_.setTimeFromEpoch(ntohl(temp32));
// 4bytes reserved
readBytes(fp, buf, buf.size(), 4);
} else {
READ_CHECK(fp, &temp64, sizeof(temp64));
serializedTime_.setTimeInSec(ntoh64(temp64));
serializedTime_.setTimeFromEpoch(ntoh64(temp64));
}
// localnode

View File

@ -101,7 +101,7 @@ void DHTRoutingTableSerializer::serialize(const std::string& filename)
WRITE_CHECK(fp, header, 8);
// write save date
uint64_t ntime = hton64(Time().getTime());
uint64_t ntime = hton64(Time().getTimeFromEpoch());
WRITE_CHECK(fp, &ntime, sizeof(ntime));
// localnode

View File

@ -257,14 +257,14 @@ bool File::utime(const Time& actime, const Time& modtime) const
{
#if defined(HAVE_UTIMES) && !defined(__MINGW32__)
struct timeval times[2] = {
{ actime.getTime(), 0 },
{ modtime.getTime(), 0 }
{ actime.getTimeFromEpoch(), 0 },
{ modtime.getTimeFromEpoch(), 0 }
};
return utimes(name_.c_str(), times) == 0;
#else // !HAVE_UTIMES
a2utimbuf ub;
ub.actime = actime.getTime();
ub.modtime = modtime.getTime();
ub.actime = actime.getTimeFromEpoch();
ub.modtime = modtime.getTimeFromEpoch();
return a2utime(utf8ToWChar(name_).c_str(), &ub) == 0;
#endif // !HAVE_UTIMES
}

View File

@ -232,7 +232,7 @@ std::string HttpRequest::createRequest()
std::string path = getDir();
path += getFile();
auto cookies = cookieStorage_->criteriaFind(getHost(), path,
Time().getTime(),
Time().getTimeFromEpoch(),
getProtocol() == "https");
for(auto c : cookies) {
cookiesValue += c->toString();

View File

@ -144,7 +144,7 @@ void HttpResponse::retrieveCookie()
((*r.first).second,
httpRequest_->getHost(),
httpRequest_->getDir(),
now.getTime());
now.getTimeFromEpoch());
}
}

View File

@ -217,7 +217,7 @@ int MultiUrlRequestInfo::prepare()
File cookieFile(option_->get(PREF_LOAD_COOKIES));
if(cookieFile.isFile() &&
e_->getCookieStorage()->load(cookieFile.getPath(),
Time().getTime())) {
Time().getTimeFromEpoch())) {
A2_LOG_INFO(fmt("Loaded cookies from '%s'.",
cookieFile.getPath().c_str()));
} else {
@ -278,7 +278,7 @@ int MultiUrlRequestInfo::prepare()
if(!serverStatIf.empty()) {
e_->getRequestGroupMan()->loadServerStat(serverStatIf);
e_->getRequestGroupMan()->removeStaleServerStat
(option_->getAsInt(PREF_SERVER_STAT_TIMEOUT));
(std::chrono::seconds(option_->getAsInt(PREF_SERVER_STAT_TIMEOUT)));
}
e_->setStatCalc(getStatCalc(option_));
if(uriListParser_) {

View File

@ -897,7 +897,7 @@ bool RequestGroupMan::saveServerStat(const std::string& filename) const
return serverStatMan_->save(filename);
}
void RequestGroupMan::removeStaleServerStat(time_t timeout)
void RequestGroupMan::removeStaleServerStat(const std::chrono::seconds& timeout)
{
serverStatMan_->removeStaleServerStat(timeout);
}

View File

@ -270,7 +270,7 @@ public:
bool saveServerStat(const std::string& filename) const;
void removeStaleServerStat(time_t timeout);
void removeStaleServerStat(const std::chrono::seconds& timeout);
// Returns true if current download speed exceeds
// maxOverallDownloadSpeedLimit_. Always returns false if

View File

@ -211,7 +211,7 @@ std::string ServerStat::toString() const
getDownloadSpeed(),
getSingleConnectionAvgSpeed(),
getMultiConnectionAvgSpeed(),
getLastUpdated().getTime(),
getLastUpdated().getTimeFromEpoch(),
getCounter(),
STATUS_STRING[getStatus()]);
}

View File

@ -233,26 +233,11 @@ bool ServerStatMan::load(const std::string& filename)
return true;
}
namespace {
class FindStaleServerStat {
private:
time_t timeout_;
Time time_;
public:
FindStaleServerStat(time_t timeout):timeout_(timeout) {}
bool operator()(const std::shared_ptr<ServerStat>& ss) const
{
return ss->getLastUpdated().difference(time_) >= timeout_;
}
};
} // namespace
void ServerStatMan::removeStaleServerStat(time_t timeout)
void ServerStatMan::removeStaleServerStat(const std::chrono::seconds& timeout)
{
FindStaleServerStat finder(timeout);
for(auto i = serverStats_.begin(), eoi = serverStats_.end(); i != eoi;) {
if(finder(*i)) {
auto now = Time();
for(auto i = std::begin(serverStats_); i != std::end(serverStats_);) {
if((*i)->getLastUpdated().difference(now) >= timeout) {
serverStats_.erase(i++);
} else {
++i;

View File

@ -62,7 +62,8 @@ public:
bool save(const std::string& filename) const;
void removeStaleServerStat(time_t timeout);
void removeStaleServerStat(const std::chrono::seconds& timeout);
private:
typedef std::set<std::shared_ptr<ServerStat>,
DerefLess<std::shared_ptr<ServerStat> > > ServerStatSet;

View File

@ -2,7 +2,7 @@
/*
* aria2 - The high speed download utility
*
* Copyright (C) 2006 Tatsuhiro Tsujikawa
* Copyright (C) 2015 Tatsuhiro Tsujikawa
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@ -42,140 +42,40 @@
namespace aria2 {
Time::Time():good_(true)
Time::Time() : tp_(Clock::now()), good_(true)
{
reset();
}
Time::Time(const Time& time)
Time::Time(time_t t) : tp_(Clock::from_time_t(t)), good_(true)
{
tv_ = time.tv_;
good_ = time.good_;
}
Time::Time(time_t sec):good_(true)
void Time::reset()
{
setTimeInSec(sec);
tp_ = Clock::now();
good_ = true;
}
Time::Time(const struct timeval& tv):good_(true)
Time::Clock::duration Time::difference() const
{
tv_ = tv;
return Clock::now() - tp_;
}
Time::~Time() {}
Time& Time::operator=(const Time& time)
Time::Clock::duration Time::difference(const Time& time) const
{
if(this != &time) {
tv_ = time.tv_;
good_ = time.good_;
}
return *this;
return time.tp_ - tp_;
}
bool Time::operator<(const Time& time) const
void Time::setTimeFromEpoch(time_t t)
{
return util::difftv(time.tv_, tv_) > 0;
}
void Time::reset() {
gettimeofday(&tv_, nullptr);
}
struct timeval Time::getCurrentTime() const {
struct timeval now;
gettimeofday(&now, nullptr);
return now;
}
bool Time::elapsed(time_t sec) const {
// Because of gettimeofday called from getCurrentTime() is slow, and most of
// the time this function is called before specified time passes, we first do
// simple test using time.
// Then only when the further test is required, call gettimeofday.
time_t now = time(nullptr);
if(tv_.tv_sec+sec < now) {
return true;
} else if(tv_.tv_sec+sec == now) {
return
util::difftv(getCurrentTime(), tv_) >= static_cast<int64_t>(sec)*1000000;
} else {
return false;
}
}
bool Time::elapsedInMillis(int64_t millis) const {
return util::difftv(getCurrentTime(), tv_)/1000 >= millis;
}
bool Time::isNewer(const Time& time) const {
return util::difftv(tv_, time.tv_) > 0;
}
time_t Time::difference() const
{
return util::difftv(getCurrentTime(), tv_)/1000000;
}
time_t Time::difference(const struct timeval& now) const
{
return util::difftv(now, tv_)/1000000;
}
int64_t Time::differenceInMillis() const {
return util::difftv(getCurrentTime(), tv_)/1000;
}
int64_t Time::differenceInMillis(const struct timeval& now) const
{
return util::difftv(now, tv_)/1000;
}
bool Time::isZero() const
{
return tv_.tv_sec == 0 && tv_.tv_usec == 0;
}
int64_t Time::getTimeInMicros() const
{
return (int64_t)tv_.tv_sec*1000*1000+tv_.tv_usec;
}
int64_t Time::getTimeInMillis() const
{
return (int64_t)tv_.tv_sec*1000+tv_.tv_usec/1000;
}
time_t Time::getTime() const
{
return tv_.tv_sec;
}
void Time::setTimeInSec(time_t sec) {
tv_.tv_sec = sec;
tv_.tv_usec = 0;
}
void Time::advance(time_t sec)
{
tv_.tv_sec += sec;
}
bool Time::good() const
{
return good_;
}
bool Time::bad() const
{
return !good_;
tp_ = Clock::from_time_t(t);
good_ = true;
}
std::string Time::toHTTPDate() const
{
char buf[32];
time_t t = getTime();
time_t t = getTimeFromEpoch();
struct tm* tms = gmtime(&t); // returned struct is statically allocated.
size_t r = strftime(buf, sizeof(buf), "%a, %d %b %Y %H:%M:%S GMT", tms);
return std::string(&buf[0], &buf[r]);
@ -241,11 +141,4 @@ Time Time::parseHTTPDate(const std::string& datetime)
return Time::null();
}
Time Time::null()
{
Time t(0);
t.good_ = false;
return t;
}
} // namespace aria2

View File

@ -2,7 +2,7 @@
/*
* aria2 - The high speed download utility
*
* Copyright (C) 2006 Tatsuhiro Tsujikawa
* Copyright (C) 2015 Tatsuhiro Tsujikawa
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@ -46,71 +46,45 @@
namespace aria2 {
class Time {
private:
struct timeval tv_;
bool good_;
struct timeval getCurrentTime() const;
public:
using Clock = std::chrono::system_clock;
// The time value is initialized so that it represents the time at which
// this object was created.
Time();
Time(const Time& time);
Time(const Time& time) = default;
Time(Time&& time) = default;
Time(time_t sec);
Time(const struct timeval& tv);
~Time();
Time& operator=(const Time& time) = default;
Time& operator=(Time&& time) = default;
Time& operator=(const Time& time);
bool operator<(const Time& time) const;
bool operator<(const Time& time) const { return tp_ < time.tp_; }
bool operator>(const Time& time) const { return time < *this; }
bool operator<=(const Time& time) const { return !(time < *this); }
bool operator>=(const Time& time) const { return !(*this < time); }
// Makes this object's time value up to date.
void reset();
bool elapsed(time_t sec) const;
Clock::duration difference() const;
Clock::duration difference(const Time& now) const;
bool elapsedInMillis(int64_t millis) const;
const Clock::time_point& getTime() const { return tp_; }
time_t difference() const;
void setTimeFromEpoch(time_t sec);
time_t getTimeFromEpoch() const { return Clock::to_time_t(tp_); }
time_t difference(const struct timeval& now) const;
time_t difference(const Time& now) const
template <typename duration>
void advance(const duration& t)
{
return difference(now.tv_);
tp_ += t;
}
int64_t differenceInMillis() const;
bool good() const { return good_; }
bool bad() const { return !good_; }
int64_t differenceInMillis(const struct timeval& now) const;
int64_t differenceInMillis(const Time& now) const
{
return differenceInMillis(now.tv_);
}
// Returns true if this object's time value is zero.
bool isZero() const;
int64_t getTimeInMicros() const;
int64_t getTimeInMillis() const;
// Returns this object's time value in seconds.
time_t getTime() const;
void setTimeInSec(time_t sec);
bool isNewer(const Time& time) const;
void advance(time_t sec);
bool good() const;
// Returns !good()
bool bad() const;
static Time null() { return Time(0, false); }
std::string toHTTPDate() const;
@ -142,7 +116,11 @@ public:
// these functions.
static Time parseHTTPDate(const std::string& datetime);
static Time null();
private:
Time(time_t t, bool good) : tp_(Clock::from_time_t(t)), good_(good) {}
Clock::time_point tp_;
bool good_;
};
} // namespace aria2

View File

@ -60,8 +60,6 @@ void DHTRoutingTableDeserializerTest::testDeserialize()
CPPUNIT_ASSERT(memcmp(localNode->getID(), d.getLocalNode()->getID(),
DHT_ID_LENGTH) == 0);
std::cout << d.getSerializedTime().getTime() << std::endl;
CPPUNIT_ASSERT_EQUAL((size_t)2, d.getNodes().size());
const std::vector<std::shared_ptr<DHTNode> >& dsnodes = d.getNodes();
CPPUNIT_ASSERT_EQUAL(std::string("192.168.0.1"), dsnodes[0]->getIPAddress());
@ -97,8 +95,6 @@ void DHTRoutingTableDeserializerTest::testDeserialize6()
CPPUNIT_ASSERT(memcmp(localNode->getID(), d.getLocalNode()->getID(),
DHT_ID_LENGTH) == 0);
std::cout << d.getSerializedTime().getTime() << std::endl;
CPPUNIT_ASSERT_EQUAL((size_t)2, d.getNodes().size());
const std::vector<std::shared_ptr<DHTNode> >& dsnodes = d.getNodes();
CPPUNIT_ASSERT_EQUAL(std::string("2001::1001"), dsnodes[0]->getIPAddress());

View File

@ -261,7 +261,7 @@ void FileTest::testUtime()
a2_struct_stat buf;
CPPUNIT_ASSERT(0 == a2stat(utf8ToWChar(f.getPath()).c_str(), &buf));
CPPUNIT_ASSERT_EQUAL((time_t)atime, (time_t)buf.st_atime);
CPPUNIT_ASSERT_EQUAL((time_t)mtime, f.getModifiedTime().getTime());
CPPUNIT_ASSERT_EQUAL((time_t)mtime, f.getModifiedTime().getTimeFromEpoch());
File notFound(A2_TEST_OUT_DIR"/aria2_FileTest_testUTime_notFound");
notFound.remove();

View File

@ -164,7 +164,7 @@ void FtpConnectionTest::testReceiveMdtmResponse()
serverSocket_->writeData("\r\n");
waitRead(clientSocket_);
CPPUNIT_ASSERT_EQUAL(213, ftp_->receiveMdtmResponse(t));
CPPUNIT_ASSERT_EQUAL((time_t)1220877792, t.getTime());
CPPUNIT_ASSERT_EQUAL((time_t)1220877792, t.getTimeFromEpoch());
}
{
// see milli second part is ignored
@ -172,7 +172,7 @@ void FtpConnectionTest::testReceiveMdtmResponse()
serverSocket_->writeData("213 20080908124312.014\r\n");
waitRead(clientSocket_);
CPPUNIT_ASSERT_EQUAL(213, ftp_->receiveMdtmResponse(t));
CPPUNIT_ASSERT_EQUAL((time_t)1220877792, t.getTime());
CPPUNIT_ASSERT_EQUAL((time_t)1220877792, t.getTimeFromEpoch());
}
{
// hhmmss part is missing
@ -190,10 +190,10 @@ void FtpConnectionTest::testReceiveMdtmResponse()
CPPUNIT_ASSERT_EQUAL(213, ftp_->receiveMdtmResponse(t));
#ifdef HAVE_TIMEGM
// Time will be normalized. Wed Jul 8 12:43:12 2009
CPPUNIT_ASSERT_EQUAL((time_t)1247056992, t.getTime());
CPPUNIT_ASSERT_EQUAL((time_t)1247056992, t.getTimeFromEpoch());
#else // !HAVE_TIMEGM
// The replacement timegm does not normalize.
CPPUNIT_ASSERT_EQUAL((time_t)-1, t.getTime());
CPPUNIT_ASSERT_EQUAL((time_t)-1, t.getTimeFromEpoch());
#endif // !HAVE_TIMEGM
}
{

View File

@ -427,14 +427,17 @@ void MultiDiskAdaptorTest::testUtime()
CPPUNIT_ASSERT_EQUAL((size_t)2, adaptor.utime(Time(atime), Time(mtime)));
CPPUNIT_ASSERT_EQUAL((time_t)mtime,
File(entries[0]->getPath()).getModifiedTime().getTime());
CPPUNIT_ASSERT_EQUAL(
(time_t)mtime,
File(entries[0]->getPath()).getModifiedTime().getTimeFromEpoch());
CPPUNIT_ASSERT_EQUAL((time_t)mtime,
File(entries[3]->getPath()).getModifiedTime().getTime());
CPPUNIT_ASSERT_EQUAL(
(time_t)mtime,
File(entries[3]->getPath()).getModifiedTime().getTimeFromEpoch());
CPPUNIT_ASSERT((time_t)mtime !=
File(entries[2]->getPath()).getModifiedTime().getTime());
CPPUNIT_ASSERT(
(time_t)mtime !=
File(entries[2]->getPath()).getModifiedTime().getTimeFromEpoch());
}
void MultiDiskAdaptorTest::testWriteCache()

View File

@ -132,7 +132,7 @@ void ServerStatManTest::testLoad()
CPPUNIT_ASSERT_EQUAL(102, localhost_http->getMultiConnectionAvgSpeed());
CPPUNIT_ASSERT_EQUAL(6, localhost_http->getCounter());
CPPUNIT_ASSERT_EQUAL(static_cast<time_t>(1210000000),
localhost_http->getLastUpdated().getTime());
localhost_http->getLastUpdated().getTimeFromEpoch());
CPPUNIT_ASSERT_EQUAL(ServerStat::OK, localhost_http->getStatus());
std::shared_ptr<ServerStat> mirror = ssm.find("mirror", "http");
@ -159,7 +159,7 @@ void ServerStatManTest::testRemoveStaleServerStat()
CPPUNIT_ASSERT(ssm.add(localhost_ftp));
CPPUNIT_ASSERT(ssm.add(mirror));
ssm.removeStaleServerStat(24*60*60);
ssm.removeStaleServerStat(std::chrono::hours(24));
CPPUNIT_ASSERT(ssm.find("localhost", "http"));
CPPUNIT_ASSERT(!ssm.find("localhost", "ftp"));

View File

@ -18,7 +18,6 @@ class TimeTest:public CppUnit::TestFixture {
CPPUNIT_TEST(testParseAsctime);
CPPUNIT_TEST(testParseHTTPDate);
CPPUNIT_TEST(testOperatorLess);
CPPUNIT_TEST(testElapsed);
CPPUNIT_TEST(testToHTTPDate);
CPPUNIT_TEST_SUITE_END();
public:
@ -33,7 +32,6 @@ public:
void testParseAsctime();
void testParseHTTPDate();
void testOperatorLess();
void testElapsed();
void testToHTTPDate();
};
@ -43,47 +41,47 @@ 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());
CPPUNIT_ASSERT_EQUAL((time_t)1220714793, t1.getTimeFromEpoch());
}
void TimeTest::testParseRFC1123Alt()
{
Time t1 = Time::parseRFC1123Alt("Sat, 06 Sep 2008 15:26:33 +0000");
CPPUNIT_ASSERT_EQUAL((time_t)1220714793, t1.getTime());
CPPUNIT_ASSERT_EQUAL((time_t)1220714793, t1.getTimeFromEpoch());
}
void TimeTest::testParseRFC850()
{
Time t1 = Time::parseRFC850("Saturday, 06-Sep-08 15:26:33 GMT");
CPPUNIT_ASSERT_EQUAL((time_t)1220714793, t1.getTime());
CPPUNIT_ASSERT_EQUAL((time_t)1220714793, t1.getTimeFromEpoch());
}
void TimeTest::testParseRFC850Ext()
{
Time t1 = Time::parseRFC850Ext("Saturday, 06-Sep-2008 15:26:33 GMT");
CPPUNIT_ASSERT_EQUAL((time_t)1220714793, t1.getTime());
CPPUNIT_ASSERT_EQUAL((time_t)1220714793, t1.getTimeFromEpoch());
}
void TimeTest::testParseAsctime()
{
Time t1 = Time::parseAsctime("Sun Sep 6 15:26:33 2008");
CPPUNIT_ASSERT_EQUAL((time_t)1220714793, t1.getTime());
CPPUNIT_ASSERT_EQUAL((time_t)1220714793, t1.getTimeFromEpoch());
}
void TimeTest::testParseHTTPDate()
{
CPPUNIT_ASSERT_EQUAL((time_t)1220714793,
Time::parseHTTPDate
("Sat, 06 Sep 2008 15:26:33 GMT").getTime());
("Sat, 06 Sep 2008 15:26:33 GMT").getTimeFromEpoch());
CPPUNIT_ASSERT_EQUAL((time_t)1220714793,
Time::parseHTTPDate
("Sat, 06-Sep-2008 15:26:33 GMT").getTime());
("Sat, 06-Sep-2008 15:26:33 GMT").getTimeFromEpoch());
CPPUNIT_ASSERT_EQUAL((time_t)1220714793,
Time::parseHTTPDate
("Sat, 06-Sep-08 15:26:33 GMT").getTime());
("Sat, 06-Sep-08 15:26:33 GMT").getTimeFromEpoch());
CPPUNIT_ASSERT_EQUAL((time_t)1220714793,
Time::parseHTTPDate
("Sun Sep 6 15:26:33 2008").getTime());
("Sun Sep 6 15:26:33 2008").getTimeFromEpoch());
CPPUNIT_ASSERT(Time::parseHTTPDate
("Sat, 2008-09-06 15:26:33 GMT").bad());
}
@ -93,17 +91,6 @@ void TimeTest::testOperatorLess()
CPPUNIT_ASSERT(Time(1) < Time(2));
CPPUNIT_ASSERT(!(Time(1) < Time(1)));
CPPUNIT_ASSERT(!(Time(2) < Time(1)));
struct timeval tv1;
tv1.tv_sec = 0;
tv1.tv_usec = 1;
struct timeval tv2;
tv2.tv_sec = 1;
tv2.tv_usec = 0;
CPPUNIT_ASSERT(Time(tv1) < Time(tv2));
tv2.tv_sec = 0;
CPPUNIT_ASSERT(Time(tv2) < Time(tv1));
}
void TimeTest::testToHTTPDate()
@ -117,30 +104,4 @@ void TimeTest::testToHTTPDate()
#endif // !__MINGW32__
}
void TimeTest::testElapsed()
{
struct timeval now;
gettimeofday(&now, nullptr);
{
struct timeval tv = now;
CPPUNIT_ASSERT(!Time(tv).elapsed(1));
}
{
struct timeval tv;
suseconds_t usec = now.tv_usec+500000;
if(usec > 999999) {
tv.tv_sec = now.tv_sec+usec/1000000;
tv.tv_usec = usec%1000000;
} else {
tv.tv_sec = now.tv_sec;
tv.tv_usec = usec;
}
CPPUNIT_ASSERT(!Time(tv).elapsed(1));
}
{
struct timeval tv = { now.tv_sec-2, now.tv_usec };
CPPUNIT_ASSERT(Time(tv).elapsed(1));
}
}
} // namespace aria2