- Added support for eom and eocm.
- Added more named date unit tests.
This commit is contained in:
Paul Beckingham 2014-01-06 23:42:02 -05:00
parent 55cc02bbc1
commit ab0d929a3c
2 changed files with 67 additions and 2 deletions

View file

@ -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

View file

@ -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;
}