---
title: llarp/util/time.hpp

---

# llarp/util/time.hpp



## Namespaces

| Name           |
| -------------- |
| **[llarp](Namespaces/namespacellarp.md)** <br>[crypto.hpp] |
| **[fmt](Namespaces/namespacefmt.md)**  |
| **[std::chrono_literals](Namespaces/namespacestd_1_1chrono__literals.md)**  |

## Classes

|                | Name           |
| -------------- | -------------- |
| struct | **[llarp::time_delta](Classes/structllarp_1_1time__delta.md)**  |
| struct | **[fmt::formatter< llarp::time_delta< Time_Duration > >](Classes/structfmt_1_1formatter_3_01llarp_1_1time__delta_3_01Time__Duration_01_4_01_4.md)**  |
| struct | **[fmt::formatter< llarp::Duration_t >](Classes/structfmt_1_1formatter_3_01llarp_1_1Duration__t_01_4.md)**  |
| struct | **[fmt::formatter< llarp::TimePoint_t >](Classes/structfmt_1_1formatter_3_01llarp_1_1TimePoint__t_01_4.md)**  |




## Source code

```cpp
#pragma once

#include "types.hpp"
#include <nlohmann/json.hpp>
#include <iostream>
#include <fmt/format.h>
#include <fmt/chrono.h>

using namespace std::chrono_literals;

namespace llarp
{
  Duration_t
  time_now_ms();

  Duration_t
  uptime();

  uint64_t
  ToMS(Duration_t duration);

  nlohmann::json
  to_json(const Duration_t& t);

  template <typename Time_Duration>
  struct time_delta
  {
    const TimePoint_t at;
  };
}  // namespace llarp

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)
    {
      const auto dlt =
          std::chrono::duration_cast<llarp::Duration_t>(llarp::TimePoint_t::clock::now() - td.at);
      using Parent = formatter<std::string>;
      if (dlt > 0s)
        return Parent::format(fmt::format("{} ago", dlt), ctx);
      if (dlt < 0s)
        return Parent::format(fmt::format("in {}", -dlt), ctx);
      return Parent::format("now", ctx);
    }
  };

  template <>
  struct formatter<llarp::Duration_t> : formatter<std::string>
  {
    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);
    }
  };

  template <>
  struct formatter<llarp::TimePoint_t> : formatter<std::string>
  {
    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
```


-------------------------------

Updated on 2022-09-01 at 18:16:54 +0000
