Added Easy::string_to_time().

This commit is contained in:
tastytea 2019-04-30 23:29:05 +02:00
parent 6543ea4b51
commit de78019cb3
Signed by: tastytea
GPG Key ID: CFC39497F1B26E07
3 changed files with 19 additions and 32 deletions

View File

@ -79,6 +79,16 @@ const vector<Easy::stream_event> 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"));

View File

@ -125,6 +125,13 @@ namespace Easy
*/
const std::vector<stream_event> 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.
*

View File

@ -21,6 +21,7 @@
#include <regex>
#include <algorithm>
#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<typename T> GenericEntity<T>::GenericEntity(const string &json)
// : Entity(json)
// {}
// template<typename T> GenericEntity<T>::GenericEntity()
// : Entity()
// {}
// template<typename T> bool GenericEntity<T>::valid() const
// {
// return true;
// }