time.cpp 540 Bytes
Newer Older
1
#include <utils/time.hpp>
louiz’'s avatar
louiz’ committed
2
#include <time.h>
3 4 5 6 7

namespace utils
{
std::string to_string(const std::time_t& timestamp)
{
8 9 10 11 12
  constexpr std::size_t stamp_size = 21;
  char date_buf[stamp_size];
  if (std::strftime(date_buf, stamp_size, "%FT%TZ", std::gmtime(&timestamp)) != stamp_size - 1)
    return "";
  return {std::begin(date_buf), std::end(date_buf) - 1};
13
}
louiz’'s avatar
louiz’ committed
14 15 16 17

std::time_t parse_datetime(const std::string& stamp)
{
  struct tm tm;
18
  if (!::strptime(stamp.data(), "%FT%T%Z", &tm))
louiz’'s avatar
louiz’ committed
19 20 21 22 23
    return -1;
  auto res = ::timegm(&tm);
  return res;
}

24
}