lokinet/llarp/util/time.cpp
Jeff Becker 7ad47f2dba
* get rid of dht explore for service nodes
* add Time_t using std::chrono for future uses
* make decaying hashset constructor with llarp_time_t explicit
* add decaying hashset implicit constructor using Time_t
* add timeouts for gossiper replay
* allow regossip of our RC
2020-02-12 12:10:48 -05:00

70 lines
1.4 KiB
C++

#include <util/time.hpp>
#include <chrono>
#include <util/logging/logger.hpp>
namespace llarp
{
using Clock_t = std::chrono::system_clock;
template < typename Res, typename Clock >
static llarp_time_t
time_since_epoch()
{
return std::chrono::duration_cast< Res >(Clock::now().time_since_epoch())
.count();
}
const static llarp_time_t started_at_system =
time_since_epoch< std::chrono::milliseconds, Clock_t >();
const static llarp_time_t started_at_steady =
time_since_epoch< std::chrono::milliseconds,
std::chrono::steady_clock >();
/// get our uptime in ms
static llarp_time_t
time_since_started()
{
return time_since_epoch< std::chrono::milliseconds,
std::chrono::steady_clock >()
- started_at_steady;
}
Time_t
time_now()
{
return Time_t(time_now_ms());
}
llarp_time_t
time_now_ms()
{
static llarp_time_t lastTime = 0;
auto t = time_since_started();
#ifdef TESTNET_SPEED
t /= TESTNET_SPEED;
#endif
t += started_at_system;
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;
}
} // namespace llarp