Skip to content

Commit

Permalink
Add start of month functions
Browse files Browse the repository at this point in the history
  • Loading branch information
igitur committed Jul 24, 2024
1 parent 40bd338 commit 71b79d7
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 0 deletions.
14 changes: 14 additions & 0 deletions ql/time/calendar.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,12 @@ namespace QuantLib {
weekend for the given market.
*/
bool isWeekend(Weekday w) const;
/*! Returns <tt>true</tt> iff in the given market, the date is on
or before the first business day for that month.
*/
bool isStartOfMonth(const Date& d) const;
//! first business day of the month to which the given date belongs
Date startOfMonth(const Date& d) const;
/*! Returns <tt>true</tt> iff in the given market, the date is on
or after the last business day for that month.
*/
Expand Down Expand Up @@ -240,6 +246,14 @@ namespace QuantLib {
return impl_->isBusinessDay(_d);
}

inline bool Calendar::isStartOfMonth(const Date& d) const {
return (d.month() != adjust(d-1, Preceding).month());
}

inline Date Calendar::startOfMonth(const Date& d) const {
return adjust(Date::startOfMonth(d), Following);
}

inline bool Calendar::isEndOfMonth(const Date& d) const {
return (d.month() != adjust(d+1).month());
}
Expand Down
14 changes: 14 additions & 0 deletions ql/time/date.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,10 @@ namespace QuantLib {
static Date maxDate();
//! whether the given year is a leap one
static bool isLeap(Year y);
//! first day of the month to which the given date belongs
static Date startOfMonth(const Date& d);
//! whether a date is the first day of its month
static bool isStartOfMonth(const Date& d);
//! last day of the month to which the given date belongs
static Date endOfMonth(const Date& d);
//! whether a date is the last day of its month
Expand Down Expand Up @@ -425,6 +429,16 @@ namespace QuantLib {
return advance(*this,-p.length(),p.units());
}

inline Date Date::startOfMonth(const Date& d) {
Month m = d.month();
Year y = d.year();
return Date(1, m, y);
}

inline bool Date::isStartOfMonth(const Date& d) {
return (d.dayOfMonth() == 1);
}

inline Date Date::endOfMonth(const Date& d) {
Month m = d.month();
Year y = d.year();
Expand Down
20 changes: 20 additions & 0 deletions test-suite/calendars.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3369,6 +3369,26 @@ BOOST_AUTO_TEST_CASE(testMexicoInaugurationDay) {
}
}

BOOST_AUTO_TEST_CASE(testStartOfMonth) {
BOOST_TEST_MESSAGE("Testing start-of-month calculation...");

Calendar c = TARGET(); // any calendar would be OK

Date som, counter = Date::minDate() + 2 * Months;
Date last = Date::maxDate();

while (counter < last) {
som = c.startOfMonth(counter);
// check that som is som
if (!c.isStartOfMonth(som))
BOOST_FAIL("\n " << som.weekday() << " " << som << " is not the first business day in "
<< som.month() << " " << som.year() << " according to " << c.name());
// check that som is in the same month as counter
if (som.month() != counter.month())
BOOST_FAIL("\n " << som << " is not in the same month as " << counter);
counter = counter + 1;
}
}

BOOST_AUTO_TEST_CASE(testEndOfMonth) {
BOOST_TEST_MESSAGE("Testing end-of-month calculation...");
Expand Down

0 comments on commit 71b79d7

Please sign in to comment.