2021-03-09 22:24:35 +00:00
|
|
|
#include "time.hpp"
|
2019-11-05 16:58:53 +00:00
|
|
|
#include <chrono>
|
2021-03-09 22:24:35 +00:00
|
|
|
#include <llarp/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-04-07 18:38:56 +00:00
|
|
|
template <typename Res, typename Clock>
|
2018-10-04 13:02:03 +00:00
|
|
|
static llarp_time_t
|
|
|
|
time_since_epoch()
|
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
return std::chrono::duration_cast<Res>(Clock::now().time_since_epoch());
|
2018-10-04 13:02:03 +00:00
|
|
|
}
|
|
|
|
|
2020-01-23 14:41:33 +00:00
|
|
|
const static llarp_time_t started_at_system =
|
2020-04-07 18:38:56 +00:00
|
|
|
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 =
|
2020-04-07 18:38:56 +00:00
|
|
|
time_since_epoch<std::chrono::milliseconds, std::chrono::steady_clock>();
|
2020-01-23 14:41:33 +00:00
|
|
|
/// get our uptime in ms
|
2020-01-23 12:59:51 +00:00
|
|
|
static llarp_time_t
|
|
|
|
time_since_started()
|
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
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()
|
|
|
|
{
|
2020-02-24 19:40:45 +00:00
|
|
|
static llarp_time_t lastTime = 0s;
|
2020-04-07 18:38:56 +00:00
|
|
|
auto t = time_since_started();
|
2020-01-23 12:59:51 +00:00
|
|
|
#ifdef TESTNET_SPEED
|
2020-02-24 19:40:45 +00:00
|
|
|
t /= uint64_t(TESTNET_SPEED);
|
2020-01-23 12:59:51 +00:00
|
|
|
#endif
|
2020-01-23 14:41:33 +00:00
|
|
|
t += started_at_system;
|
2020-01-23 12:59:51 +00:00
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
if (t <= lastTime)
|
2019-11-05 16:58:53 +00:00
|
|
|
{
|
|
|
|
return lastTime;
|
|
|
|
}
|
2020-04-07 18:38:56 +00:00
|
|
|
if (lastTime == 0s)
|
2019-11-05 16:58:53 +00:00
|
|
|
{
|
|
|
|
lastTime = t;
|
|
|
|
}
|
|
|
|
const auto dlt = t - lastTime;
|
2020-04-07 18:38:56 +00:00
|
|
|
if (dlt > 5s)
|
2019-11-05 16:58:53 +00:00
|
|
|
{
|
|
|
|
// big timeskip
|
2020-04-07 18:38:56 +00:00
|
|
|
t = lastTime;
|
2020-02-24 19:40:45 +00:00
|
|
|
lastTime = 0s;
|
2019-11-05 16:58:53 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
lastTime = t;
|
|
|
|
}
|
|
|
|
return t;
|
2018-11-19 22:45:37 +00:00
|
|
|
}
|
2020-02-25 17:05:13 +00:00
|
|
|
|
|
|
|
nlohmann::json
|
2020-04-07 18:38:56 +00:00
|
|
|
to_json(const llarp_time_t& t)
|
2020-02-25 17:05:13 +00:00
|
|
|
{
|
|
|
|
return t.count();
|
|
|
|
}
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
std::ostream&
|
|
|
|
operator<<(std::ostream& out, const llarp_time_t& t)
|
2020-02-25 17:05:13 +00:00
|
|
|
{
|
|
|
|
std::chrono::milliseconds amount = t;
|
2020-04-07 18:38:56 +00:00
|
|
|
auto h = std::chrono::duration_cast<std::chrono::hours>(amount);
|
2020-02-25 17:05:13 +00:00
|
|
|
amount -= h;
|
2020-04-07 18:38:56 +00:00
|
|
|
auto m = std::chrono::duration_cast<std::chrono::minutes>(amount);
|
2020-02-25 17:05:13 +00:00
|
|
|
amount -= m;
|
2020-04-07 18:38:56 +00:00
|
|
|
auto s = std::chrono::duration_cast<std::chrono::seconds>(amount);
|
2020-02-25 17:05:13 +00:00
|
|
|
amount -= s;
|
2020-04-07 18:38:56 +00:00
|
|
|
auto ms = amount;
|
2020-02-25 17:05:13 +00:00
|
|
|
auto old_fill = out.fill('0');
|
2020-04-07 18:38:56 +00:00
|
|
|
if (h > 0h)
|
2020-02-25 17:05:13 +00:00
|
|
|
{
|
|
|
|
out << h.count() << 'h';
|
|
|
|
out.width(2); // 0-fill minutes if we have hours
|
|
|
|
}
|
2020-04-07 18:38:56 +00:00
|
|
|
if (h > 0h || m > 0min)
|
2020-02-25 17:05:13 +00:00
|
|
|
{
|
|
|
|
out << m.count() << 'm';
|
|
|
|
out.width(2); // 0-fill seconds if we have minutes
|
|
|
|
}
|
|
|
|
out << s.count() << '.';
|
|
|
|
out.width(3);
|
|
|
|
out << ms.count();
|
|
|
|
out.fill(old_fill);
|
|
|
|
return out << "s";
|
|
|
|
}
|
2018-10-04 13:02:03 +00:00
|
|
|
} // namespace llarp
|