From de78019cb3177cc316cdd10cba100945c18fa6e2 Mon Sep 17 00:00:00 2001 From: tastytea Date: Tue, 30 Apr 2019 23:29:05 +0200 Subject: [PATCH] Added Easy::string_to_time(). --- src/easy/easy.cpp | 10 ++++++++++ src/easy/easy.hpp | 7 +++++++ src/easy/entity.cpp | 34 ++-------------------------------- 3 files changed, 19 insertions(+), 32 deletions(-) diff --git a/src/easy/easy.cpp b/src/easy/easy.cpp index 8368eed..a519aa6 100644 --- a/src/easy/easy.cpp +++ b/src/easy/easy.cpp @@ -79,6 +79,16 @@ const vector Easy::parse_stream( return vec; } +const Easy::time string_to_time(const string &strtime) +{ + std::stringstream sstime(strtime); + struct std::tm tm = {}; + tm.tm_isdst = -1; // Detect daylight saving time. + sstime >> std::get_time(&tm, "%Y-%m-%dT%T"); + std::time_t time = timegm(&tm); // Assume time is UTC. + return { system_clock::from_time_t(time) }; +} + const Easy::Link Easy::API::get_link() const { return Link(get_header("Link")); diff --git a/src/easy/easy.hpp b/src/easy/easy.hpp index 24c3ffd..de84abb 100644 --- a/src/easy/easy.hpp +++ b/src/easy/easy.hpp @@ -125,6 +125,13 @@ namespace Easy */ const std::vector parse_stream(const std::string &streamdata); + /*! + * @brief Convert ISO 8601 time string to Easy::time. + * + * @param strtime Time string as returned by Mastodon. + */ + const Easy::time string_to_time(const string &strtime); + /*! * @brief Child of Mastodon::API with abstract methods. * diff --git a/src/easy/entity.cpp b/src/easy/entity.cpp index 6072044..aa6fa83 100644 --- a/src/easy/entity.cpp +++ b/src/easy/entity.cpp @@ -21,6 +21,7 @@ #include #include #include "easy/entity.hpp" +#include "easy/easy.hpp" #include "debug.hpp" using namespace Mastodon; @@ -232,13 +233,8 @@ const Easy::time Easy::Entity::get_time(const string &key) const if (node.isString()) { - std::stringstream sstime(node.asString()); - struct std::tm tm = {}; - tm.tm_isdst = -1; // Detect daylight saving time. - sstime >> std::get_time(&tm, "%Y-%m-%dT%T"); - std::time_t time = timegm(&tm); // Assume time is UTC. _was_set = true; - return { system_clock::from_time_t(time) }; + return Easy::string_to_time(node.asString()); } _was_set = false; @@ -313,29 +309,3 @@ std::uint64_t Easy::Entity::stouint64(const string &str) const return stoull(str); } } - -// Easy::GenericEntity::GenericEntity(const string &json) -// : Entity(json) -// {} - -// Easy::GenericEntity::GenericEntity() -// : Entity() -// {} - -// bool Easy::GenericEntity::valid() const -// { -// return true; -// } - -// template GenericEntity::GenericEntity(const string &json) -// : Entity(json) -// {} - -// template GenericEntity::GenericEntity() -// : Entity() -// {} - -// template bool GenericEntity::valid() const -// { -// return true; -// }