#include "util/id_util.hpp"

#include <chrono>
#include <ctime>
#include <random>

namespace lm {

std::string IdUtil::newId()
{
  static thread_local std::mt19937_64 rng{std::random_device{}()};
  static constexpr char kHex[] = "0123456789abcdef";
  std::string out;
  out.resize(16);
  for (int i = 0; i < 16; i++)
    out[i] = kHex[static_cast<size_t>(rng() & 0xF)];
  return out;
}

std::string IdUtil::nowIso8601()
{
  using clock = std::chrono::system_clock;
  const std::time_t t = clock::to_time_t(clock::now());
  std::tm tm{};
#if defined(_WIN32)
  gmtime_s(&tm, &t);
#else
  gmtime_r(&t, &tm);
#endif
  char buf[32];
  std::strftime(buf, sizeof(buf), "%Y-%m-%dT%H:%M:%SZ", &tm);
  return buf;
}

}  // namespace lm
