Add date & time to entries.

This commit is contained in:
tastytea 2020-07-05 08:01:30 +02:00
parent 4c2db38cff
commit 2e164316dd
Signed by: tastytea
GPG Key ID: CFC39497F1B26E07
5 changed files with 108 additions and 1 deletions

View File

@ -17,9 +17,11 @@
#include "cgi.hpp"
#include "files.hpp"
#include "fs-compat.hpp"
#include "time.hpp"
#include <cgicc/Cgicc.h>
#include <chrono>
#include <fstream>
#include <ios>
#include <iostream>
@ -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())

View File

@ -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<vector<string>>();
try
{
entry.report_time = json[0].at("report_time").get<string>();
}
// 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;
}

43
src/time.cpp Normal file
View File

@ -0,0 +1,43 @@
/* This file is part of FediBlock-backend.
* Copyright © 2020 tastytea <tastytea@tastytea.de>
*
* 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 <http://www.gnu.org/licenses/>.
*/
#include "time.hpp"
#include <cstdint>
#include <ctime>
#include <string_view>
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

44
src/time.hpp Normal file
View File

@ -0,0 +1,44 @@
/* This file is part of FediBlock-backend.
* Copyright © 2020 tastytea <tastytea@tastytea.de>
*
* 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 <http://www.gnu.org/licenses/>.
*/
#ifndef FEDIBLOCK_BACKEND_TIME_HPP
#define FEDIBLOCK_BACKEND_TIME_HPP
#include <chrono>
#include <string>
#include <string_view>
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

View File

@ -33,6 +33,7 @@ struct entry_type
vector<string> receipts;
string description;
string screenshot_filepath;
string report_time;
};
enum class http_method