From ab0d929a3ccc29376173c141dc85d50b3e134387 Mon Sep 17 00:00:00 2001 From: Paul Beckingham Date: Mon, 6 Jan 2014 23:42:02 -0500 Subject: [PATCH] Dates - Added support for eom and eocm. - Added more named date unit tests. --- src/Dates.cpp | 54 +++++++++++++++++++++++++++++++++++++++++++++++- test/dates.t.cpp | 15 +++++++++++++- 2 files changed, 67 insertions(+), 2 deletions(-) diff --git a/src/Dates.cpp b/src/Dates.cpp index e13eec087..1343f49ed 100644 --- a/src/Dates.cpp +++ b/src/Dates.cpp @@ -50,6 +50,11 @@ static const char* months_short[] = "jul", "aug", "sep", "oct", "nov", "dec", }; +static int month_days[12] = +{ + 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31, +}; + //////////////////////////////////////////////////////////////////////////////// static bool isMonth (const std::string& name, int& i) { @@ -70,6 +75,29 @@ static bool isDay (const std::string& name, int& i) return false; } +//////////////////////////////////////////////////////////////////////////////// +static bool leapYear (int year) +{ + bool ly = false; + + // (year % 4 == 0) && (year % 100 !=0) OR + // (year % 400 == 0) + // are leapyears + + if (((!(year % 4)) && (year % 100)) || (!(year % 400))) ly = true; + + return ly; +} + +//////////////////////////////////////////////////////////////////////////////// +static int daysInMonth (int year, int month) +{ + if (month == 2 && leapYear (year)) + return 29; + + return month_days[month - 1]; +} + //////////////////////////////////////////////////////////////////////////////// bool namedDates (const std::string& name, Variant& value) { @@ -157,6 +185,30 @@ bool namedDates (const std::string& name, Variant& value) value = Variant (mktime (t), Variant::type_date); } + else if (name == "som") + { + struct tm* t = localtime (&now); + t->tm_hour = t->tm_min = t->tm_sec = 0; + + t->tm_mon++; + if (t->tm_mon == 12) + { + t->tm_year++; + t->tm_mon = 0; + } + + t->tm_mday = 1; + value = Variant (mktime (t), Variant::type_date); + } + + else if (name == "eom" || name == "eocm") + { + struct tm* t = localtime (&now); + t->tm_hour = t->tm_min = t->tm_sec = 0; + t->tm_mday = daysInMonth (t->tm_year + 1900, t->tm_mon + 1); + value = Variant (mktime (t), Variant::type_date); + } + else if (name == "later" || name == "someday") { struct tm* t = localtime (&now); @@ -206,7 +258,7 @@ bool namedDates (const std::string& name, Variant& value) // TODO /* - {s,e}o{w,m,q,ww,cw,cm} + {s,e}o{w,q,ww,cw} midsommar midsommarafton diff --git a/test/dates.t.cpp b/test/dates.t.cpp index 2ecdd48cf..a8275cd22 100644 --- a/test/dates.t.cpp +++ b/test/dates.t.cpp @@ -55,7 +55,7 @@ void testInit (UnitTest& t, const std::string& value, Variant& var) //////////////////////////////////////////////////////////////////////////////// int main (int argc, char** argv) { - UnitTest t (75); + UnitTest t (87); Variant sunday; testInit (t, "sunday", sunday); Variant monday; testInit (t, "monday", monday); @@ -130,7 +130,9 @@ int main (int argc, char** argv) Variant soy; testInit (t, "soy", soy); Variant eoy; testInit (t, "eoy", eoy); Variant socm; testInit (t, "socm", socm); + Variant eocm; testInit (t, "eocm", eocm); Variant som; testInit (t, "som", som); + Variant eom; testInit (t, "eom", eom); Variant later; testInit (t, "later", later); Variant someday; testInit (t, "someday", someday); Variant easter; testInit (t, "easter", easter); @@ -142,6 +144,17 @@ int main (int argc, char** argv) Variant var_true; testInit (t, "true", var_true); Variant var_false; testInit (t, "false", var_false); + t.ok (now >= today, "now >= today"); + t.ok (sod == today, "sod == today"); + t.ok (sod < eod, "sod < eod"); + t.ok (yesterday < today, "yesterday < today"); + t.ok (today < tomorrow, "today < tomorrow"); + t.ok (socm < eocm, "socm < eocm"); + t.ok (now < later, "now < later"); + t.ok (now < someday, "now < someday"); + t.ok (goodfriday < easter, "goodfriday < easter"); + t.ok (easter < eastermonday, "easter < eastermonday"); + return 0; }