Fix datetime objects

main
Simon Quigley 6 days ago
parent a2a993d713
commit 7879ec07be

@ -8,6 +8,7 @@ find_package(nlohmann_json REQUIRED)
find_package(OpenSSL REQUIRED) find_package(OpenSSL REQUIRED)
find_package(CURL REQUIRED) find_package(CURL REQUIRED)
find_package(PkgConfig REQUIRED) find_package(PkgConfig REQUIRED)
find_package(date REQUIRED)
pkg_check_modules(LIBSECRET REQUIRED libsecret-1) pkg_check_modules(LIBSECRET REQUIRED libsecret-1)
add_library(launchpad SHARED add_library(launchpad SHARED
@ -78,5 +79,6 @@ target_link_libraries(launchpad PRIVATE
nlohmann_json::nlohmann_json nlohmann_json::nlohmann_json
OpenSSL::Crypto OpenSSL::Crypto
CURL::libcurl CURL::libcurl
date::date-tz
${LIBSECRET_LIBRARIES} ${LIBSECRET_LIBRARIES}
) )

@ -17,6 +17,7 @@
#include "source_package_publishing_history.h" #include "source_package_publishing_history.h"
#include "utils.h" #include "utils.h"
#include "launchpad.h" #include "launchpad.h"
#include <date/date.h>
#include <iostream> #include <iostream>
#include <nlohmann/json.hpp> #include <nlohmann/json.hpp>
#include <regex> #include <regex>
@ -66,48 +67,48 @@ void build::parse_json(const std::string& json_data) {
{"external_dependencies", [this](const nlohmann::json& val) { external_dependencies = val.get<std::string>(); }}, {"external_dependencies", [this](const nlohmann::json& val) { external_dependencies = val.get<std::string>(); }},
{"pocket", [this](const nlohmann::json& val) { pocket = val.get<std::string>(); }}, {"pocket", [this](const nlohmann::json& val) { pocket = val.get<std::string>(); }},
{"date_first_dispatched", [this](const nlohmann::json& val) { {"date_first_dispatched", [this](const nlohmann::json& val) {
std::string datetimeStr = val.get<std::string>(); std::chrono::sys_time<std::chrono::microseconds> parsed_time;
std::istringstream ss(datetimeStr); std::istringstream ss(val.get<std::string>());
std::tm tm = {}; ss >> date::parse("%FT%T%z", parsed_time);
ss >> std::get_time(&tm, "%Y-%m-%d %H:%M:%S.%f%z");
if (ss.fail()) { if (ss.fail()) {
std::cout << val << std::endl; throw std::runtime_error("Failed to parse date_first_dispatched: " + val.get<std::string>());
throw std::runtime_error("Failed to parse date_first_dispatched");
} }
date_first_dispatched = std::chrono::system_clock::from_time_t(std::mktime(&tm));
date_first_dispatched = std::chrono::time_point_cast<std::chrono::system_clock::duration>(parsed_time);
}}, }},
{"date_started", [this](const nlohmann::json& val) { {"date_started", [this](const nlohmann::json& val) {
std::string datetimeStr = val.get<std::string>(); std::chrono::sys_time<std::chrono::microseconds> parsed_time;
std::istringstream ss(datetimeStr); std::istringstream ss(val.get<std::string>());
std::tm tm = {}; ss >> date::parse("%FT%T%z", parsed_time);
ss >> std::get_time(&tm, "%Y-%m-%d %H:%M:%S.%f%z");
if (ss.fail()) { if (ss.fail()) {
std::cout << val << std::endl; throw std::runtime_error("Failed to parse date_started: " + val.get<std::string>());
throw std::runtime_error("Failed to parse date_started");
} }
date_started = std::chrono::system_clock::from_time_t(std::mktime(&tm));
date_started = std::chrono::time_point_cast<std::chrono::system_clock::duration>(parsed_time);
}}, }},
{"datebuilt", [this](const nlohmann::json& val) { {"datebuilt", [this](const nlohmann::json& val) {
std::string datetimeStr = val.get<std::string>(); std::chrono::sys_time<std::chrono::microseconds> parsed_time;
std::istringstream ss(datetimeStr); std::istringstream ss(val.get<std::string>());
std::tm tm = {}; ss >> date::parse("%FT%T%z", parsed_time);
ss >> std::get_time(&tm, "%Y-%m-%d %H:%M:%S.%f%z");
if (ss.fail()) { if (ss.fail()) {
std::cout << val << std::endl; throw std::runtime_error("Failed to parse datebuilt: " + val.get<std::string>());
throw std::runtime_error("Failed to parse datebuilt");
} }
datebuilt = std::chrono::system_clock::from_time_t(std::mktime(&tm));
datebuilt = std::chrono::time_point_cast<std::chrono::system_clock::duration>(parsed_time);
}}, }},
{"datecreated", [this](const nlohmann::json& val) { {"datecreated", [this](const nlohmann::json& val) {
std::string datetimeStr = val.get<std::string>(); std::chrono::sys_time<std::chrono::microseconds> parsed_time;
std::istringstream ss(datetimeStr); std::istringstream ss(val.get<std::string>());
std::tm tm = {}; ss >> date::parse("%FT%T%z", parsed_time);
ss >> std::get_time(&tm, "%Y-%m-%d %H:%M:%S.%f%z");
if (ss.fail()) { if (ss.fail()) {
std::cout << val << std::endl; throw std::runtime_error("Failed to parse datecreated: " + val.get<std::string>());
throw std::runtime_error("Failed to parse datecreated");
} }
datecreated = std::chrono::system_clock::from_time_t(std::mktime(&tm));
datecreated = std::chrono::time_point_cast<std::chrono::system_clock::duration>(parsed_time);
}} }}
}; };

Loading…
Cancel
Save