Moved time functions into own files.
This commit is contained in:
parent
76548146c0
commit
8ec8a1a558
|
@ -16,9 +16,6 @@
|
|||
|
||||
#include <iostream>
|
||||
#include <exception>
|
||||
#include <ctime>
|
||||
#include <iomanip>
|
||||
#include <sstream>
|
||||
#include <popl.hpp>
|
||||
#include "version.hpp"
|
||||
#include "parse_options.hpp"
|
||||
|
@ -34,16 +31,6 @@ options::options(const uint8_t &status)
|
|||
: status_code(status)
|
||||
{}
|
||||
|
||||
const system_clock::time_point string_to_timepoint(const string &strtime)
|
||||
{
|
||||
std::stringstream sstime(strtime);
|
||||
struct std::tm tm = {};
|
||||
tm.tm_isdst = -1; // Detect daylight saving time.
|
||||
sstime >> std::get_time(&tm, "%Y-%m-%dT%T");
|
||||
std::time_t time = timelocal(&tm); // Assume time is local.
|
||||
return system_clock::from_time_t(time);
|
||||
}
|
||||
|
||||
const options parse_options(const int argc, const char *argv[])
|
||||
{
|
||||
string tags;
|
||||
|
|
|
@ -22,6 +22,7 @@
|
|||
#include <array>
|
||||
#include <chrono>
|
||||
#include <cstdint>
|
||||
#include "time.hpp"
|
||||
|
||||
using std::string;
|
||||
using std::vector;
|
||||
|
@ -49,9 +50,6 @@ typedef struct options
|
|||
explicit options(const uint8_t &status);
|
||||
} options;
|
||||
|
||||
// Convert ISO 8601 time-string to time_point.
|
||||
const system_clock::time_point string_to_timepoint(const string &strtime);
|
||||
|
||||
// Parse command-line options.
|
||||
const options parse_options(const int argc, const char *argv[]);
|
||||
|
||||
|
|
|
@ -0,0 +1,44 @@
|
|||
/* 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/>.
|
||||
*/
|
||||
|
||||
#include <ctime>
|
||||
#include <iomanip>
|
||||
#include <sstream>
|
||||
#include <cstdint>
|
||||
#include "time.hpp"
|
||||
|
||||
const system_clock::time_point string_to_timepoint(const string &strtime)
|
||||
{
|
||||
std::stringstream sstime(strtime);
|
||||
struct std::tm tm = {};
|
||||
tm.tm_isdst = -1; // Detect daylight saving time.
|
||||
sstime >> std::get_time(&tm, "%Y-%m-%dT%T");
|
||||
std::time_t time = timelocal(&tm); // Assume time is local.
|
||||
return system_clock::from_time_t(time);
|
||||
}
|
||||
|
||||
const string timepoint_to_string(const system_clock::time_point &tp)
|
||||
{
|
||||
constexpr std::uint16_t bufsize = 32;
|
||||
std::time_t time = system_clock::to_time_t(tp);
|
||||
std::tm *tm;
|
||||
tm = std::localtime(&time);
|
||||
|
||||
char buffer[bufsize];
|
||||
std::strftime(buffer, bufsize, "%FT%T", tm);
|
||||
|
||||
return static_cast<const string>(buffer);
|
||||
}
|
|
@ -0,0 +1,32 @@
|
|||
/* 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/>.
|
||||
*/
|
||||
|
||||
#ifndef REMWHAREAD_TIME_HPP
|
||||
#define REMWHAREAD_TIME_HPP
|
||||
|
||||
#include <string>
|
||||
#include <chrono>
|
||||
|
||||
using std::string;
|
||||
using std::chrono::system_clock;
|
||||
|
||||
// Convert ISO 8601 time-string to time_point.
|
||||
const system_clock::time_point string_to_timepoint(const string &strtime);
|
||||
|
||||
// Convert time_point to USO 8601 time-string.
|
||||
const string timepoint_to_string(const system_clock::time_point &tp);
|
||||
|
||||
#endif // REMWHAREAD_TIME_HPP
|
|
@ -16,45 +16,13 @@
|
|||
|
||||
#include <exception>
|
||||
#include <string>
|
||||
#include <chrono>
|
||||
#include <vector>
|
||||
#include <catch.hpp>
|
||||
#include "parse_options.hpp"
|
||||
|
||||
using std::string;
|
||||
using std::chrono::system_clock;
|
||||
using std::chrono::seconds;
|
||||
using std::vector;
|
||||
|
||||
SCENARIO ("The time conversion works correctly")
|
||||
{
|
||||
bool exception = false;
|
||||
const string datetime = "1970-01-04T00:00:00";
|
||||
system_clock::time_point tp;
|
||||
seconds seconds_since_epoch;
|
||||
|
||||
GIVEN ("The date and time " + datetime)
|
||||
{
|
||||
try
|
||||
{
|
||||
tp = string_to_timepoint(datetime);
|
||||
seconds_since_epoch = std::chrono::duration_cast<seconds>
|
||||
(tp.time_since_epoch());
|
||||
}
|
||||
catch (const std::exception &e)
|
||||
{
|
||||
exception = true;
|
||||
}
|
||||
|
||||
THEN ("No exception is thrown")
|
||||
AND_THEN ("Seconds since epoch is not 0")
|
||||
{
|
||||
REQUIRE_FALSE(exception);
|
||||
REQUIRE(seconds_since_epoch != seconds(0));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
SCENARIO ("The option parser works correctly")
|
||||
{
|
||||
bool exception = false;
|
||||
|
|
|
@ -0,0 +1,74 @@
|
|||
/* 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/>.
|
||||
*/
|
||||
|
||||
#include <exception>
|
||||
#include <string>
|
||||
#include <chrono>
|
||||
#include <catch.hpp>
|
||||
#include "time.hpp"
|
||||
|
||||
using std::string;
|
||||
using std::chrono::system_clock;
|
||||
using std::chrono::seconds;
|
||||
|
||||
SCENARIO ("The time conversion works correctly")
|
||||
{
|
||||
bool exception = false;
|
||||
const string datetime = "1970-01-04T00:00:00";
|
||||
system_clock::time_point tp;
|
||||
seconds seconds_since_epoch;
|
||||
|
||||
GIVEN ("The date and time " + datetime + " as string")
|
||||
{
|
||||
try
|
||||
{
|
||||
tp = string_to_timepoint(datetime);
|
||||
seconds_since_epoch = std::chrono::duration_cast<seconds>
|
||||
(tp.time_since_epoch());
|
||||
}
|
||||
catch (const std::exception &e)
|
||||
{
|
||||
exception = true;
|
||||
}
|
||||
|
||||
THEN ("No exception is thrown")
|
||||
AND_THEN ("Seconds since epoch is not 0")
|
||||
{
|
||||
REQUIRE_FALSE(exception);
|
||||
REQUIRE(seconds_since_epoch != seconds(0));
|
||||
}
|
||||
|
||||
WHEN ("Converted back to string")
|
||||
{
|
||||
string datetime2;
|
||||
try
|
||||
{
|
||||
datetime2 = timepoint_to_string(tp);
|
||||
}
|
||||
catch (const std::exception &e)
|
||||
{
|
||||
exception = true;
|
||||
}
|
||||
|
||||
THEN ("No exception is thrown")
|
||||
AND_THEN ("Date and time is the same as the initial value")
|
||||
{
|
||||
REQUIRE_FALSE(exception);
|
||||
REQUIRE(datetime2 == datetime);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue