diff --git a/src/Date.cpp b/src/Date.cpp index f81aeb8f1..829cab59a 100644 --- a/src/Date.cpp +++ b/src/Date.cpp @@ -585,6 +585,12 @@ int Date::month () const return t->tm_mon + 1; } +//////////////////////////////////////////////////////////////////////////////// +int Date::week () const +{ + return Date::weekOfYear (Date::dayOfWeek (context.config.get ("weekstart"))); +} + //////////////////////////////////////////////////////////////////////////////// int Date::day () const { @@ -679,6 +685,16 @@ bool Date::sameDay (const Date& rhs) const return false; } +//////////////////////////////////////////////////////////////////////////////// +bool Date::sameWeek (const Date& rhs) const +{ + if (this->year () == rhs.year () && + this->week () == rhs.week ()) + return true; + + return false; +} + //////////////////////////////////////////////////////////////////////////////// bool Date::sameMonth (const Date& rhs) const { diff --git a/src/Date.h b/src/Date.h index dfd28186c..cb4c1dd68 100644 --- a/src/Date.h +++ b/src/Date.h @@ -76,6 +76,7 @@ public: static int length (const std::string&); int month () const; + int week () const; int day () const; int year () const; int weekOfYear (int) const; @@ -93,6 +94,7 @@ public: bool operator>= (const Date&) const; bool sameHour (const Date&) const; bool sameDay (const Date&) const; + bool sameWeek (const Date&) const; bool sameMonth (const Date&) const; bool sameYear (const Date&) const; diff --git a/test/date.t.cpp b/test/date.t.cpp index 962e7debe..cadeb0628 100644 --- a/test/date.t.cpp +++ b/test/date.t.cpp @@ -36,7 +36,7 @@ Context context; //////////////////////////////////////////////////////////////////////////////// int main (int argc, char** argv) { - UnitTest t (205); + UnitTest t (209); try { @@ -61,9 +61,11 @@ int main (int argc, char** argv) t.ok (now < tomorrow, "now < tomorrow"); // Date::Date ("now") + context.config.set ("weekstart", "monday"); Date relative_now ("now"); t.ok (relative_now.sameHour (now), "Date ().sameHour (Date (now))"); t.ok (relative_now.sameDay (now), "Date ().sameDay (Date (now))"); + t.ok (relative_now.sameWeek (now), "Date ().sameWeek (Date (now))"); t.ok (relative_now.sameMonth (now), "Date ().sameMonth (Date (now))"); t.ok (relative_now.sameYear (now), "Date ().sameYear (Date (now))"); @@ -71,6 +73,7 @@ int main (int argc, char** argv) Date left ("7/4/2008"); Date comp1 ("7/4/2008"); t.ok (left.sameDay (comp1), "7/4/2008 is on the same day as 7/4/2008"); + t.ok (left.sameWeek (comp1), "7/4/2008 is on the same week as 7/4/2008"); t.ok (left.sameMonth (comp1), "7/4/2008 is in the same month as 7/4/2008"); t.ok (left.sameYear (comp1), "7/4/2008 is in the same year as 7/4/2008"); @@ -81,11 +84,13 @@ int main (int argc, char** argv) Date comp3 ("8/4/2008"); t.notok (left.sameDay (comp3), "7/4/2008 is not on the same day as 8/4/2008"); + t.notok (left.sameWeek (comp3), "7/4/2008 is not on the same week as 8/4/2008"); t.notok (left.sameMonth (comp3), "7/4/2008 is not in the same month as 8/4/2008"); t.ok (left.sameYear (comp3), "7/4/2008 is in the same year as 8/4/2008"); Date comp4 ("7/4/2009"); t.notok (left.sameDay (comp4), "7/4/2008 is not on the same day as 7/4/2009"); + t.notok (left.sameWeek (comp3), "7/4/2008 is not on the same week as 7/4/2009"); t.notok (left.sameMonth (comp4), "7/4/2008 is not in the same month as 7/4/2009"); t.notok (left.sameYear (comp4), "7/4/2008 is not in the same year as 7/4/2009");