2021-03-09 22:24:35 +00:00
|
|
|
#include "time.hpp"
|
2019-11-05 16:58:53 +00:00
|
|
|
#include <chrono>
|
2022-05-04 14:37:35 +00:00
|
|
|
#include <iomanip>
|
2018-01-19 16:51:27 +00:00
|
|
|
|
2018-10-04 13:02:03 +00:00
|
|
|
namespace llarp
|
|
|
|
{
|
2022-05-04 14:37:35 +00:00
|
|
|
namespace
|
2018-10-04 13:02:03 +00:00
|
|
|
{
|
2022-05-04 14:37:35 +00:00
|
|
|
using Clock_t = std::chrono::system_clock;
|
|
|
|
|
|
|
|
template <typename Res, typename Clock>
|
|
|
|
static Duration_t
|
|
|
|
time_since_epoch(std::chrono::time_point<Clock> point)
|
|
|
|
{
|
|
|
|
return std::chrono::duration_cast<Res>(point.time_since_epoch());
|
|
|
|
}
|
2018-10-04 13:02:03 +00:00
|
|
|
|
2022-05-04 14:37:35 +00:00
|
|
|
const static auto started_at_system = Clock_t::now();
|
2021-04-02 15:10:37 +00:00
|
|
|
|
2022-05-04 14:37:35 +00:00
|
|
|
const static auto started_at_steady = std::chrono::steady_clock::now();
|
|
|
|
} // namespace
|
2021-04-02 15:10:37 +00:00
|
|
|
|
|
|
|
uint64_t
|
|
|
|
ToMS(Duration_t ms)
|
|
|
|
{
|
|
|
|
return ms.count();
|
|
|
|
}
|
2020-01-23 12:59:51 +00:00
|
|
|
|
2020-01-23 14:41:33 +00:00
|
|
|
/// get our uptime in ms
|
2021-04-02 15:10:37 +00:00
|
|
|
Duration_t
|
|
|
|
uptime()
|
2020-01-23 12:59:51 +00:00
|
|
|
{
|
2021-04-02 15:10:37 +00:00
|
|
|
return std::chrono::duration_cast<Duration_t>(
|
|
|
|
std::chrono::steady_clock::now() - started_at_steady);
|
2020-01-23 12:59:51 +00:00
|
|
|
}
|
|
|
|
|
2021-04-02 15:10:37 +00:00
|
|
|
Duration_t
|
2018-11-19 22:45:37 +00:00
|
|
|
time_now_ms()
|
|
|
|
{
|
2021-04-02 15:10:37 +00:00
|
|
|
auto t = uptime();
|
2020-01-23 12:59:51 +00:00
|
|
|
#ifdef TESTNET_SPEED
|
2021-04-02 15:10:37 +00:00
|
|
|
t /= uint64_t{TESTNET_SPEED};
|
2020-01-23 12:59:51 +00:00
|
|
|
#endif
|
2021-04-02 15:10:37 +00:00
|
|
|
return t + time_since_epoch<Duration_t, Clock_t>(started_at_system);
|
2018-11-19 22:45:37 +00:00
|
|
|
}
|
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-02-25 17:05:13 +00:00
|
|
|
{
|
2021-04-02 15:10:37 +00:00
|
|
|
return ToMS(t);
|
2020-02-25 17:05:13 +00:00
|
|
|
}
|
2018-10-04 13:02:03 +00:00
|
|
|
} // namespace llarp
|