43 lines
1.0 KiB
C++
43 lines
1.0 KiB
C++
#include "rdp_worker/common/time.hpp"
|
|
|
|
#include <ctime>
|
|
#include <iomanip>
|
|
#include <sstream>
|
|
#include <stdexcept>
|
|
|
|
namespace rdp_worker::common {
|
|
|
|
Clock::time_point NowUtc() {
|
|
return Clock::now();
|
|
}
|
|
|
|
std::string ToRfc3339(Clock::time_point time_point) {
|
|
const std::time_t raw = Clock::to_time_t(time_point);
|
|
std::tm utc_tm{};
|
|
#if defined(_WIN32)
|
|
gmtime_s(&utc_tm, &raw);
|
|
#else
|
|
gmtime_r(&raw, &utc_tm);
|
|
#endif
|
|
std::ostringstream output;
|
|
output << std::put_time(&utc_tm, "%Y-%m-%dT%H:%M:%SZ");
|
|
return output.str();
|
|
}
|
|
|
|
Clock::time_point ParseRfc3339(const std::string& value) {
|
|
std::tm utc_tm{};
|
|
std::istringstream input(value);
|
|
input >> std::get_time(&utc_tm, "%Y-%m-%dT%H:%M:%SZ");
|
|
if (input.fail()) {
|
|
throw std::runtime_error("failed to parse RFC3339 timestamp: " + value);
|
|
}
|
|
#if defined(_WIN32)
|
|
const std::time_t raw = _mkgmtime(&utc_tm);
|
|
#else
|
|
const std::time_t raw = timegm(&utc_tm);
|
|
#endif
|
|
return Clock::from_time_t(raw);
|
|
}
|
|
|
|
} // namespace rdp_worker::common
|