From 05fa4905e002fab844a73fe3a8dc9bf9f005f33d Mon Sep 17 00:00:00 2001 From: tastytea Date: Sat, 5 May 2018 04:25:22 +0200 Subject: [PATCH] Added Easy::strtime_utc() and Easy::strtime_local() --- CMakeLists.txt | 2 +- examples/example13_easy_stream.cpp | 16 ++-------------- examples/example14_easy_treeview.cpp | 23 ++++++++--------------- src/easy/easy.cpp | 26 ++++++++++++++++++++++++++ src/easy/easy.hpp | 27 +++++++++++++++++++++++++++ 5 files changed, 64 insertions(+), 30 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 9005738..ee47383 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,6 @@ cmake_minimum_required (VERSION 3.7) project (mastodon-cpp - VERSION 0.10.1 + VERSION 0.11.0 LANGUAGES CXX ) diff --git a/examples/example13_easy_stream.cpp b/examples/example13_easy_stream.cpp index 826bf48..1483fda 100644 --- a/examples/example13_easy_stream.cpp +++ b/examples/example13_easy_stream.cpp @@ -11,7 +11,6 @@ #include #include #include -#include // If we are compiling mastodon-cpp, use another include path #ifdef MASTODON_CPP @@ -28,18 +27,6 @@ using Mastodon::Easy; using std::cout; using std::chrono::system_clock; -// Transform time_point into a string with the local time -std::string get_localtime(const system_clock::time_point &timepoint) -{ - std::time_t time = system_clock::to_time_t(timepoint); - std::tm *timeinfo = std::localtime(&time); - char buffer[9]; - - std::strftime(buffer, 9, "%T", timeinfo); - - return buffer; -} - int main(int argc, char *argv[]) { if (argc < 3) @@ -80,7 +67,8 @@ int main(int argc, char *argv[]) { case Easy::event_type::Update: status.from_string(event.second); - cout << "[" << get_localtime(status.created_at()) << "] "; + cout << "[" << + Easy::strtime_local(status.created_at(), "%T") << "] "; cout << "Status from: " << status.account().acct() << " (" << status.url() << ")\n"; break; diff --git a/examples/example14_easy_treeview.cpp b/examples/example14_easy_treeview.cpp index 0c4b624..f130a1e 100644 --- a/examples/example14_easy_treeview.cpp +++ b/examples/example14_easy_treeview.cpp @@ -31,18 +31,6 @@ using Mastodon::Easy; using std::cout; using std::chrono::system_clock; -// Transform time_point into a string with the local time -std::string get_localtime(const system_clock::time_point &timepoint) -{ - std::time_t time = system_clock::to_time_t(timepoint); - std::tm *timeinfo = std::localtime(&time); - char buffer[9]; - - std::strftime(buffer, 9, "%T", timeinfo); - - return buffer; -} - // Print a status to stdout, nicely formatted void format_status(const Easy::Status &status, const std::uint8_t &level) { @@ -67,7 +55,7 @@ void format_status(const Easy::Status &status, const std::uint8_t &level) } } cout << space << "| " - << get_localtime(status.created_at()) << '\n'; + << Easy::strtime_local(status.created_at(), "%T") << '\n'; cout << space << "+-----------------------------------------" << std::endl; } @@ -77,14 +65,19 @@ std::uint16_t print_status(Easy &masto, const std::string &id, { std::uint16_t ret; std::string answer; + API::parametermap parameters = + { + { "id", { id }} + }; - ret = masto.get(Mastodon::API::v1::statuses_id, id, answer); + ret = masto.get(Mastodon::API::v1::statuses_id, parameters, answer); if (ret == 0) { format_status(Easy::Status(answer), level); - ret = masto.get(Mastodon::API::v1::statuses_id_context, id, answer); + ret = masto.get(Mastodon::API::v1::statuses_id_context, + parameters, answer); if (ret == 0) { Easy::Context context(answer); diff --git a/src/easy/easy.cpp b/src/easy/easy.cpp index ff048f1..e301544 100644 --- a/src/easy/easy.cpp +++ b/src/easy/easy.cpp @@ -82,6 +82,32 @@ const Easy::Link Easy::get_link() const return Link(get_header("Link")); } +const string Easy::strtime_utc(const system_clock::time_point &timepoint, + const string &format) +{ + constexpr std::uint_fast16_t bufsize = 1024; + std::time_t time = system_clock::to_time_t(timepoint); + std::tm *timeinfo = std::gmtime(&time); + char buffer[bufsize]; + + std::strftime(buffer, bufsize, format.c_str(), timeinfo); + + return buffer; +} + +const string Easy::strtime_local(const system_clock::time_point &timepoint, + const string &format) +{ + constexpr std::uint_fast16_t bufsize = 1024; + std::time_t time = system_clock::to_time_t(timepoint); + std::tm *timeinfo = std::localtime(&time); + char buffer[bufsize]; + + std::strftime(buffer, bufsize, format.c_str(), timeinfo); + + return buffer; +} + Easy::Link::Link(const string &link_header) : _next(0) , _prev(0) diff --git a/src/easy/easy.hpp b/src/easy/easy.hpp index 7018c6c..0131ffa 100644 --- a/src/easy/easy.hpp +++ b/src/easy/easy.hpp @@ -181,6 +181,33 @@ public: */ const Link get_link() const; + /*! + * @brief Converts a time_point to a string + * + * The return value can not exceed 1023 chars. + * + * @param timepoint The timepoint + * @param format The format of the string, same as with `strftime`. + * + * Example: + * @code + * auto timepoint = status.created_at(); + * cout << Easy::strtime_utc(timepoint, "%F, %T") << '\n'; + * @endcode + * + * @return The UTC time as string + */ + static const string strtime_utc(const system_clock::time_point &timepoint, + const string &format); + + /*! + * @brief See strtime_utc + * + * @return The local time as string + */ + static const string strtime_local(const system_clock::time_point &timepoint, + const string &format); + /*! * @brief Base class for all entities. */