QuantLib: a free/open-source library for quantitative finance
Fully annotated sources - version 1.32
Loading...
Searching...
No Matches
calendar.hpp
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
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
39namespace QuantLib {
40
41 class Period;
42
44
61 class Calendar {
62 protected:
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:
78 Calendar() = default;
80
81
82 bool empty() const;
84
88 std::string name() const;
94 const std::set<Date>& addedHolidays() const;
95
97 const std::set<Date>& removedHolidays() const;
98
101
102 bool isBusinessDay(const Date& d) const;
106 bool isHoliday(const Date& d) const;
110 bool isWeekend(Weekday w) const;
114 bool isEndOfMonth(const Date& d) const;
116 Date endOfMonth(const Date& d) const;
117
119 void addHoliday(const Date&);
121 void removeHoliday(const Date&);
122
124 std::vector<Date> holidayList(const Date& from,
125 const Date& to,
126 bool includeWeekEnds = false) const;
128 std::vector<Date> businessDayList(const Date& from,
129 const Date& to) const;
130
134 Date adjust(const Date&,
135 BusinessDayConvention convention = Following) const;
140 Date advance(const Date&,
141 Integer n,
142 TimeUnit unit,
143 BusinessDayConvention convention = Following,
144 bool endOfMonth = false) const;
149 Date advance(const Date& date,
150 const Period& period,
151 BusinessDayConvention convention = Following,
152 bool endOfMonth = false) const;
157 const Date& to,
158 bool includeFirst = true,
159 bool includeLast = false) const;
161
162 protected:
164
168 class WesternImpl : public Impl {
169 public:
170 bool isWeekend(Weekday) const override;
172 static Day easterMonday(Year);
173 };
175
179 class OrthodoxImpl : public Impl {
180 public:
181 bool isWeekend(Weekday) const override;
183 static Day easterMonday(Year);
184 };
185 };
186
191 bool operator==(const Calendar&, const Calendar&);
192
194 bool operator!=(const Calendar&, const Calendar&);
195
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(_d) != impl_->addedHolidays.end())
234 return false;
235
236 if (!impl_->removedHolidays.empty() &&
237 impl_->removedHolidays.find(_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 {
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
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:179
static Day easterMonday(Year)
expressed relative to first day of year
Definition: calendar.cpp:235
bool isWeekend(Weekday) const override
Definition: calendar.cpp:231
partial calendar implementation
Definition: calendar.hpp:168
static Day easterMonday(Year)
expressed relative to first day of year
Definition: calendar.cpp:193
bool isWeekend(Weekday) const override
Definition: calendar.cpp:189
calendar class
Definition: calendar.hpp:61
const std::set< Date > & removedHolidays() const
Definition: calendar.hpp:217
bool isWeekend(Weekday w) const
Definition: calendar.hpp:255
std::string name() const
Returns the name of the calendar.
Definition: calendar.hpp:206
Date::serial_type businessDaysBetween(const Date &from, const Date &to, bool includeFirst=true, bool includeLast=false) const
Definition: calendar.cpp:176
void removeHoliday(const Date &)
Definition: calendar.cpp:62
bool isEndOfMonth(const Date &d) const
Definition: calendar.hpp:243
bool empty() const
Returns whether or not the calendar is initialized.
Definition: calendar.hpp:202
bool isBusinessDay(const Date &d) const
Definition: calendar.hpp:223
void addHoliday(const Date &)
Definition: calendar.cpp:45
Date adjust(const Date &, BusinessDayConvention convention=Following) const
Definition: calendar.cpp:84
std::vector< Date > holidayList(const Date &from, const Date &to, bool includeWeekEnds=false) const
Definition: calendar.cpp:271
bool isHoliday(const Date &d) const
Definition: calendar.hpp:251
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:211
std::vector< Date > businessDayList(const Date &from, const Date &to) const
Definition: calendar.cpp:285
Date endOfMonth(const Date &d) const
last business day of the month to which the given date belongs
Definition: calendar.hpp:247
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:428
std::int_fast32_t serial_type
serial number type
Definition: date.hpp:128
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:35
bool operator==(const Currency &c1, const Currency &c2)
Definition: currency.hpp:191
std::ostream & operator<<(std::ostream &out, GFunctionFactory::YieldCurveModel type)
bool operator!=(const Currency &c1, const Currency &c2)
Definition: currency.hpp:196