forked from npshub/mantid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DateTimeValidator.cpp
52 lines (46 loc) · 1.69 KB
/
DateTimeValidator.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// Mantid Repository : https://github.com/mantidproject/mantid
//
// Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
// NScD Oak Ridge National Laboratory, European Spallation Source,
// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
// SPDX - License - Identifier: GPL - 3.0 +
#include "MantidKernel/DateTimeValidator.h"
#include "MantidKernel/DateAndTimeHelpers.h"
#include "MantidKernel/System.h"
#include <memory>
#include <stdexcept>
namespace Mantid::Kernel {
using namespace DateAndTimeHelpers;
/**
* @return A clone of the current state of the validator
*/
IValidator_sptr DateTimeValidator::clone() const { return std::make_shared<DateTimeValidator>(*this); }
DateTimeValidator::DateTimeValidator() { m_allowedEmpty = false; }
/**
* Sets the value of the m_allowEmpty variable
* @param allow The new value of m_allowEmpty
*/
void DateTimeValidator::allowEmpty(const bool &allow) { m_allowedEmpty = allow; }
/**
* @param value A string to check for an ISO formatted timestamp
* @return An empty string if the value is valid or an string containing
* a description of the error otherwise
*/
std::string DateTimeValidator::checkValidity(const std::string &value) const {
// simply pass off the work DateAndTime constructor
// the DateAndTimeHelpers::stringIsISO8601 does not seem strict enough, it
// accepts empty strings & strings of letters!
if (m_allowedEmpty && value.empty()) {
return "";
} else {
std::string error;
try {
Types::Core::DateAndTime timestamp(value);
UNUSED_ARG(timestamp);
} catch (std::invalid_argument &exc) {
error = exc.what();
}
return error;
}
}
} // namespace Mantid::Kernel