ISO8601d: Added ::monthName and tests

This commit is contained in:
Paul Beckingham 2015-09-26 14:02:26 -04:00
parent 965415d7a4
commit 0d6788635e
3 changed files with 41 additions and 1 deletions

View file

@ -27,6 +27,7 @@
#include <cmake.h>
#include <sstream>
#include <stdlib.h>
#include <assert.h>
#include <Lexer.h>
#include <ISO8601.h>
#include <Date.h>
@ -825,6 +826,30 @@ int ISO8601d::daysInYear (int year)
return ISO8601d::leapYear (year) ? 366 : 365;
}
////////////////////////////////////////////////////////////////////////////////
std::string ISO8601d::monthName (int month)
{
static const char* months[12] =
{
STRING_DATE_JANUARY,
STRING_DATE_FEBRUARY,
STRING_DATE_MARCH,
STRING_DATE_APRIL,
STRING_DATE_MAY,
STRING_DATE_JUNE,
STRING_DATE_JULY,
STRING_DATE_AUGUST,
STRING_DATE_SEPTEMBER,
STRING_DATE_OCTOBER,
STRING_DATE_NOVEMBER,
STRING_DATE_DECEMBER,
};
assert (month > 0);
assert (month <= 12);
return ucFirst (months[month - 1]);
}
////////////////////////////////////////////////////////////////////////////////
// Static
int ISO8601d::dayOfWeek (const std::string& input)

View file

@ -47,6 +47,7 @@ public:
static bool leapYear (int);
static int daysInMonth (int, int);
static int daysInYear (int);
static std::string monthName (int);
static int dayOfWeek (const std::string&);

View file

@ -71,7 +71,7 @@ void testParse (
////////////////////////////////////////////////////////////////////////////////
int main (int argc, char** argv)
{
UnitTest t (796);
UnitTest t (808);
ISO8601d iso;
std::string::size_type start = 0;
@ -232,6 +232,20 @@ int main (int argc, char** argv)
t.is (ISO8601d::daysInMonth (2, 2008), 29, "29 days in February 2008");
t.is (ISO8601d::daysInMonth (2, 2007), 28, "28 days in February 2007");
// Names.
t.is (ISO8601d::monthName (1), "January", "1 = January");
t.is (ISO8601d::monthName (2), "February", "2 = February");
t.is (ISO8601d::monthName (3), "March", "3 = March");
t.is (ISO8601d::monthName (4), "April", "4 = April");
t.is (ISO8601d::monthName (5), "May", "5 = May");
t.is (ISO8601d::monthName (6), "June", "6 = June");
t.is (ISO8601d::monthName (7), "July", "7 = July");
t.is (ISO8601d::monthName (8), "August", "8 = August");
t.is (ISO8601d::monthName (9), "September", "9 = September");
t.is (ISO8601d::monthName (10), "October", "10 = October");
t.is (ISO8601d::monthName (11), "November", "11 = November");
t.is (ISO8601d::monthName (12), "December", "12 = December");
t.is (ISO8601d::dayOfWeek ("SUNDAY"), 0, "SUNDAY == 0");
t.is (ISO8601d::dayOfWeek ("sunday"), 0, "sunday == 0");
t.is (ISO8601d::dayOfWeek ("Sunday"), 0, "Sunday == 0");