2021-03-09 22:24:35 +00:00
|
|
|
#pragma once
|
2018-10-08 11:56:40 +00:00
|
|
|
|
2021-03-09 22:24:35 +00:00
|
|
|
#include "types.hpp"
|
2020-02-25 17:05:13 +00:00
|
|
|
#include <nlohmann/json.hpp>
|
2021-04-02 15:10:37 +00:00
|
|
|
#include <iostream>
|
2022-07-16 00:41:14 +00:00
|
|
|
#include <fmt/format.h>
|
|
|
|
#include <fmt/chrono.h>
|
2018-11-19 22:45:37 +00:00
|
|
|
|
2020-02-24 19:40:45 +00:00
|
|
|
using namespace std::chrono_literals;
|
2020-01-30 22:10:56 +00:00
|
|
|
|
2018-10-08 11:56:40 +00:00
|
|
|
namespace llarp
|
|
|
|
{
|
2020-01-23 14:41:33 +00:00
|
|
|
/// get time right now as milliseconds, this is monotonic
|
2021-04-02 15:10:37 +00:00
|
|
|
Duration_t
|
2018-11-19 22:45:37 +00:00
|
|
|
time_now_ms();
|
2020-02-25 17:05:13 +00:00
|
|
|
|
2021-04-02 15:10:37 +00:00
|
|
|
/// get the uptime of the process
|
|
|
|
Duration_t
|
|
|
|
uptime();
|
|
|
|
|
|
|
|
/// convert to milliseconds
|
|
|
|
uint64_t
|
|
|
|
ToMS(Duration_t duration);
|
|
|
|
|
2020-02-25 17:05:13 +00:00
|
|
|
nlohmann::json
|
2021-04-02 15:10:37 +00:00
|
|
|
to_json(const Duration_t& t);
|
2020-01-30 22:10:56 +00:00
|
|
|
|
2022-05-20 15:11:34 +00:00
|
|
|
template <typename Time_Duration>
|
|
|
|
struct time_delta
|
|
|
|
{
|
|
|
|
const TimePoint_t at;
|
2022-07-16 00:41:14 +00:00
|
|
|
};
|
|
|
|
} // namespace llarp
|
2022-05-20 15:11:34 +00:00
|
|
|
|
2022-07-16 00:41:14 +00:00
|
|
|
namespace fmt
|
|
|
|
{
|
|
|
|
template <typename Time_Duration>
|
|
|
|
struct formatter<llarp::time_delta<Time_Duration>> : formatter<std::string>
|
|
|
|
{
|
|
|
|
template <typename FormatContext>
|
|
|
|
auto
|
|
|
|
format(const llarp::time_delta<Time_Duration>& td, FormatContext& ctx)
|
2022-05-20 15:11:34 +00:00
|
|
|
{
|
2022-07-16 00:41:14 +00:00
|
|
|
const auto dlt =
|
|
|
|
std::chrono::duration_cast<llarp::Duration_t>(llarp::TimePoint_t::clock::now() - td.at);
|
|
|
|
using Parent = formatter<std::string>;
|
2022-05-20 15:11:34 +00:00
|
|
|
if (dlt > 0s)
|
2022-07-16 00:41:14 +00:00
|
|
|
return Parent::format(fmt::format("{} ago", dlt), ctx);
|
|
|
|
if (dlt < 0s)
|
|
|
|
return Parent::format(fmt::format("in {}", -dlt), ctx);
|
|
|
|
return Parent::format("now", ctx);
|
2022-05-20 15:11:34 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-07-16 00:41:14 +00:00
|
|
|
template <>
|
|
|
|
struct formatter<llarp::Duration_t> : formatter<std::string>
|
2022-05-20 15:11:34 +00:00
|
|
|
{
|
2022-07-16 00:41:14 +00:00
|
|
|
template <typename FormatContext>
|
|
|
|
auto
|
|
|
|
format(llarp::Duration_t elapsed, FormatContext& ctx)
|
|
|
|
{
|
|
|
|
bool neg = elapsed < 0s;
|
|
|
|
if (neg)
|
|
|
|
elapsed = -elapsed;
|
|
|
|
const auto hours = std::chrono::duration_cast<std::chrono::hours>(elapsed).count();
|
|
|
|
const auto mins = (std::chrono::duration_cast<std::chrono::minutes>(elapsed) % 1h).count();
|
|
|
|
const auto secs = (std::chrono::duration_cast<std::chrono::seconds>(elapsed) % 1min).count();
|
|
|
|
const auto ms = (std::chrono::duration_cast<std::chrono::milliseconds>(elapsed) % 1s).count();
|
|
|
|
return formatter<std::string>::format(
|
|
|
|
fmt::format(
|
|
|
|
elapsed >= 1h ? "{0}{1:d}h{2:02d}m{3:02d}.{4:03d}s"
|
|
|
|
: elapsed >= 1min ? "{0}{2:d}m{3:02d}.{4:03d}s"
|
|
|
|
: "{0}{3:d}.{4:03d}s",
|
|
|
|
neg ? "-" : "",
|
|
|
|
hours,
|
|
|
|
mins,
|
|
|
|
secs,
|
|
|
|
ms),
|
|
|
|
ctx);
|
|
|
|
}
|
|
|
|
};
|
2022-05-20 15:11:34 +00:00
|
|
|
|
2022-07-16 00:41:14 +00:00
|
|
|
template <>
|
|
|
|
struct formatter<llarp::TimePoint_t> : formatter<std::string>
|
2022-05-20 15:11:34 +00:00
|
|
|
{
|
2022-07-16 00:41:14 +00:00
|
|
|
template <typename FormatContext>
|
|
|
|
auto
|
|
|
|
format(const llarp::TimePoint_t& tp, FormatContext& ctx)
|
|
|
|
{
|
|
|
|
return formatter<std::string>::format(fmt::format("{:%c %Z}", tp), ctx);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
} // namespace fmt
|