ISO8601: Implemented ::dayNameShort and ::monthNameShort

- Added tests.
This commit is contained in:
Paul Beckingham 2015-11-01 18:01:24 -05:00
parent 9143d8b8e7
commit 7a48d25eaa
3 changed files with 80 additions and 13 deletions

View file

@ -1264,6 +1264,31 @@ std::string ISO8601d::monthName (int month)
return Lexer::ucFirst (months[month - 1]); return Lexer::ucFirst (months[month - 1]);
} }
////////////////////////////////////////////////////////////////////////////////
// Static
std::string ISO8601d::monthNameShort (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 Lexer::ucFirst (months[month - 1]).substr (0, 3);
}
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
// Static // Static
std::string ISO8601d::dayName (int dow) std::string ISO8601d::dayName (int dow)
@ -1282,6 +1307,24 @@ std::string ISO8601d::dayName (int dow)
return Lexer::ucFirst (days[dow]); return Lexer::ucFirst (days[dow]);
} }
////////////////////////////////////////////////////////////////////////////////
// Static
std::string ISO8601d::dayNameShort (int dow)
{
static const char* days[7] =
{
STRING_DATE_SUNDAY,
STRING_DATE_MONDAY,
STRING_DATE_TUESDAY,
STRING_DATE_WEDNESDAY,
STRING_DATE_THURSDAY,
STRING_DATE_FRIDAY,
STRING_DATE_SATURDAY,
};
return Lexer::ucFirst (days[dow]).substr (0, 3);
}
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
// Static // Static
int ISO8601d::dayOfWeek (const std::string& input) int ISO8601d::dayOfWeek (const std::string& input)

View file

@ -67,7 +67,9 @@ public:
static int daysInMonth (int, int); static int daysInMonth (int, int);
static int daysInYear (int); static int daysInYear (int);
static std::string monthName (int); static std::string monthName (int);
static std::string monthNameShort (int);
static std::string dayName (int); static std::string dayName (int);
static std::string dayNameShort (int);
static int dayOfWeek (const std::string&); static int dayOfWeek (const std::string&);
static int dayOfWeek (int, int, int); static int dayOfWeek (int, int, int);
static int monthOfYear (const std::string&); static int monthOfYear (const std::string&);

View file

@ -71,7 +71,7 @@ void testParse (
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
int main (int, char**) int main (int, char**)
{ {
UnitTest t (1006); UnitTest t (1025);
ISO8601d iso; ISO8601d iso;
std::string::size_type start = 0; std::string::size_type start = 0;
@ -318,6 +318,20 @@ int main (int, char**)
t.is (ISO8601d::monthName (11), "November", "11 = November"); t.is (ISO8601d::monthName (11), "November", "11 = November");
t.is (ISO8601d::monthName (12), "December", "12 = December"); t.is (ISO8601d::monthName (12), "December", "12 = December");
// Names.
t.is (ISO8601d::monthNameShort (1), "Jan", "1 = Jan");
t.is (ISO8601d::monthNameShort (2), "Feb", "2 = Feb");
t.is (ISO8601d::monthNameShort (3), "Mar", "3 = Mar");
t.is (ISO8601d::monthNameShort (4), "Apr", "4 = Apr");
t.is (ISO8601d::monthNameShort (5), "May", "5 = May");
t.is (ISO8601d::monthNameShort (6), "Jun", "6 = Jun");
t.is (ISO8601d::monthNameShort (7), "Jul", "7 = Jul");
t.is (ISO8601d::monthNameShort (8), "Aug", "8 = Aug");
t.is (ISO8601d::monthNameShort (9), "Sep", "9 = Sep");
t.is (ISO8601d::monthNameShort (10), "Oct", "10 = Oct");
t.is (ISO8601d::monthNameShort (11), "Nov", "11 = Nov");
t.is (ISO8601d::monthNameShort (12), "Dec", "12 = Dec");
// Names. // Names.
t.is (ISO8601d::monthOfYear ("January"), 1, "January = 1"); t.is (ISO8601d::monthOfYear ("January"), 1, "January = 1");
t.is (ISO8601d::monthOfYear ("February"), 2, "February = 2"); t.is (ISO8601d::monthOfYear ("February"), 2, "February = 2");
@ -340,6 +354,14 @@ int main (int, char**)
t.is (ISO8601d::dayName (5), "Friday", "5 == Friday"); t.is (ISO8601d::dayName (5), "Friday", "5 == Friday");
t.is (ISO8601d::dayName (6), "Saturday", "6 == Saturday"); t.is (ISO8601d::dayName (6), "Saturday", "6 == Saturday");
t.is (ISO8601d::dayNameShort (0), "Sun", "0 == Sun");
t.is (ISO8601d::dayNameShort (1), "Mon", "1 == Mon");
t.is (ISO8601d::dayNameShort (2), "Tue", "2 == Tue");
t.is (ISO8601d::dayNameShort (3), "Wed", "3 == Wed");
t.is (ISO8601d::dayNameShort (4), "Thu", "4 == Thu");
t.is (ISO8601d::dayNameShort (5), "Fri", "5 == Fri");
t.is (ISO8601d::dayNameShort (6), "Sat", "6 == Sat");
t.is (ISO8601d::dayOfWeek ("SUNDAY"), 0, "SUNDAY == 0"); t.is (ISO8601d::dayOfWeek ("SUNDAY"), 0, "SUNDAY == 0");
t.is (ISO8601d::dayOfWeek ("sunday"), 0, "sunday == 0"); t.is (ISO8601d::dayOfWeek ("sunday"), 0, "sunday == 0");
t.is (ISO8601d::dayOfWeek ("Sunday"), 0, "Sunday == 0"); t.is (ISO8601d::dayOfWeek ("Sunday"), 0, "Sunday == 0");