orotool/src/oro/dateconv.cpp

42 lines
1 KiB
C++
Raw Normal View History

#include "dateconv.hpp"
#define HAS_UNCAUGHT_EXCEPTIONS 1
#include "date/date.h"
#include <sstream>
#include <clocale>
#include <memory>
namespace oro {
namespace {
struct LocaleDeleter {
void operator() (char* old_locale) {
std::setlocale(LC_TIME, old_locale);
}
};
Timestamp to_timestamp (const char* fmt, const std::string& str) {
std::istringstream iss(str);
Timestamp ts;
date::from_stream(iss, fmt, ts.ts);
return ts;
}
typedef std::unique_ptr<char, LocaleDeleter> UniqueLocale;
} //unnamed namespace
//For formatting modifiers see:
//https://howardhinnant.github.io/date/date.html#from_stream_formatting
Timestamp from_json_timestamp (const std::string& str) {
//date has this format: 2020-06-19T22:33:36.855672+00:00
return to_timestamp("%FT%T%Ez", str);
}
Timestamp from_header_timestamp (const std::string& str) {
UniqueLocale loc(std::setlocale(LC_TIME, "POSIX"));
//date has this format: Fri, 19 Jun 2020 22:33:43 GMT
return to_timestamp("%a, %d %b %Y %T %Z", str);
}
} //namespace oro