2019-01-10 19:41:51 +00:00
|
|
|
#include <util/time.hpp>
|
2019-11-05 16:58:53 +00:00
|
|
|
#include <chrono>
|
|
|
|
#include <util/logging/logger.hpp>
|
2018-01-19 16:51:27 +00:00
|
|
|
|
2018-10-04 13:02:03 +00:00
|
|
|
namespace llarp
|
|
|
|
{
|
2019-11-05 16:58:53 +00:00
|
|
|
using Clock_t = std::chrono::system_clock;
|
|
|
|
|
2020-01-23 12:59:51 +00:00
|
|
|
template < typename Res, typename Clock >
|
2018-10-04 13:02:03 +00:00
|
|
|
static llarp_time_t
|
|
|
|
time_since_epoch()
|
|
|
|
{
|
2020-01-23 12:59:51 +00:00
|
|
|
return std::chrono::duration_cast< Res >(Clock::now().time_since_epoch())
|
2018-10-04 13:02:03 +00:00
|
|
|
.count();
|
|
|
|
}
|
|
|
|
|
2020-01-23 14:41:33 +00:00
|
|
|
const static llarp_time_t started_at_system =
|
|
|
|
time_since_epoch< std::chrono::milliseconds, Clock_t >();
|
2020-01-23 12:59:51 +00:00
|
|
|
|
2020-01-23 14:41:33 +00:00
|
|
|
const static llarp_time_t started_at_steady =
|
|
|
|
time_since_epoch< std::chrono::milliseconds,
|
|
|
|
std::chrono::steady_clock >();
|
|
|
|
/// get our uptime in ms
|
2020-01-23 12:59:51 +00:00
|
|
|
static llarp_time_t
|
|
|
|
time_since_started()
|
|
|
|
{
|
|
|
|
return time_since_epoch< std::chrono::milliseconds,
|
|
|
|
std::chrono::steady_clock >()
|
2020-01-23 14:41:33 +00:00
|
|
|
- started_at_steady;
|
2020-01-23 12:59:51 +00:00
|
|
|
}
|
|
|
|
|
2018-11-19 22:45:37 +00:00
|
|
|
llarp_time_t
|
|
|
|
time_now_ms()
|
|
|
|
{
|
2019-11-05 16:58:53 +00:00
|
|
|
static llarp_time_t lastTime = 0;
|
2020-01-23 12:59:51 +00:00
|
|
|
auto t = time_since_started();
|
|
|
|
#ifdef TESTNET_SPEED
|
|
|
|
t /= TESTNET_SPEED;
|
|
|
|
#endif
|
2020-01-23 14:41:33 +00:00
|
|
|
t += started_at_system;
|
2020-01-23 12:59:51 +00:00
|
|
|
|
2019-11-05 16:58:53 +00:00
|
|
|
if(t <= lastTime)
|
|
|
|
{
|
|
|
|
return lastTime;
|
|
|
|
}
|
|
|
|
if(lastTime == 0)
|
|
|
|
{
|
|
|
|
lastTime = t;
|
|
|
|
}
|
|
|
|
const auto dlt = t - lastTime;
|
|
|
|
if(dlt > 5000)
|
|
|
|
{
|
|
|
|
// big timeskip
|
|
|
|
t = lastTime;
|
|
|
|
lastTime = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
lastTime = t;
|
|
|
|
}
|
|
|
|
return t;
|
2018-11-19 22:45:37 +00:00
|
|
|
}
|
2018-10-04 13:02:03 +00:00
|
|
|
} // namespace llarp
|