diff --git a/src/Date.cpp b/src/Date.cpp index 452dc58df..be3889e7c 100644 --- a/src/Date.cpp +++ b/src/Date.cpp @@ -339,7 +339,7 @@ bool Date::valid (const int d, const int y) if (y < 0) return false; - if (d < 0 || d > 365) + if (d < 1 || d > Date::daysInYear (y)) return false; return true; @@ -371,6 +371,12 @@ int Date::daysInMonth (int month, int year) return days[Date::leapYear (year) ? 1 : 0][month - 1]; } +//////////////////////////////////////////////////////////////////////////////// +int Date::daysInYear (int year) +{ + return Date::leapYear (year) ? 366 : 365; +} + //////////////////////////////////////////////////////////////////////////////// std::string Date::monthName (int month) { diff --git a/src/Date.h b/src/Date.h index 9c80b1028..413b7486e 100644 --- a/src/Date.h +++ b/src/Date.h @@ -67,6 +67,7 @@ public: static time_t easter (int year); static bool leapYear (int); static int daysInMonth (int, int); + static int daysInYear (int); static std::string monthName (int); static void dayName (int, std::string&); static std::string dayName (int); diff --git a/src/Nibbler.cpp b/src/Nibbler.cpp index e54699d02..09a88d73d 100644 --- a/src/Nibbler.cpp +++ b/src/Nibbler.cpp @@ -734,7 +734,7 @@ bool Nibbler::getDate (const std::string& format, time_t& t) { case 'm': if (i + 2 <= _length && - (_input[i + 0] == '0' || _input[i + 0] == '1') && + isdigit (_input[i + 0]) && isdigit (_input[i + 1])) { month = atoi (_input.substr (i, 2).c_str ()); @@ -882,7 +882,7 @@ bool Nibbler::getDate (const std::string& format, time_t& t) case 'h': if (i + 2 <= _length && - (_input[i + 0] == '0' || _input[i + 0] == '1') && + isdigit (_input[i + 0]) && isdigit (_input[i + 1])) { hour = atoi (_input.substr (i, 2).c_str ()); @@ -1019,27 +1019,15 @@ bool Nibbler::getDate (const std::string& format, time_t& t) minute = (minute == -1) ? 0 : minute; second = (second == -1) ? 0 : second; + // Check that values are correct + if (! Date::valid (month, day, year, hour, minute, second)) + return false; + // Convert to epoch. struct tm tms = {0}; tms.tm_isdst = -1; // Requests that mktime determine summer time effect. - - if (month == 0 && day >= 0 && day <= 365) - { - tms.tm_yday = day; - tms.tm_mon = 0; - - if (! Date::valid (day, year)) - return false; - } - else - { - tms.tm_mday = day; - tms.tm_mon = month > 0 ? month - 1 : 0; - - if (! Date::valid (month, day, year)) - return false; - } - + tms.tm_mon = month - 1; + tms.tm_mday = day; tms.tm_year = year - 1900; tms.tm_hour = hour; tms.tm_min = minute; diff --git a/test/date.t.cpp b/test/date.t.cpp index cc127a3ca..792fbbc18 100644 --- a/test/date.t.cpp +++ b/test/date.t.cpp @@ -35,7 +35,7 @@ Context context; //////////////////////////////////////////////////////////////////////////////// int main (int argc, char** argv) { - UnitTest t (177); + UnitTest t (179); try { @@ -86,6 +86,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 (366, 2008), "valid: 366 days in 2008"); + t.notok (Date::valid (366, 2007), "invalid: 366 days in 2007"); + // Time validity. t.ok (Date::valid (2, 28, 2010, 0, 0, 0), "valid 2/28/2010 0:00:00"); t.ok (Date::valid (2, 28, 2010, 23, 59, 59), "valid 2/28/2010 23:59:59");