QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.38
Loading...
Searching...
No Matches
calendar.hpp
Go to the documentation of this file.
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>
34#include <ql/shared_ptr.hpp>
35#include <set>
36#include <vector>
37#include <string>
38
39namespace 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;
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 */
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 before the first business day for that month.
113 */
114 bool isStartOfMonth(const Date& d) const;
115 //! first business day of the month to which the given date belongs
116 Date startOfMonth(const Date& d) const;
117 /*! Returns <tt>true</tt> iff in the given market, the date is on
118 or after the last business day for that month.
119 */
120 bool isEndOfMonth(const Date& d) const;
121 //! last business day of the month to which the given date belongs
122 Date endOfMonth(const Date& d) const;
123
124 /*! Adds a date to the set of holidays for the given calendar. */
125 void addHoliday(const Date&);
126 /*! Removes a date from the set of holidays for the given calendar. */
127 void removeHoliday(const Date&);
128
129 /*! Returns the holidays between two dates. */
130 std::vector<Date> holidayList(const Date& from,
131 const Date& to,
132 bool includeWeekEnds = false) const;
133 /*! Returns the business days between two dates. */
134 std::vector<Date> businessDayList(const Date& from,
135 const Date& to) const;
136
137 /*! Adjusts a non-business day to the appropriate near business day
138 with respect to the given convention.
139 */
140 Date adjust(const Date&,
141 BusinessDayConvention convention = Following) const;
142 /*! Advances the given date of the given number of business days and
143 returns the result.
144 \note The input date is not modified.
145 */
146 Date advance(const Date&,
147 Integer n,
148 TimeUnit unit,
149 BusinessDayConvention convention = Following,
150 bool endOfMonth = false) const;
151 /*! Advances the given date as specified by the given period and
152 returns the result.
153 \note The input date is not modified.
154 */
155 Date advance(const Date& date,
156 const Period& period,
157 BusinessDayConvention convention = Following,
158 bool endOfMonth = false) const;
159 /*! Calculates the number of business days between two given
160 dates and returns the result.
161 */
163 const Date& to,
164 bool includeFirst = true,
165 bool includeLast = false) const;
166 //@}
167
168 protected:
169 //! partial calendar implementation
170 /*! This class provides the means of determining the Easter
171 Monday for a given year, as well as specifying Saturdays
172 and Sundays as weekend days.
173 */
174 class WesternImpl : public Impl {
175 public:
176 bool isWeekend(Weekday) const override;
177 //! expressed relative to first day of year
178 static Day easterMonday(Year);
179 };
180 //! partial calendar implementation
181 /*! This class provides the means of determining the Orthodox
182 Easter Monday for a given year, as well as specifying
183 Saturdays and Sundays as weekend days.
184 */
185 class OrthodoxImpl : public Impl {
186 public:
187 bool isWeekend(Weekday) const override;
188 //! expressed relative to first day of year
189 static Day easterMonday(Year);
190 };
191 };
192
193 /*! Returns <tt>true</tt> iff the two calendars belong to the same
194 derived class.
195 \relates Calendar
196 */
197 bool operator==(const Calendar&, const Calendar&);
198
199 /*! \relates Calendar */
200 bool operator!=(const Calendar&, const Calendar&);
201
202 /*! \relates Calendar */
203 std::ostream& operator<<(std::ostream&, const Calendar&);
204
205
206 // inline definitions
207
208 inline bool Calendar::empty() const {
209 return !impl_;
210 }
211
212 inline std::string Calendar::name() const {
213 QL_REQUIRE(impl_, "no calendar implementation provided");
214 return impl_->name();
215 }
216
217 inline const std::set<Date>& Calendar::addedHolidays() const {
218 QL_REQUIRE(impl_, "no calendar implementation provided");
219
220 return impl_->addedHolidays;
221 }
222
223 inline const std::set<Date>& Calendar::removedHolidays() const {
224 QL_REQUIRE(impl_, "no calendar implementation provided");
225
226 return impl_->removedHolidays;
227 }
228
229 inline bool Calendar::isBusinessDay(const Date& d) const {
230 QL_REQUIRE(impl_, "no calendar implementation provided");
231
232#ifdef QL_HIGH_RESOLUTION_DATE
233 const Date _d(d.dayOfMonth(), d.month(), d.year());
234#else
235 const Date& _d = d;
236#endif
237
238 if (!impl_->addedHolidays.empty() &&
239 impl_->addedHolidays.find(_d) != impl_->addedHolidays.end())
240 return false;
241
242 if (!impl_->removedHolidays.empty() &&
243 impl_->removedHolidays.find(_d) != impl_->removedHolidays.end())
244 return true;
245
246 return impl_->isBusinessDay(_d);
247 }
248
249 inline bool Calendar::isStartOfMonth(const Date& d) const {
250 return d <= startOfMonth(d);
251 }
252
253 inline Date Calendar::startOfMonth(const Date& d) const {
255 }
256
257 inline bool Calendar::isEndOfMonth(const Date& d) const {
258 return d >= endOfMonth(d);
259 }
260
261 inline Date Calendar::endOfMonth(const Date& d) const {
263 }
264
265 inline bool Calendar::isHoliday(const Date& d) const {
266 return !isBusinessDay(d);
267 }
268
269 inline bool Calendar::isWeekend(Weekday w) const {
270 QL_REQUIRE(impl_, "no calendar implementation provided");
271 return impl_->isWeekend(w);
272 }
273
274 inline bool operator==(const Calendar& c1, const Calendar& c2) {
275 return (c1.empty() && c2.empty())
276 || (!c1.empty() && !c2.empty() && c1.name() == c2.name());
277 }
278
279 inline bool operator!=(const Calendar& c1, const Calendar& c2) {
280 return !(c1 == c2);
281 }
282
283 inline std::ostream& operator<<(std::ostream& out, const Calendar &c) {
284 return out << c.name();
285 }
286
287}
288
289#endif
BusinessDayConvention enumeration.
abstract base class for calendar implementations
Definition: calendar.hpp:64
virtual bool isBusinessDay(const Date &) const =0
std::set< Date > addedHolidays
Definition: calendar.hpp:70
virtual ~Impl()=default
virtual bool isWeekend(Weekday) const =0
virtual std::string name() const =0
std::set< Date > removedHolidays
Definition: calendar.hpp:70
partial calendar implementation
Definition: calendar.hpp:185
static Day easterMonday(Year)
expressed relative to first day of year
Definition: calendar.cpp:241
bool isWeekend(Weekday) const override
Definition: calendar.cpp:237
partial calendar implementation
Definition: calendar.hpp:174
static Day easterMonday(Year)
expressed relative to first day of year
Definition: calendar.cpp:199
bool isWeekend(Weekday) const override
Definition: calendar.cpp:195
calendar class
Definition: calendar.hpp:61
const std::set< Date > & removedHolidays() const
Definition: calendar.hpp:223
bool isWeekend(Weekday w) const
Definition: calendar.hpp:269
bool isStartOfMonth(const Date &d) const
Definition: calendar.hpp:249
std::string name() const
Returns the name of the calendar.
Definition: calendar.hpp:212
Date::serial_type businessDaysBetween(const Date &from, const Date &to, bool includeFirst=true, bool includeLast=false) const
Definition: calendar.cpp:182
void removeHoliday(const Date &)
Definition: calendar.cpp:62
bool isEndOfMonth(const Date &d) const
Definition: calendar.hpp:257
bool empty() const
Returns whether or not the calendar is initialized.
Definition: calendar.hpp:208
bool isBusinessDay(const Date &d) const
Definition: calendar.hpp:229
void addHoliday(const Date &)
Definition: calendar.cpp:45
Date adjust(const Date &, BusinessDayConvention convention=Following) const
Definition: calendar.cpp:84
Date startOfMonth(const Date &d) const
first business day of the month to which the given date belongs
Definition: calendar.hpp:253
std::vector< Date > holidayList(const Date &from, const Date &to, bool includeWeekEnds=false) const
Definition: calendar.cpp:277
bool isHoliday(const Date &d) const
Definition: calendar.hpp:265
Date advance(const Date &, Integer n, TimeUnit unit, BusinessDayConvention convention=Following, bool endOfMonth=false) const
Definition: calendar.cpp:130
ext::shared_ptr< Impl > impl_
Definition: calendar.hpp:72
void resetAddedAndRemovedHolidays()
Definition: calendar.cpp:79
const std::set< Date > & addedHolidays() const
Definition: calendar.hpp:217
std::vector< Date > businessDayList(const Date &from, const Date &to) const
Definition: calendar.cpp:291
Date endOfMonth(const Date &d) const
last business day of the month to which the given date belongs
Definition: calendar.hpp:261
Concrete date class.
Definition: date.hpp:125
static Date endOfMonth(const Date &d)
last day of the month to which the given date belongs
Definition: date.hpp:424
std::int_fast32_t serial_type
serial number type
Definition: date.hpp:128
static Date startOfMonth(const Date &d)
first day of the month to which the given date belongs
Definition: date.hpp:380
date- and time-related classes, typedefs and enumerations
Classes and functions for error handling.
#define QL_REQUIRE(condition, message)
throw an error if the given pre-condition is not verified
Definition: errors.hpp:117
Date d
Integer Year
Year number.
Definition: date.hpp:87
Integer Day
Day number.
Definition: date.hpp:53
TimeUnit
Units used to describe time periods.
Definition: timeunit.hpp:37
BusinessDayConvention
Business Day conventions.
QL_INTEGER Integer
integer number
Definition: types.hpp:35
Definition: any.hpp:37
bool operator==(const Currency &c1, const Currency &c2)
Definition: currency.hpp:179
std::ostream & operator<<(std::ostream &out, GFunctionFactory::YieldCurveModel type)
bool operator!=(const Currency &c1, const Currency &c2)
Definition: currency.hpp:184
Maps shared_ptr to either the boost or std implementation.