Mantid
Loading...
Searching...
No Matches
DateValidator.cpp
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
4// NScD Oak Ridge National Laboratory, European Spallation Source,
5// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
6// SPDX - License - Identifier: GPL - 3.0 +
8#include <boost/lexical_cast.hpp>
9#include <ctime>
10#include <memory>
11
12namespace Mantid::Kernel {
13
14namespace {
15//
16// Keeps time.h out of the header
17//
23struct tm getTimeValue(const std::string &sDate, std::string &error) {
24 struct tm timeinfo;
25 // Zero it out
26 timeinfo.tm_sec = 0;
27 timeinfo.tm_min = 0;
28 timeinfo.tm_hour = 0;
29 timeinfo.tm_mday = 0;
30 timeinfo.tm_mon = 0;
31 timeinfo.tm_year = 0;
32 timeinfo.tm_wday = 0;
33 timeinfo.tm_yday = 0;
34 timeinfo.tm_isdst = -1;
35#ifndef _WIN32
36 timeinfo.tm_gmtoff = 0;
37 timeinfo.tm_zone = nullptr;
38#endif
39
40 std::basic_string<char>::size_type index, off = 0;
41 int day, month, year;
42
43 // look for the first '/' to extract day part from the date string
44 index = sDate.find('/', off);
45 if (index == std::string::npos) {
46 error = "Invalid Date:date format must be DD/MM/YYYY";
47 timeinfo.tm_mday = 0;
48 return timeinfo;
49 }
50 // get day part of the date
51 try {
52 day = boost::lexical_cast<int>(sDate.substr(off, index - off).c_str());
53 } catch (boost::bad_lexical_cast &) {
54 error = "Invalid Date";
55 return timeinfo;
56 }
57 timeinfo.tm_mday = day;
58
59 // change the offset to the next position after "/"
60 off = index + 1;
61 // look for 2nd '/' to get month part from the date string
62 index = sDate.find('/', off);
63 if (index == std::string::npos) {
64 error = "Invalid Date:date format must be DD/MM/YYYY";
65 return timeinfo;
66 }
67 // now get the month part
68 try {
69 month = boost::lexical_cast<int>(sDate.substr(off, index - off).c_str());
70
71 } catch (boost::bad_lexical_cast &) {
72 error = "Invalid Date";
73 return timeinfo;
74 }
75 timeinfo.tm_mon = month - 1;
76 // change the offset to the position after "/"
77 off = index + 1;
78 // now get the year part from the date string
79 try {
80 year = boost::lexical_cast<int>(sDate.substr(off, 4).c_str());
81
82 } catch (boost::bad_lexical_cast &) {
83 error = "Invalid Date";
84 return timeinfo;
85 }
86
87 timeinfo.tm_year = year - 1900;
88
89 return timeinfo;
90}
91} // namespace
92
94IValidator_sptr DateValidator::clone() const { return std::make_shared<DateValidator>(*this); }
95
100std::string DateValidator::checkValidity(const std::string &value) const {
101 // empty strings are allowed
102 if (value.empty()) {
103 return "";
104 }
105 std::string formaterror;
106 struct tm timeinfo = getTimeValue(value, formaterror);
107 if (!formaterror.empty()) {
108 return formaterror;
109 }
110
111 if (timeinfo.tm_mday < 1 || timeinfo.tm_mday > 31) {
112 return "Invalid Date:Day part of the Date parameter must be between 1 and "
113 "31";
114 }
115 if (timeinfo.tm_mon < 0 || timeinfo.tm_mon > 11) {
116 return "Invalid Date:Month part of the Date parameter must be between 1 "
117 "and 12";
118 }
119 // get current time
120 time_t rawtime;
121 time(&rawtime);
122 struct tm *currenttime = localtime(&rawtime);
123 if (timeinfo.tm_year > currenttime->tm_year) {
124 return "Invalid Date:Year part of the Date parameter can not be greater "
125 "than the current year";
126 }
127 return "";
128}
129} // namespace Mantid::Kernel
double value
The value of the point.
Definition: FitMW.cpp:51
double error
Definition: IndexPeaks.cpp:133
std::map< DeltaEMode::Type, std::string > index
Definition: DeltaEMode.cpp:19
std::string checkValidity(const std::string &value) const override
Checks the value is valid.
IValidator_sptr clone() const override
Clone the current state.
std::shared_ptr< IValidator > IValidator_sptr
A shared_ptr to an IValidator.
Definition: IValidator.h:26
STL namespace.