Enhancements - date

- Relocated valid.cpp/dataValid to Date::valid.
- Added new unit tests in date.t.cpp.
This commit is contained in:
Paul Beckingham 2009-06-13 12:51:51 -04:00
parent d99dec5556
commit 0665caae55
5 changed files with 23 additions and 16 deletions

View file

@ -254,6 +254,22 @@ const std::string Date::toString (const std::string& format /*= "m/d/Y" */) cons
return formatted;
}
////////////////////////////////////////////////////////////////////////////////
bool Date::valid (const std::string& input, const std::string& format)
{
try
{
Date test (input, format);
}
catch (...)
{
return false;
}
return true;
}
////////////////////////////////////////////////////////////////////////////////
bool Date::valid (const int m, const int d, const int y)
{

View file

@ -46,6 +46,7 @@ public:
time_t toEpoch ();
void toMDY (int&, int&, int&);
const std::string toString (const std::string& format = "m/d/Y") const;
static bool valid (const std::string&, const std::string& format = "m/d/Y");
static bool valid (const int, const int, const int);
static bool leapYear (int);

View file

@ -39,7 +39,6 @@
// valid.cpp
void guess (const std::string&, const char**, std::string&);
bool validPriority (const std::string&);
bool validDate (std::string&);
bool validDescription (const std::string&);
bool validDuration (std::string&);
void validReportColumns (const std::vector <std::string>&);

View file

@ -34,7 +34,7 @@ Context context;
////////////////////////////////////////////////////////////////////////////////
int main (int argc, char** argv)
{
UnitTest t (100);
UnitTest t (102);
try
{
@ -75,6 +75,9 @@ int main (int argc, char** argv)
t.ok (Date::valid (2, 29, 2008), "valid: 2/29/2008");
t.notok (Date::valid (2, 29, 2007), "invalid: 2/29/2007");
t.ok (Date::valid ("2/29/2008"), "valid: 2/29/2008");
t.notok (Date::valid ("2/29/2007"), "invalid: 2/29/2007");
// Leap year.
t.ok (Date::leapYear (2008), "2008 is a leap year");
t.notok (Date::leapYear (2007), "2007 is not a leap year");

View file

@ -152,18 +152,6 @@ void guess (
guess (type, options, candidate);
}
////////////////////////////////////////////////////////////////////////////////
bool validDate (std::string& date)
{
Date test (date, context.config.get ("dateformat", "m/d/Y"));
char epoch[16];
sprintf (epoch, "%d", (int) test.toEpoch ());
date = epoch;
return true;
}
////////////////////////////////////////////////////////////////////////////////
bool validPriority (const std::string& input)
{
@ -207,10 +195,10 @@ bool validAttribute (std::string& name, std::string& value)
guess ("color", colors, value);
else if (name == "due" && value != "")
validDate (value);
Date (value);
else if (name == "until" && value != "")
validDate (value);
Date (value);
else if (name == "priority")
{