From b7bb942cadfe3d657895af1557b78acc2559947e Mon Sep 17 00:00:00 2001 From: Kelly Rauchenberger Date: Sun, 5 Aug 2018 15:58:14 -0400 Subject: Tweets store their created time C/C++ time handling is awful, pass it on --- src/tweet.cpp | 43 +++++++++++++++++++++++++------------------ 1 file changed, 25 insertions(+), 18 deletions(-) (limited to 'src/tweet.cpp') diff --git a/src/tweet.cpp b/src/tweet.cpp index 1c869f9..1003647 100644 --- a/src/tweet.cpp +++ b/src/tweet.cpp @@ -1,11 +1,12 @@ #include "tweet.h" #include +#include #include "util.h" #include "codes.h" #include "client.h" namespace twitter { - + tweet::tweet(std::string data) try : _valid(true) { @@ -13,14 +14,20 @@ namespace twitter { _id = json["id"].get(); _text = json["text"].get(); _author = std::make_unique(json["user"].dump()); - + + std::tm ctt = { 0 }; + std::stringstream createdAtStream; + createdAtStream << json["created_at"].get(); + createdAtStream >> std::get_time(&ctt, "%a %b %d %H:%M:%S +0000 %Y"); + _created_at = twitter::timegm(&ctt); + if (!json["retweeted_status"].is_null()) { _is_retweet = true; - + _retweeted_status = std::make_unique(json["retweeted_status"].dump()); } - + if (!json["entities"].is_null()) { auto entities = json["entities"]; @@ -39,7 +46,7 @@ namespace twitter { { std::throw_with_nested(malformed_object("tweet", data)); } - + tweet::tweet(const tweet& other) { _valid = other._valid; @@ -47,27 +54,27 @@ namespace twitter { _text = other._text; _author = std::make_unique(*other._author); _is_retweet = other._is_retweet; - + if (_is_retweet) { _retweeted_status = std::make_unique(*other._retweeted_status); } - + _mentions = other._mentions; } - + tweet::tweet(tweet&& other) : tweet() { swap(*this, other); } - + tweet& tweet::operator=(tweet other) { swap(*this, other); - + return *this; } - + void swap(tweet& first, tweet& second) { std::swap(first._valid, second._valid); @@ -78,12 +85,12 @@ namespace twitter { std::swap(first._retweeted_status, second._retweeted_status); std::swap(first._mentions, second._mentions); } - + std::string tweet::generateReplyPrefill(const user& me) const { std::ostringstream output; output << "@" << _author->getScreenName() << " "; - + for (auto mention : _mentions) { if ((mention.first != _author->getID()) && (mention.first != me.getID())) @@ -91,21 +98,21 @@ namespace twitter { output << "@" << mention.second << " "; } } - + return output.str(); } - + std::string tweet::getURL() const { assert(_valid); - + std::ostringstream urlstr; urlstr << "https://twitter.com/"; urlstr << _author->getScreenName(); urlstr << "/statuses/"; urlstr << _id; - + return urlstr.str(); } - + }; -- cgit 1.4.1