summaryrefslogtreecommitdiff
path: root/louloulibs/utils/time.cpp
blob: abf0a84dbf56452ddf347e398c4e5feb9581b4e8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <utils/time.hpp>
#include <time.h>

namespace utils
{
std::string to_string(const std::time_t& timestamp)
{
  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};
}

std::time_t parse_datetime(const std::string& stamp)
{
  auto stamp2 = stamp.substr(0, stamp.size() - 1) + "z";
  struct tm tm;
  if (!::strptime(stamp2.data(), "%FT%T%Z", &tm))
    return -1;
  auto res = ::timegm(&tm);
  return res;
}

}