2019-05-14 22:13:58 +02:00
|
|
|
/* This file is part of remwharead.
|
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
2019-10-25 06:01:14 +02:00
|
|
|
#include "time.hpp"
|
2019-09-30 13:20:36 +02:00
|
|
|
#include "sqlite.hpp"
|
|
|
|
#include <array>
|
|
|
|
#include <cstdint>
|
2019-05-14 22:13:58 +02:00
|
|
|
#include <ctime>
|
|
|
|
#include <iomanip>
|
|
|
|
#include <sstream>
|
|
|
|
|
2019-07-27 09:59:43 +02:00
|
|
|
namespace remwharead
|
2019-05-14 22:13:58 +02:00
|
|
|
{
|
2019-09-30 13:20:36 +02:00
|
|
|
using std::array;
|
2019-09-23 22:53:11 +02:00
|
|
|
|
2019-09-30 13:20:36 +02:00
|
|
|
time_point string_to_timepoint(const string &strtime, bool sqlite)
|
|
|
|
{
|
|
|
|
std::stringstream sstime(strtime);
|
|
|
|
struct std::tm tm = {};
|
2019-11-27 02:11:30 +01:00
|
|
|
tm.tm_isdst = -1; // Don't convert to/from daylight saving time.
|
2019-09-30 13:20:36 +02:00
|
|
|
if (sqlite)
|
2019-05-16 00:03:57 +02:00
|
|
|
{
|
2019-09-30 13:20:36 +02:00
|
|
|
sstime >> std::get_time(&tm, "%Y-%m-%d %T");
|
2019-05-16 00:03:57 +02:00
|
|
|
}
|
2019-09-30 13:20:36 +02:00
|
|
|
else
|
2019-05-16 00:03:57 +02:00
|
|
|
{
|
2019-09-30 13:20:36 +02:00
|
|
|
sstime >> std::get_time(&tm, "%Y-%m-%dT%T");
|
|
|
|
}
|
2019-11-27 02:11:30 +01:00
|
|
|
std::time_t time = mktime(&tm);
|
2019-09-30 13:20:36 +02:00
|
|
|
return system_clock::from_time_t(time);
|
|
|
|
}
|
2019-05-14 22:13:58 +02:00
|
|
|
|
2019-09-30 13:20:36 +02:00
|
|
|
string timepoint_to_string(const time_point &tp, bool sqlite)
|
|
|
|
{
|
|
|
|
constexpr std::uint16_t bufsize = 32;
|
|
|
|
std::time_t time = system_clock::to_time_t(tp);
|
|
|
|
std::tm *tm;
|
|
|
|
tm = std::localtime(&time);
|
2019-09-23 22:53:11 +02:00
|
|
|
|
2019-09-30 13:20:36 +02:00
|
|
|
array<char, bufsize> buffer = {};
|
2019-05-14 22:13:58 +02:00
|
|
|
|
2019-09-30 13:20:36 +02:00
|
|
|
if (sqlite)
|
|
|
|
{
|
|
|
|
std::strftime(buffer.begin(), bufsize, "%F %T", tm);
|
2019-05-16 00:03:57 +02:00
|
|
|
}
|
2019-09-30 13:20:36 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
std::strftime(buffer.begin(), bufsize, "%FT%T", tm);
|
|
|
|
}
|
|
|
|
|
|
|
|
return buffer.begin();
|
|
|
|
}
|
2019-09-23 22:53:11 +02:00
|
|
|
} // namespace remwharead
|