Changeset View
Changeset View
Standalone View
Standalone View
src/utiltime.cpp
Show All 26 Lines | int64_t GetTime() { | ||||
assert(now > 0); | assert(now > 0); | ||||
return now; | return now; | ||||
} | } | ||||
void SetMockTime(int64_t nMockTimeIn) { | void SetMockTime(int64_t nMockTimeIn) { | ||||
nMockTime.store(nMockTimeIn, std::memory_order_relaxed); | nMockTime.store(nMockTimeIn, std::memory_order_relaxed); | ||||
} | } | ||||
int64_t GetMockTime() { | |||||
return nMockTime.load(std::memory_order_relaxed); | |||||
} | |||||
int64_t GetTimeMillis() { | int64_t GetTimeMillis() { | ||||
int64_t now = (boost::posix_time::microsec_clock::universal_time() - | int64_t now = (boost::posix_time::microsec_clock::universal_time() - | ||||
boost::posix_time::ptime(boost::gregorian::date(1970, 1, 1))) | boost::posix_time::ptime(boost::gregorian::date(1970, 1, 1))) | ||||
.total_milliseconds(); | .total_milliseconds(); | ||||
assert(now > 0); | assert(now > 0); | ||||
return now; | return now; | ||||
} | } | ||||
int64_t GetTimeMicros() { | int64_t GetTimeMicros() { | ||||
int64_t now = (boost::posix_time::microsec_clock::universal_time() - | int64_t now = (boost::posix_time::microsec_clock::universal_time() - | ||||
boost::posix_time::ptime(boost::gregorian::date(1970, 1, 1))) | boost::posix_time::ptime(boost::gregorian::date(1970, 1, 1))) | ||||
.total_microseconds(); | .total_microseconds(); | ||||
assert(now > 0); | assert(now > 0); | ||||
return now; | return now; | ||||
} | } | ||||
int64_t GetSystemTimeInSeconds() { | int64_t GetSystemTimeInSeconds() { | ||||
return GetTimeMicros() / 1000000; | return GetTimeMicros() / 1000000; | ||||
} | } | ||||
/** Return a time useful for the debug log */ | |||||
int64_t GetLogTimeMicros() { | |||||
int64_t mocktime = nMockTime.load(std::memory_order_relaxed); | |||||
if (mocktime) { | |||||
return mocktime * 1000000; | |||||
} | |||||
return GetTimeMicros(); | |||||
} | |||||
void MilliSleep(int64_t n) { | void MilliSleep(int64_t n) { | ||||
boost::this_thread::sleep_for(boost::chrono::milliseconds(n)); | boost::this_thread::sleep_for(boost::chrono::milliseconds(n)); | ||||
} | } | ||||
std::string DateTimeStrFormat(const char *pszFormat, int64_t nTime) { | std::string DateTimeStrFormat(const char *pszFormat, int64_t nTime) { | ||||
static std::locale classic(std::locale::classic()); | static std::locale classic(std::locale::classic()); | ||||
// std::locale takes ownership of the pointer | // std::locale takes ownership of the pointer | ||||
std::locale loc(classic, new boost::posix_time::time_facet(pszFormat)); | std::locale loc(classic, new boost::posix_time::time_facet(pszFormat)); | ||||
std::stringstream ss; | std::stringstream ss; | ||||
ss.imbue(loc); | ss.imbue(loc); | ||||
ss << boost::posix_time::from_time_t(nTime); | ss << boost::posix_time::from_time_t(nTime); | ||||
return ss.str(); | return ss.str(); | ||||
} | } |