Files
biboumi/louloulibs/utils/time.cpp
T
louiz’ aaa2ca670a Fix the parse_datetime by using %Z instead of %z
If anybody knows why fedora accepts both, but it only works with %z on
debian, please tell me.
2016-09-17 00:34:28 +02:00

25 lines
540 B
C++

#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)
{
struct tm tm;
if (!::strptime(stamp.data(), "%FT%T%Z", &tm))
return -1;
auto res = ::timegm(&tm);
return res;
}
}