Skip to content

Commit 9266f74

Browse files
MarcoFalkefanquake
authored andcommitted
util: Use std::chrono for time getters
1 parent 3c2e16b commit 9266f74

File tree

1 file changed

+11
-9
lines changed

1 file changed

+11
-9
lines changed

src/util/time.cpp

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,14 @@ template std::chrono::seconds GetTime();
9090
template std::chrono::milliseconds GetTime();
9191
template std::chrono::microseconds GetTime();
9292

93+
template <typename T>
94+
static T GetSystemTime()
95+
{
96+
const auto now = std::chrono::duration_cast<T>(std::chrono::system_clock::now().time_since_epoch());
97+
assert(now.count() > 0);
98+
return now;
99+
}
100+
93101
void SetMockTime(int64_t nMockTimeIn)
94102
{
95103
Assert(nMockTimeIn >= 0);
@@ -103,23 +111,17 @@ int64_t GetMockTime()
103111

104112
int64_t GetTimeMillis()
105113
{
106-
int64_t now = (boost::posix_time::microsec_clock::universal_time() -
107-
boost::posix_time::ptime(boost::gregorian::date(1970,1,1))).total_milliseconds();
108-
assert(now > 0);
109-
return now;
114+
return int64_t{GetSystemTime<std::chrono::milliseconds>().count()};
110115
}
111116

112117
int64_t GetTimeMicros()
113118
{
114-
int64_t now = (boost::posix_time::microsec_clock::universal_time() -
115-
boost::posix_time::ptime(boost::gregorian::date(1970,1,1))).total_microseconds();
116-
assert(now > 0);
117-
return now;
119+
return int64_t{GetSystemTime<std::chrono::microseconds>().count()};
118120
}
119121

120122
int64_t GetSystemTimeInSeconds()
121123
{
122-
return GetTimeMicros()/1000000;
124+
return int64_t{GetSystemTime<std::chrono::seconds>().count()};
123125
}
124126

125127
std::string FormatISO8601DateTime(int64_t nTime) {

0 commit comments

Comments
 (0)