Newer
Older
//----------------------------------------------------------------------
// Includes
//----------------------------------------------------------------------
#include "MantidKernel/DateValidator.h"
#include <boost/lexical_cast.hpp>
namespace Mantid
{
namespace Kernel
{
/// constructor
DateValidator::DateValidator()
{
}
/// destructor
DateValidator::~DateValidator()
{
}
/// create a copy of the the validator
IValidator<std::string> *DateValidator::clone()
{
return new DateValidator(*this);
}
/** Checks the validity of date string ,expected format is "DD/MM/YYYY"
Janik Zikovsky
committed
* @param sDate :: expected date format
* @param error :: string which describes the error
* @return tm structure with the date information filled in
*/
struct tm DateValidator::getTimevalue(const std::string& sDate, std::string & error) const
{
struct tm timeinfo;
// Zero it out
timeinfo.tm_sec = 0; timeinfo.tm_min = 0; timeinfo.tm_hour = 0;
timeinfo.tm_mday = 0; timeinfo.tm_mon = 0; timeinfo.tm_year = 0;
timeinfo.tm_isdst = -1;
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
std::basic_string<char>::size_type index, off = 0;
int day, month, year;
//look for the first '/' to extract day part from the date string
index = sDate.find('/', off);
if (index == std::string::npos)
{
error = "Invalid Date:date format must be DD/MM/YYYY";
timeinfo.tm_mday = 0;
return timeinfo;
}
//get day part of the date
//day=atoi(sDate.substr(off,index-off).c_str());
try
{
day = boost::lexical_cast<int>(sDate.substr(off, index - off).c_str());
} catch (boost::bad_lexical_cast&)
{
error = "Invalid Date";
return timeinfo;
}
timeinfo.tm_mday = day;
//change the offset to the next position after "/"
off = index + 1;
//look for 2nd '/' to get month part from the date string
index = sDate.find('/', off);
if (index == std::string::npos)
{
error = "Invalid Date:date format must be DD/MM/YYYY";
return timeinfo;
}
//now get the month part
//month=atoi(sDate.substr(off,index-off).c_str());
try
{
month = boost::lexical_cast<int>(sDate.substr(off, index - off).c_str());
} catch (boost::bad_lexical_cast&)
{
error = "Invalid Date";
return timeinfo;
}
timeinfo.tm_mon = month - 1;
//change the offset to the position after "/"
off = index + 1;
//now get the year part from the date string
//year=atoi(sDate.substr(off,4).c_str());
try
{
year = boost::lexical_cast<int>(sDate.substr(off, 4).c_str());
} catch (boost::bad_lexical_cast&)
{
error = "Invalid Date";
return timeinfo;
}
timeinfo.tm_year = year - 1900;
return timeinfo;
}
/** Checks the given value is a valid date
Janik Zikovsky
committed
* @param value :: input date property to validate
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
* @return a string which describes the error else ""
*/
std::string DateValidator::checkValidity(const std::string& value) const
{
//empty strings are allowed
if (!value.compare(""))
{
return "";
}
std::string formaterror;
struct tm timeinfo = getTimevalue(value, formaterror);
if (!formaterror.empty())
{
return formaterror;
}
if (timeinfo.tm_mday < 1 || timeinfo.tm_mday > 31)
{
return "Invalid Date:Day part of the Date parameter must be between 1 and 31";
}
if (timeinfo.tm_mon < 0 || timeinfo.tm_mon > 11)
{
return "Invalid Date:Month part of the Date parameter must be between 1 and 12";
}
//get current time
time_t rawtime;
time(&rawtime);
struct tm* currenttime = localtime(&rawtime);
if (timeinfo.tm_year > currenttime->tm_year)
{
return "Invalid Date:Year part of the Date parameter can not be greater than the current year";
}
return "";
}
}
}