Skip to content

Commit c068ec3

Browse files
authored
Allow crossed ranges in holidayList and businessDayList (#2362)
2 parents d823f4e + e1b3051 commit c068ec3

File tree

2 files changed

+9
-10
lines changed

2 files changed

+9
-10
lines changed

ql/time/calendar.cpp

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -277,9 +277,6 @@ namespace QuantLib {
277277
std::vector<Date> Calendar::holidayList(
278278
const Date& from, const Date& to, bool includeWeekEnds) const {
279279

280-
QL_REQUIRE(to>=from, "'from' date ("
281-
<< from << ") must be equal to or earlier than 'to' date ("
282-
<< to << ")");
283280
std::vector<Date> result;
284281
for (Date d = from; d <= to; ++d) {
285282
if (isHoliday(d) && (includeWeekEnds || !isWeekend(d.weekday())))
@@ -291,9 +288,6 @@ namespace QuantLib {
291288
std::vector<Date> Calendar::businessDayList(
292289
const Date& from, const Date& to) const {
293290

294-
QL_REQUIRE(to>=from, "'from' date ("
295-
<< from << ") must be equal to or earlier than 'to' date ("
296-
<< to << ")");
297291
std::vector<Date> result;
298292
for (Date d = from; d <= to; ++d) {
299293
if (isBusinessDay(d))

test-suite/calendars.cpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3831,15 +3831,20 @@ BOOST_AUTO_TEST_CASE(testDayLists) {
38313831
Calendar germany = Germany();
38323832
Date firstDate = Settings::instance().evaluationDate(), endDate = firstDate + 1 * Years;
38333833

3834-
// Test that same day holidayList and businessDayList does not throw an error
3835-
germany.holidayList(firstDate, firstDate, true);
3836-
germany.businessDayList(firstDate, firstDate);
3834+
// Test that a crossed range returns an empty vector
3835+
BOOST_CHECK_EQUAL(germany.holidayList(endDate, firstDate, true).size(), 0);
3836+
BOOST_CHECK_EQUAL(germany.businessDayList(endDate, firstDate).size(), 0);
3837+
// Test that the range is inclusive on both sides
3838+
BOOST_CHECK_EQUAL(germany.holidayList(firstDate, firstDate, true).size(),
3839+
static_cast<size_t>(germany.isHoliday(firstDate)));
3840+
BOOST_CHECK_EQUAL(germany.businessDayList(firstDate, firstDate).size(),
3841+
static_cast<size_t>(germany.isBusinessDay(firstDate)));
38373842

38383843
std::vector<Date> holidays = germany.holidayList(firstDate, endDate, true);
38393844
std::vector<Date> businessDays = germany.businessDayList(firstDate, endDate);
38403845

38413846
auto it_holidays = holidays.begin(), it_businessDays = businessDays.begin();
3842-
for (Date d = firstDate; d < endDate; d++) {
3847+
for (Date d = firstDate; d <= endDate; d++) {
38433848
if (it_holidays != holidays.end() && it_businessDays != businessDays.end() &&
38443849
d == *it_holidays && d == *it_businessDays) {
38453850
BOOST_FAIL("Date " << d << "is both holiday and business day.");

0 commit comments

Comments
 (0)