lokinet/llarp/util/time.cpp

49 lines
1.0 KiB
C++
Raw Normal View History

#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;
2018-10-04 13:02:03 +00:00
template < typename Res >
static llarp_time_t
time_since_epoch()
{
return std::chrono::duration_cast< Res >(
llarp::Clock_t::now().time_since_epoch())
.count();
}
2018-11-19 22:45:37 +00:00
// use std::chrono because otherwise the network breaks with Daylight Savings
// this time, it doesn't get truncated -despair
// that concern is what drove me back to the POSIX C time functions
// in the first place
llarp_time_t
time_now_ms()
{
2019-11-05 16:58:53 +00:00
static llarp_time_t lastTime = 0;
auto t = llarp::time_since_epoch< std::chrono::milliseconds >();
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