From 8d90035bbc858ae930e0e5c69c17c7ba142b7095 Mon Sep 17 00:00:00 2001 From: Paul Beckingham Date: Thu, 12 Jun 2008 23:58:58 -0400 Subject: [PATCH] - Now properly parses dates according to specified date format. --- ChangeLog | 1 + src/Config.cpp | 2 +- src/Date.cpp | 30 +++++++++++++++++++++++++----- src/task.cpp | 2 -- src/tests/date.t.cpp | 18 +++++++++++++++++- 5 files changed, 44 insertions(+), 9 deletions(-) diff --git a/ChangeLog b/ChangeLog index e9281cd96..c74c1d653 100644 --- a/ChangeLog +++ b/ChangeLog @@ -8,6 +8,7 @@ represents a feature release, and the z represents a patch. 1.2.0 (?) + Bug: "dateformat" configuration variable used to display dates, but not parse them + "task list x" now performs a caseless comparison between "x" and the description + + Task sub projects supported. ------ reality ----------------------------------- diff --git a/src/Config.cpp b/src/Config.cpp index a8ecf9587..c9e6bfed7 100644 --- a/src/Config.cpp +++ b/src/Config.cpp @@ -136,7 +136,7 @@ void Config::createDefault (const std::string& file) set ("data.location", taskDir); set ("command.logging", "off"); set ("confirmation", "yes"); - set ("next", 2); + set ("next", 1); set ("curses", "on"); set ("color", "on"); set ("color.overdue", "red"); diff --git a/src/Date.cpp b/src/Date.cpp index 502dec718..d2237b0d3 100644 --- a/src/Date.cpp +++ b/src/Date.cpp @@ -69,7 +69,7 @@ Date::Date (const std::string& mdy, const std::string format /* = "m/d/Y" */) { switch (format[f]) { - // Single digit. + // Single or double digit. case 'm': if (i >= mdy.length () || ! ::isdigit (mdy[i])) @@ -77,8 +77,18 @@ Date::Date (const std::string& mdy, const std::string format /* = "m/d/Y" */) throw std::string ("\"") + mdy + "\" is not a valid date."; } - month = ::atoi (mdy.substr (i, 1).c_str ()); - ++i; + if (i + 1 < mdy.length () && + mdy[i + 0] == '1' && + (mdy[i + 1] == '0' || mdy[i + 1] == '1' || mdy[i + 1] == '2')) + { + month = ::atoi (mdy.substr (i, 2).c_str ()); + i += 2; + } + else + { + month = ::atoi (mdy.substr (i, 1).c_str ()); + ++i; + } break; case 'd': @@ -88,8 +98,18 @@ Date::Date (const std::string& mdy, const std::string format /* = "m/d/Y" */) throw std::string ("\"") + mdy + "\" is not a valid date."; } - day = ::atoi (mdy.substr (i, 1).c_str ()); - ++i; + if (i + 1 < mdy.length () && + (mdy[i + 0] == '1' || mdy[i + 0] == '2' || mdy[i + 0] == '3') && + ::isdigit (mdy[i + 1])) + { + day = ::atoi (mdy.substr (i, 2).c_str ()); + i += 2; + } + else + { + day = ::atoi (mdy.substr (i, 1).c_str ()); + ++i; + } break; // Double digit. diff --git a/src/task.cpp b/src/task.cpp index 7ceeac4ae..0d243352a 100644 --- a/src/task.cpp +++ b/src/task.cpp @@ -952,8 +952,6 @@ void handleInfo (const TDB& tdb, T& task, Config& conf) if (due.length ()) { - Date dt (::atoi (due.c_str ())); - overdue = (dt < now) ? true : false; now += 7 * 86400; imminent = dt < now ? true : false; diff --git a/src/tests/date.t.cpp b/src/tests/date.t.cpp index 519fa495b..e602c5b10 100644 --- a/src/tests/date.t.cpp +++ b/src/tests/date.t.cpp @@ -9,7 +9,7 @@ //////////////////////////////////////////////////////////////////////////////// int main (int argc, char** argv) { - plan (52); + plan (63); Date now; Date yesterday; @@ -27,6 +27,8 @@ int main (int argc, char** argv) ok (Date::leapYear (2008), "2008 is a leap year"); notok (Date::leapYear (2007), "2007 is not a leap year"); + ok (Date::leapYear (2000), "2000 is a leap year"); + ok (Date::leapYear (1900), "1900 is a leap year"); is (Date::daysInMonth (2, 2008), 29, "29 days in February 2008"); is (Date::daysInMonth (2, 2007), 28, "28 days in February 2007"); @@ -91,6 +93,20 @@ int main (int argc, char** argv) is (fromString3.day (), 1, "ctor (std::string) -> d"); is (fromString3.year (), 2008, "ctor (std::string) -> y"); + Date fromString4 ("12/31/2007"); + is (fromString4.month (), 12, "ctor (std::string) -> m"); + is (fromString4.day (), 31, "ctor (std::string) -> d"); + is (fromString4.year (), 2007, "ctor (std::string) -> y"); + + Date fromString5 ("12/31/2007", "m/d/Y"); + is (fromString5.month (), 12, "ctor (std::string) -> m"); + is (fromString5.day (), 31, "ctor (std::string) -> d"); + is (fromString5.year (), 2007, "ctor (std::string) -> y"); + + Date fromString6 ("20071231", "YMD"); + is (fromString6.month (), 12, "ctor (std::string) -> m"); + is (fromString6.day (), 31, "ctor (std::string) -> d"); + is (fromString6.year (), 2007, "ctor (std::string) -> y"); return 0; }