From 2e164316dde35bdff472c84a0c8e8c66344ac2bb Mon Sep 17 00:00:00 2001 From: tastytea Date: Sun, 5 Jul 2020 08:01:30 +0200 Subject: [PATCH] Add date & time to entries. --- src/cgi.cpp | 4 ++++ src/json.cpp | 17 ++++++++++++++++- src/time.cpp | 43 +++++++++++++++++++++++++++++++++++++++++++ src/time.hpp | 44 ++++++++++++++++++++++++++++++++++++++++++++ src/types.hpp | 1 + 5 files changed, 108 insertions(+), 1 deletion(-) create mode 100644 src/time.cpp create mode 100644 src/time.hpp diff --git a/src/cgi.cpp b/src/cgi.cpp index a31c86a..9c94e59 100644 --- a/src/cgi.cpp +++ b/src/cgi.cpp @@ -17,9 +17,11 @@ #include "cgi.hpp" #include "files.hpp" #include "fs-compat.hpp" +#include "time.hpp" #include +#include #include #include #include @@ -39,6 +41,7 @@ using std::string; using std::string_view; using std::stringstream; using std::vector; +using std::chrono::system_clock; entry_type parse_formdata() { @@ -49,6 +52,7 @@ entry_type parse_formdata() entry.tags = string_to_vector(cgi("tags")); entry.receipts = string_to_vector(cgi("receipts")); entry.description = cgi("description"); + entry.report_time = time::to_string(system_clock::now()); const auto screenshot = cgi.getFile("screenshot"); if (screenshot != cgi.getFiles().end()) diff --git a/src/json.cpp b/src/json.cpp index 2d304ae..bb53b6b 100644 --- a/src/json.cpp +++ b/src/json.cpp @@ -30,7 +30,8 @@ string to_json(const entry_type &entry) {"tags", entry.tags}, {"receipts", entry.receipts}, {"description", entry.description}, - {"screenshot", filename}}; + {"screenshot", filename}, + {"report_time", entry.report_time}}; return json.dump(4); } @@ -61,6 +62,20 @@ entry_type from_json(const string_view json_string) #endif {} entry.tags = json[0].at("tags").get>(); + try + { + entry.report_time = json[0].at("report_time").get(); + } + // TODO: this is only necessary for compatibility with old test data. Remove + // before going live. +#if NLOHMANN_JSON_VERSION_MAJOR >= 3 + catch (const nlohmann::detail::out_of_range &) +#else + catch (const std::out_of_range &) +#endif + { + entry.report_time = "1970-01-01T00:00:00"; + } return entry; } diff --git a/src/time.cpp b/src/time.cpp new file mode 100644 index 0000000..472d213 --- /dev/null +++ b/src/time.cpp @@ -0,0 +1,43 @@ +/* This file is part of FediBlock-backend. + * Copyright © 2020 tastytea + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero 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 Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +#include "time.hpp" + +#include +#include +#include + +namespace FediBlock::time +{ + +using std::string_view; +using std::time_t; +using std::uint16_t; + +string to_string(const system_clock::time_point &timepoint, + const string_view format) +{ + constexpr uint16_t bufsize = 1024; + time_t time = system_clock::to_time_t(timepoint); + std::tm *tm{nullptr}; + tm = std::gmtime(&time); + char buffer[bufsize]; + std::strftime(buffer, bufsize, format.data(), tm); + + return buffer; +} + +} // namespace FediBlock::time diff --git a/src/time.hpp b/src/time.hpp new file mode 100644 index 0000000..feebede --- /dev/null +++ b/src/time.hpp @@ -0,0 +1,44 @@ +/* This file is part of FediBlock-backend. + * Copyright © 2020 tastytea + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero 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 Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +#ifndef FEDIBLOCK_BACKEND_TIME_HPP +#define FEDIBLOCK_BACKEND_TIME_HPP + +#include +#include +#include + +namespace FediBlock::time +{ + +using std::string; +using std::string_view; +using std::chrono::system_clock; + +/*! + * @brief Return date and time as string. + + * @param timepoint The point in time to convert. + * @param format Format of the string (optional, defaults to %FT%T). + * + * @since 0.2.0 + */ +[[nodiscard]] string to_string(const system_clock::time_point &timepoint, + string_view format = "%FT%T"); + +} // namespace FediBlock::time + +#endif // FEDIBLOCK_BACKEND_TIME_HPP diff --git a/src/types.hpp b/src/types.hpp index 1465893..8e3242a 100644 --- a/src/types.hpp +++ b/src/types.hpp @@ -33,6 +33,7 @@ struct entry_type vector receipts; string description; string screenshot_filepath; + string report_time; }; enum class http_method