2020-08-10 11:22:25 +01:00
|
|
|
/* Copyright 2020, Michele Santullo
|
|
|
|
* This file is part of orotool.
|
|
|
|
*
|
|
|
|
* Orotool is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* Orotool is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with Orotool. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2020-06-20 02:01:52 +02:00
|
|
|
#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
|