Parse the date from the responce header

This commit is contained in:
King_DuckZ 2020-06-20 02:01:52 +02:00
parent 96e35a5814
commit bab47fbd0b
7 changed files with 69 additions and 5 deletions

View file

@ -5,11 +5,14 @@ int main() {
oro::Api oro_api("https://api.originsro.org", "RESTC_CPP", "Testing");
auto ping = oro_api.ping();
std::cout << "date: " << ping.first.date << '\n';
std::cout << "rate limit: " << ping.first.rate_limit << '\n';
std::cout << "remaining: " << ping.first.rate_limit_remaining << '\n';
std::cout << "reset: " << ping.first.rate_limit_reset << '\n';
std::cout << "retry after: " << ping.first.retry_after << '\n';
std::cout << "server: " << ping.first.server << '\n';
std::cout << "-----\n";
std::cout << "timestamp: " << ping.second.generation_timestamp << '\n';
std::cout << "answer: " << ping.second.message << '\n';
std::cout << "version: " << ping.second.version << '\n';
return 0;

View file

@ -9,6 +9,7 @@ executable(meson.project_name(),
'main.cpp',
'oro/datatypes.cpp',
'oro/api.cpp',
'oro/dateconv.cpp',
install: true,
dependencies: [restc_cpp_dep],
include_directories: date_incdir,

View file

@ -1,5 +1,6 @@
#include "api.hpp"
#include "datatypes.hpp"
#include "dateconv.hpp"
#include <restc-cpp/restc-cpp.h>
#include <restc-cpp/RequestBuilder.h>
#include <boost/fusion/adapted.hpp>
@ -32,6 +33,9 @@ namespace {
.Execute();
Header h;
if (auto val = reply->GetHeader("Date")) {
h.date = from_header_timestamp(*val);
}
if (auto val = reply->GetHeader("X-RateLimit-Limit")) {
h.rate_limit = std::stoi(*val);
}

View file

@ -1,15 +1,18 @@
#include "datatypes.hpp"
#include "dateconv.hpp"
#define HAS_UNCAUGHT_EXCEPTIONS 1
#include "date/date.h"
#include <sstream>
namespace oro {
Timestamp& Timestamp::operator= (const std::string& str) {
std::istringstream iss(str);
//date has this format: 2020-06-19T22:33:36.855672+00:00
date::from_stream(iss, "%FT%T%Ez", ts);
ts = from_json_timestamp(str);
return *this;
}
std::ostream& operator<<(std::ostream& os, const Timestamp& ts) {
using date::operator<<;
os << ts.ts;
return os;
}
} //namespace oro

View file

@ -2,6 +2,7 @@
#include <chrono>
#include <string>
#include <iostream>
namespace oro {
typedef std::chrono::time_point<std::chrono::system_clock, std::chrono::microseconds> timestamp_t;
@ -12,4 +13,6 @@ namespace oro {
timestamp_t ts;
};
std::ostream& operator<< (std::ostream& os, const Timestamp& ts);
} //namespace oro

41
src/oro/dateconv.cpp Normal file
View file

@ -0,0 +1,41 @@
#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

9
src/oro/dateconv.hpp Normal file
View file

@ -0,0 +1,9 @@
#pragma once
#include "datatypes.hpp"
#include <string>
namespace oro {
Timestamp from_json_timestamp (const std::string& str);
Timestamp from_header_timestamp (const std::string& str);
} //namespace oro