aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMarcoFalke <falke.marco@gmail.com>2020-02-21 13:12:40 -0500
committerfanquake <fanquake@gmail.com>2021-02-17 12:26:39 +0800
commit9266f7497f256d780178829e0f3a29ddaeb794ba (patch)
treea17a9dd6fd98b40b00faa3ae2e4f52d7d49bd00b /src
parent3c2e16be22ae04bf56663ee5ec1554d0d569741b (diff)
util: Use std::chrono for time getters
Diffstat (limited to 'src')
-rw-r--r--src/util/time.cpp20
1 files changed, 11 insertions, 9 deletions
diff --git a/src/util/time.cpp b/src/util/time.cpp
index 9631c115e9..06ed8f4516 100644
--- a/src/util/time.cpp
+++ b/src/util/time.cpp
@@ -90,6 +90,14 @@ template std::chrono::seconds GetTime();
template std::chrono::milliseconds GetTime();
template std::chrono::microseconds GetTime();
+template <typename T>
+static T GetSystemTime()
+{
+ const auto now = std::chrono::duration_cast<T>(std::chrono::system_clock::now().time_since_epoch());
+ assert(now.count() > 0);
+ return now;
+}
+
void SetMockTime(int64_t nMockTimeIn)
{
Assert(nMockTimeIn >= 0);
@@ -103,23 +111,17 @@ int64_t GetMockTime()
int64_t GetTimeMillis()
{
- int64_t now = (boost::posix_time::microsec_clock::universal_time() -
- boost::posix_time::ptime(boost::gregorian::date(1970,1,1))).total_milliseconds();
- assert(now > 0);
- return now;
+ return int64_t{GetSystemTime<std::chrono::milliseconds>().count()};
}
int64_t GetTimeMicros()
{
- int64_t now = (boost::posix_time::microsec_clock::universal_time() -
- boost::posix_time::ptime(boost::gregorian::date(1970,1,1))).total_microseconds();
- assert(now > 0);
- return now;
+ return int64_t{GetSystemTime<std::chrono::microseconds>().count()};
}
int64_t GetSystemTimeInSeconds()
{
- return GetTimeMicros()/1000000;
+ return int64_t{GetSystemTime<std::chrono::seconds>().count()};
}
std::string FormatISO8601DateTime(int64_t nTime) {