diff --git a/src/Date.cpp b/src/Date.cpp index 761526c63..d895f7a04 100644 --- a/src/Date.cpp +++ b/src/Date.cpp @@ -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) { diff --git a/src/Date.h b/src/Date.h index 74c008391..70a167187 100644 --- a/src/Date.h +++ b/src/Date.h @@ -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); diff --git a/src/main.h b/src/main.h index 07835bec1..aa1d36284 100644 --- a/src/main.h +++ b/src/main.h @@ -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 &); diff --git a/src/tests/date.t.cpp b/src/tests/date.t.cpp index 97c87ce01..1e8c597ae 100644 --- a/src/tests/date.t.cpp +++ b/src/tests/date.t.cpp @@ -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"); diff --git a/src/valid.cpp b/src/valid.cpp index 45f54f453..3b183fa5a 100644 --- a/src/valid.cpp +++ b/src/valid.cpp @@ -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") {