ranczo-io/libs/date_utils.cpp
2025-08-14 10:59:01 +02:00

27 lines
999 B
C++

#include <ranczo-io/utils/date_utils.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
namespace ranczo::date {
std::optional< std::chrono::system_clock::time_point > parse_timestamp_utc(std::string_view sv) noexcept {
try {
// Strip trailing 'Z' if present; Boost expects no timezone marker
std::string s(sv);
if(!s.empty() && (s.back() == 'Z' || s.back() == 'z'))
s.pop_back();
// Parse as extended ISO string
using namespace boost::posix_time;
ptime t = from_iso_extended_string(s); // may throw on invalid
static const ptime epoch{boost::gregorian::date{1970, 1, 1}};
time_duration d = t - epoch;
// Keep sub-second precision if present (microseconds is enough for the given example)
auto us = d.total_microseconds();
return std::chrono::system_clock::time_point{std::chrono::microseconds{us}};
} catch(...) {
return std::nullopt;
}
}
} // namespace ranczo::date