Added Easy::strtime_utc() and Easy::strtime_local()
This commit is contained in:
parent
da032b871b
commit
05fa4905e0
|
@ -1,6 +1,6 @@
|
|||
cmake_minimum_required (VERSION 3.7)
|
||||
project (mastodon-cpp
|
||||
VERSION 0.10.1
|
||||
VERSION 0.11.0
|
||||
LANGUAGES CXX
|
||||
)
|
||||
|
||||
|
|
|
@ -11,7 +11,6 @@
|
|||
#include <memory>
|
||||
#include <vector>
|
||||
#include <chrono>
|
||||
#include <ctime>
|
||||
|
||||
// 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;
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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.
|
||||
*/
|
||||
|
|
Reference in New Issue