This repository has been archived on 2020-05-10. You can view files and clone it, but cannot push or open issues or pull requests.
mastodon-cpp/src/easy/types_easy.cpp

58 lines
1.6 KiB
C++

/* This file is part of mastodon-cpp.
* Copyright © 2019 tastytea <tastytea@tastytea.de>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 3.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "types_easy.hpp"
using namespace Mastodon;
Easy::time::operator const system_clock::time_point() const
{
return timepoint;
}
Easy::time::operator const string() const
{
return strtime("%FT%T%z", true);
}
const string Easy::time::strtime(const string &format, const bool &local) const
{
constexpr std::uint16_t bufsize = 1024;
std::time_t time = system_clock::to_time_t(timepoint);
std::tm *tm;
if (local)
{
tm = std::localtime(&time);
}
else
{
tm = std::gmtime(&time);
}
char buffer[bufsize];
std::strftime(buffer, bufsize, format.c_str(), tm);
return static_cast<const string>(buffer);
}
std::ostream &Mastodon::Easy::operator <<(std::ostream &out,
const Easy::time &t)
{
const string s = t; // Converts using operator const string().
out << s;
return out;
}