#include "torlinkc/util/date_parse.hpp"
#include <cstring>
#include <ctime>
#include <curl/curl.h>
namespace torlinkc {
std::optional<std::int64_t> parseDateToUnixSeconds(const std::string& s) {
if (s.empty()) return std::nullopt;
// ISO 8601 first: curl_getdate() doesn't understand "2024-01-15T00:00:00Z".
// Fractional seconds and a timezone suffix (if any) are ignored and the
// result treated as UTC -- matching JS's Date.parse for the 'Z'-suffixed
// ISO dates these APIs actually send; none of them emit a non-UTC offset.
{
std::tm tm{};
if (strptime(s.c_str(), "%Y-%m-%dT%H:%M:%S", &tm) != nullptr) {
const time_t t = timegm(&tm);
if (t != static_cast<time_t>(-1)) return static_cast<std::int64_t>(t);
}
}
const time_t t = curl_getdate(s.c_str(), nullptr);
if (t != static_cast<time_t>(-1)) return static_cast<std::int64_t>(t);
return std::nullopt;
}
} // namespace torlinkc