mirror of
https://github.com/OpenSpace/OpenSpace.git
synced 2026-02-22 12:59:07 -06:00
Add DateTime verifier
This commit is contained in:
@@ -180,6 +180,17 @@ struct DirectoryVerifier : public StringVerifier {
|
||||
std::string type() const override;
|
||||
};
|
||||
|
||||
/**
|
||||
* A Verifier that checks whether a given key inside a ghoul::Dictionary is a string and
|
||||
* a valid date time
|
||||
*/
|
||||
struct DateTimeVerifier : public StringVerifier {
|
||||
TestResult operator()(const ghoul::Dictionary& dict,
|
||||
const std::string& key) const override;
|
||||
|
||||
std::string type() const override;
|
||||
};
|
||||
|
||||
/**
|
||||
* A Verifier that checks whether a given key inside a ghoul::Dictionary is another
|
||||
* ghoul::Dictionary. The constructor takes a list of DocumentationEntry%s, which are used
|
||||
|
||||
@@ -28,6 +28,8 @@
|
||||
#include <ghoul/misc/misc.h>
|
||||
#include <algorithm>
|
||||
#include <filesystem>
|
||||
#include <iomanip>
|
||||
#include <sstream>
|
||||
|
||||
namespace openspace::documentation {
|
||||
|
||||
@@ -226,6 +228,57 @@ std::string DirectoryVerifier::type() const {
|
||||
return "Directory";
|
||||
}
|
||||
|
||||
TestResult DateTimeVerifier::operator()(const ghoul::Dictionary& dict,
|
||||
const std::string& key) const
|
||||
{
|
||||
TestResult res = StringVerifier::operator()(dict, key);
|
||||
if (!res.success) {
|
||||
return res;
|
||||
}
|
||||
|
||||
std::string dateTime = dict.value<std::string>(key);
|
||||
std::string format = "%Y %m %d %H:%M:%S"; // YYYY MM DD hh:mm:ss
|
||||
|
||||
std::tm t = {};
|
||||
std::istringstream ss(dateTime);
|
||||
ss >> std::get_time(&t, format.c_str());
|
||||
|
||||
// first check format (automatically checks if valid time)
|
||||
if (ss.fail()) {
|
||||
res.success = false;
|
||||
TestResult::Offense off;
|
||||
off.offender = key;
|
||||
off.reason = TestResult::Offense::Reason::Verification;
|
||||
off.explanation = "Not a valid format";
|
||||
res.offenses.push_back(off);
|
||||
}
|
||||
// then check if valid date
|
||||
else {
|
||||
// normalize e.g. 29/02/2013 would become 01/03/2013
|
||||
std::tm t_copy(t);
|
||||
time_t when = mktime(&t_copy);
|
||||
std::tm *norm = localtime(&when);
|
||||
|
||||
// validate (is the normalized date still the same?):
|
||||
if (norm->tm_mday != t.tm_mday &&
|
||||
norm->tm_mon != t.tm_mon &&
|
||||
norm->tm_year != t.tm_year)
|
||||
{
|
||||
res.success = false;
|
||||
TestResult::Offense off;
|
||||
off.offender = key;
|
||||
off.reason = TestResult::Offense::Reason::Verification;
|
||||
off.explanation = "Not a valid date";
|
||||
res.offenses.push_back(off);
|
||||
}
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
std::string DateTimeVerifier::type() const {
|
||||
return "Date and time";
|
||||
}
|
||||
|
||||
TestResult Color3Verifier::operator()(const ghoul::Dictionary& dictionary,
|
||||
const std::string& key) const
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user