| 1 | /* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
| 2 | |
| 3 | /* |
| 4 | Copyright (C) 2000, 2001, 2002, 2003 RiskMap srl |
| 5 | Copyright (C) 2003, 2004, 2005, 2006, 2007 StatPro Italia srl |
| 6 | Copyright (C) 2006 Piter Dias |
| 7 | Copyright (C) 2020 Leonardo Arcari |
| 8 | Copyright (C) 2020 Kline s.r.l. |
| 9 | |
| 10 | This file is part of QuantLib, a free-software/open-source library |
| 11 | for financial quantitative analysts and developers - http://quantlib.org/ |
| 12 | |
| 13 | QuantLib is free software: you can redistribute it and/or modify it |
| 14 | under the terms of the QuantLib license. You should have received a |
| 15 | copy of the license along with this program; if not, please email |
| 16 | <quantlib-dev@lists.sf.net>. The license is also available online at |
| 17 | <http://quantlib.org/license.shtml>. |
| 18 | |
| 19 | This program is distributed in the hope that it will be useful, but WITHOUT |
| 20 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 21 | FOR A PARTICULAR PURPOSE. See the license for more details. |
| 22 | */ |
| 23 | |
| 24 | /*! \file calendar.hpp |
| 25 | \brief %calendar class |
| 26 | */ |
| 27 | |
| 28 | #ifndef quantlib_calendar_hpp |
| 29 | #define quantlib_calendar_hpp |
| 30 | |
| 31 | #include <ql/errors.hpp> |
| 32 | #include <ql/time/date.hpp> |
| 33 | #include <ql/time/businessdayconvention.hpp> |
| 34 | #include <ql/shared_ptr.hpp> |
| 35 | #include <set> |
| 36 | #include <vector> |
| 37 | #include <string> |
| 38 | |
| 39 | namespace QuantLib { |
| 40 | |
| 41 | class Period; |
| 42 | |
| 43 | //! %calendar class |
| 44 | /*! This class provides methods for determining whether a date is a |
| 45 | business day or a holiday for a given market, and for |
| 46 | incrementing/decrementing a date of a given number of business days. |
| 47 | |
| 48 | The Bridge pattern is used to provide the base behavior of the |
| 49 | calendar, namely, to determine whether a date is a business day. |
| 50 | |
| 51 | A calendar should be defined for specific exchange holiday schedule |
| 52 | or for general country holiday schedule. Legacy city holiday schedule |
| 53 | calendars will be moved to the exchange/country convention. |
| 54 | |
| 55 | \ingroup datetime |
| 56 | |
| 57 | \test the methods for adding and removing holidays are tested |
| 58 | by inspecting the calendar before and after their |
| 59 | invocation. |
| 60 | */ |
| 61 | class Calendar { |
| 62 | protected: |
| 63 | //! abstract base class for calendar implementations |
| 64 | class Impl { |
| 65 | public: |
| 66 | virtual ~Impl() = default; |
| 67 | virtual std::string name() const = 0; |
| 68 | virtual bool isBusinessDay(const Date&) const = 0; |
| 69 | virtual bool isWeekend(Weekday) const = 0; |
| 70 | std::set<Date> addedHolidays, removedHolidays; |
| 71 | }; |
| 72 | ext::shared_ptr<Impl> impl_; |
| 73 | public: |
| 74 | /*! The default constructor returns a calendar with a null |
| 75 | implementation, which is therefore unusable except as a |
| 76 | placeholder. |
| 77 | */ |
| 78 | Calendar() = default; |
| 79 | //! \name Calendar interface |
| 80 | //@{ |
| 81 | //! Returns whether or not the calendar is initialized |
| 82 | bool empty() const; |
| 83 | //! Returns the name of the calendar. |
| 84 | /*! \warning This method is used for output and comparison between |
| 85 | calendars. It is <b>not</b> meant to be used for writing |
| 86 | switch-on-type code. |
| 87 | */ |
| 88 | std::string name() const; |
| 89 | /*! Returns <tt>true</tt> iff the date is a business day for the |
| 90 | given market. |
| 91 | */ |
| 92 | |
| 93 | /*! Returns the set of added holidays for the given calendar */ |
| 94 | const std::set<Date>& addedHolidays() const; |
| 95 | |
| 96 | /*! Returns the set of removed holidays for the given calendar */ |
| 97 | const std::set<Date>& removedHolidays() const; |
| 98 | |
| 99 | /*! Clear the set of added and removed holidays */ |
| 100 | void resetAddedAndRemovedHolidays(); |
| 101 | |
| 102 | bool isBusinessDay(const Date& d) const; |
| 103 | /*! Returns <tt>true</tt> iff the date is a holiday for the given |
| 104 | market. |
| 105 | */ |
| 106 | bool isHoliday(const Date& d) const; |
| 107 | /*! Returns <tt>true</tt> iff the weekday is part of the |
| 108 | weekend for the given market. |
| 109 | */ |
| 110 | bool isWeekend(Weekday w) const; |
| 111 | /*! Returns <tt>true</tt> iff in the given market, the date is on |
| 112 | or after the last business day for that month. |
| 113 | */ |
| 114 | bool isEndOfMonth(const Date& d) const; |
| 115 | //! last business day of the month to which the given date belongs |
| 116 | Date endOfMonth(const Date& d) const; |
| 117 | |
| 118 | /*! Adds a date to the set of holidays for the given calendar. */ |
| 119 | void addHoliday(const Date&); |
| 120 | /*! Removes a date from the set of holidays for the given calendar. */ |
| 121 | void removeHoliday(const Date&); |
| 122 | |
| 123 | /*! Returns the holidays between two dates. */ |
| 124 | std::vector<Date> holidayList(const Date& from, |
| 125 | const Date& to, |
| 126 | bool includeWeekEnds = false) const; |
| 127 | /*! Returns the business days between two dates. */ |
| 128 | std::vector<Date> businessDayList(const Date& from, |
| 129 | const Date& to) const; |
| 130 | |
| 131 | /*! Adjusts a non-business day to the appropriate near business day |
| 132 | with respect to the given convention. |
| 133 | */ |
| 134 | Date adjust(const Date&, |
| 135 | BusinessDayConvention convention = Following) const; |
| 136 | /*! Advances the given date of the given number of business days and |
| 137 | returns the result. |
| 138 | \note The input date is not modified. |
| 139 | */ |
| 140 | Date advance(const Date&, |
| 141 | Integer n, |
| 142 | TimeUnit unit, |
| 143 | BusinessDayConvention convention = Following, |
| 144 | bool endOfMonth = false) const; |
| 145 | /*! Advances the given date as specified by the given period and |
| 146 | returns the result. |
| 147 | \note The input date is not modified. |
| 148 | */ |
| 149 | Date advance(const Date& date, |
| 150 | const Period& period, |
| 151 | BusinessDayConvention convention = Following, |
| 152 | bool endOfMonth = false) const; |
| 153 | /*! Calculates the number of business days between two given |
| 154 | dates and returns the result. |
| 155 | */ |
| 156 | Date::serial_type businessDaysBetween(const Date& from, |
| 157 | const Date& to, |
| 158 | bool includeFirst = true, |
| 159 | bool includeLast = false) const; |
| 160 | //@} |
| 161 | |
| 162 | protected: |
| 163 | //! partial calendar implementation |
| 164 | /*! This class provides the means of determining the Easter |
| 165 | Monday for a given year, as well as specifying Saturdays |
| 166 | and Sundays as weekend days. |
| 167 | */ |
| 168 | class WesternImpl : public Impl { |
| 169 | public: |
| 170 | bool isWeekend(Weekday) const override; |
| 171 | //! expressed relative to first day of year |
| 172 | static Day easterMonday(Year); |
| 173 | }; |
| 174 | //! partial calendar implementation |
| 175 | /*! This class provides the means of determining the Orthodox |
| 176 | Easter Monday for a given year, as well as specifying |
| 177 | Saturdays and Sundays as weekend days. |
| 178 | */ |
| 179 | class OrthodoxImpl : public Impl { |
| 180 | public: |
| 181 | bool isWeekend(Weekday) const override; |
| 182 | //! expressed relative to first day of year |
| 183 | static Day easterMonday(Year); |
| 184 | }; |
| 185 | }; |
| 186 | |
| 187 | /*! Returns <tt>true</tt> iff the two calendars belong to the same |
| 188 | derived class. |
| 189 | \relates Calendar |
| 190 | */ |
| 191 | bool operator==(const Calendar&, const Calendar&); |
| 192 | |
| 193 | /*! \relates Calendar */ |
| 194 | bool operator!=(const Calendar&, const Calendar&); |
| 195 | |
| 196 | /*! \relates Calendar */ |
| 197 | std::ostream& operator<<(std::ostream&, const Calendar&); |
| 198 | |
| 199 | |
| 200 | // inline definitions |
| 201 | |
| 202 | inline bool Calendar::empty() const { |
| 203 | return !impl_; |
| 204 | } |
| 205 | |
| 206 | inline std::string Calendar::name() const { |
| 207 | QL_REQUIRE(impl_, "no calendar implementation provided" ); |
| 208 | return impl_->name(); |
| 209 | } |
| 210 | |
| 211 | inline const std::set<Date>& Calendar::addedHolidays() const { |
| 212 | QL_REQUIRE(impl_, "no calendar implementation provided" ); |
| 213 | |
| 214 | return impl_->addedHolidays; |
| 215 | } |
| 216 | |
| 217 | inline const std::set<Date>& Calendar::removedHolidays() const { |
| 218 | QL_REQUIRE(impl_, "no calendar implementation provided" ); |
| 219 | |
| 220 | return impl_->removedHolidays; |
| 221 | } |
| 222 | |
| 223 | inline bool Calendar::isBusinessDay(const Date& d) const { |
| 224 | QL_REQUIRE(impl_, "no calendar implementation provided" ); |
| 225 | |
| 226 | #ifdef QL_HIGH_RESOLUTION_DATE |
| 227 | const Date _d(d.dayOfMonth(), d.month(), d.year()); |
| 228 | #else |
| 229 | const Date& _d = d; |
| 230 | #endif |
| 231 | |
| 232 | if (!impl_->addedHolidays.empty() && |
| 233 | impl_->addedHolidays.find(x: _d) != impl_->addedHolidays.end()) |
| 234 | return false; |
| 235 | |
| 236 | if (!impl_->removedHolidays.empty() && |
| 237 | impl_->removedHolidays.find(x: _d) != impl_->removedHolidays.end()) |
| 238 | return true; |
| 239 | |
| 240 | return impl_->isBusinessDay(_d); |
| 241 | } |
| 242 | |
| 243 | inline bool Calendar::isEndOfMonth(const Date& d) const { |
| 244 | return (d.month() != adjust(d+1).month()); |
| 245 | } |
| 246 | |
| 247 | inline Date Calendar::endOfMonth(const Date& d) const { |
| 248 | return adjust(Date::endOfMonth(d), convention: Preceding); |
| 249 | } |
| 250 | |
| 251 | inline bool Calendar::isHoliday(const Date& d) const { |
| 252 | return !isBusinessDay(d); |
| 253 | } |
| 254 | |
| 255 | inline bool Calendar::isWeekend(Weekday w) const { |
| 256 | QL_REQUIRE(impl_, "no calendar implementation provided" ); |
| 257 | return impl_->isWeekend(w); |
| 258 | } |
| 259 | |
| 260 | inline bool operator==(const Calendar& c1, const Calendar& c2) { |
| 261 | return (c1.empty() && c2.empty()) |
| 262 | || (!c1.empty() && !c2.empty() && c1.name() == c2.name()); |
| 263 | } |
| 264 | |
| 265 | inline bool operator!=(const Calendar& c1, const Calendar& c2) { |
| 266 | return !(c1 == c2); |
| 267 | } |
| 268 | |
| 269 | inline std::ostream& operator<<(std::ostream& out, const Calendar &c) { |
| 270 | return out << c.name(); |
| 271 | } |
| 272 | |
| 273 | } |
| 274 | |
| 275 | #endif |
| 276 | |