diff --git a/src/ISO8601.cpp b/src/ISO8601.cpp index 71671072c..2a6ef6ddc 100644 --- a/src/ISO8601.cpp +++ b/src/ISO8601.cpp @@ -806,6 +806,18 @@ bool ISO8601d::leapYear (int year) ! (year % 400); } +//////////////////////////////////////////////////////////////////////////////// +// Static +int ISO8601d::daysInMonth (int month, int year) +{ + static int days[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; + + if (month == 2 && ISO8601d::leapYear (year)) + return 29; + + return days[month - 1]; +} + //////////////////////////////////////////////////////////////////////////////// // Static int ISO8601d::dayOfWeek (const std::string& input) diff --git a/src/ISO8601.h b/src/ISO8601.h index 2ee9b0432..34669e9af 100644 --- a/src/ISO8601.h +++ b/src/ISO8601.h @@ -45,6 +45,7 @@ public: bool parse (const std::string&, std::string::size_type&, const std::string& format = ""); static bool leapYear (int); + static int daysInMonth (int, int); static int dayOfWeek (const std::string&); diff --git a/test/iso8601d.t.cpp b/test/iso8601d.t.cpp index bdf3682f0..b3a5bd60d 100644 --- a/test/iso8601d.t.cpp +++ b/test/iso8601d.t.cpp @@ -71,7 +71,7 @@ void testParse ( //////////////////////////////////////////////////////////////////////////////// int main (int argc, char** argv) { - UnitTest t (792); + UnitTest t (794); ISO8601d iso; std::string::size_type start = 0; @@ -224,6 +224,10 @@ int main (int argc, char** argv) t.ok (ISO8601d::leapYear (2000), "2000 is a leap year"); t.notok (ISO8601d::leapYear (1900), "1900 is not a leap year"); + // Days in month. + t.is (ISO8601d::daysInMonth (2, 2008), 29, "29 days in February 2008"); + t.is (ISO8601d::daysInMonth (2, 2007), 28, "28 days in February 2007"); + t.is (ISO8601d::dayOfWeek ("SUNDAY"), 0, "SUNDAY == 0"); t.is (ISO8601d::dayOfWeek ("sunday"), 0, "sunday == 0"); t.is (ISO8601d::dayOfWeek ("Sunday"), 0, "Sunday == 0");