QuantLib: a free/open-source library for quantitative finance
Fully annotated sources - version 1.32
Loading...
Searching...
No Matches
date.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 StatPro Italia srl
6 Copyright (C) 2004, 2005, 2006 Ferdinando Ametrano
7 Copyright (C) 2006 Katiuscia Manzoni
8 Copyright (C) 2006 Toyin Akin
9 Copyright (C) 2015 Klaus Spanderen
10 Copyright (C) 2020 Leonardo Arcari
11 Copyright (C) 2020 Kline s.r.l.
12
13 This file is part of QuantLib, a free-software/open-source library
14 for financial quantitative analysts and developers - http://quantlib.org/
15
16 QuantLib is free software: you can redistribute it and/or modify it
17 under the terms of the QuantLib license. You should have received a
18 copy of the license along with this program; if not, please email
19 <quantlib-dev@lists.sf.net>. The license is also available online at
20 <http://quantlib.org/license.shtml>.
21
22 This program is distributed in the hope that it will be useful, but WITHOUT
23 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
24 FOR A PARTICULAR PURPOSE. See the license for more details.
25*/
26
31#ifndef quantlib_date_hpp
32#define quantlib_date_hpp
33
34#include <ql/time/period.hpp>
35#include <ql/time/weekday.hpp>
36#include <ql/utilities/null.hpp>
37
38#ifdef QL_HIGH_RESOLUTION_DATE
39#include <boost/date_time/posix_time/ptime.hpp>
40#include <boost/date_time/posix_time/posix_time_duration.hpp>
41#endif
42
43#include <cstdint>
44#include <utility>
45#include <functional>
46#include <string>
47
48
49namespace QuantLib {
50
52
53 typedef Integer Day;
54
56
57 enum Month { January = 1,
59 March = 3,
60 April = 4,
61 May = 5,
62 June = 6,
63 July = 7,
64 August = 8,
66 October = 10,
69 Jan = 1,
70 Feb = 2,
71 Mar = 3,
72 Apr = 4,
73 Jun = 6,
74 Jul = 7,
75 Aug = 8,
76 Sep = 9,
77 Oct = 10,
78 Nov = 11,
79 Dec = 12
80 };
81
83 std::ostream& operator<<(std::ostream&, Month);
84
86
87 typedef Integer Year;
88
89#ifdef QL_HIGH_RESOLUTION_DATE
91
92 typedef boost::posix_time::hours::hour_type Hour;
93
95
96 typedef boost::posix_time::minutes::min_type Minute;
97
99
100 typedef boost::posix_time::minutes::sec_type Second;
101
103
104 typedef boost::posix_time::time_duration::fractional_seconds_type
105 Millisecond;
106
108
109 typedef boost::posix_time::time_duration::fractional_seconds_type
110 Microsecond;
111#endif
112
114
125 class Date {
126 public:
128 typedef std::int_fast32_t serial_type;
130
131
132 Date();
136 Date(Day d, Month m, Year y);
137
138#ifdef QL_HIGH_RESOLUTION_DATE
140 explicit Date(const boost::posix_time::ptime& localTime);
142 Date(Day d, Month m, Year y,
143 Hour hours, Minute minutes, Second seconds,
144 Millisecond millisec = 0, Microsecond microsec = 0);
145#endif
147
149
150 Weekday weekday() const;
151 Day dayOfMonth() const;
153 Day dayOfYear() const;
154 Month month() const;
155 Year year() const;
157
158#ifdef QL_HIGH_RESOLUTION_DATE
159 Hour hours() const;
160 Minute minutes() const;
161 Second seconds() const;
162 Millisecond milliseconds() const;
163 Microsecond microseconds() const;
164
165 Time fractionOfDay() const;
166 Time fractionOfSecond() const;
167
168 const boost::posix_time::ptime& dateTime() const;
169#endif
171
173
174
177 Date& operator+=(const Period&);
181 Date& operator-=(const Period&);
183 Date& operator++();
185 Date operator++(int );
187 Date& operator--();
189 Date operator--(int );
193 Date operator+(const Period&) const;
197 Date operator-(const Period&) const;
199
201
202
203 static Date todaysDate();
205 static Date minDate();
207 static Date maxDate();
209 static bool isLeap(Year y);
211 static Date endOfMonth(const Date& d);
213 static bool isEndOfMonth(const Date& d);
215
220 static Date nextWeekday(const Date& d,
221 Weekday w);
223
228 static Date nthWeekday(Size n,
229 Weekday w,
230 Month m,
231 Year y);
232
233#ifdef QL_HIGH_RESOLUTION_DATE
235 static Date localDateTime();
237 static Date universalDateTime();
238
240 static boost::posix_time::time_duration::tick_type ticksPerSecond();
241#endif
242
244
245 private:
249
250#ifdef QL_HIGH_RESOLUTION_DATE
251 boost::posix_time::ptime dateTime_;
252#else
254 static Date advance(const Date& d, Integer units, TimeUnit);
255 static Integer monthLength(Month m, bool leapYear);
256 static Integer monthOffset(Month m, bool leapYear);
258#endif
259 };
260
264 Date::serial_type operator-(const Date&, const Date&);
268 Time daysBetween(const Date&, const Date&);
269
271 bool operator==(const Date&, const Date&);
273 bool operator!=(const Date&, const Date&);
275 bool operator<(const Date&, const Date&);
277 bool operator<=(const Date&, const Date&);
279 bool operator>(const Date&, const Date&);
281 bool operator>=(const Date&, const Date&);
282
304 std::size_t hash_value(const Date& d);
305
307 std::ostream& operator<<(std::ostream&, const Date&);
308
309 namespace detail {
310
312 explicit short_date_holder(const Date d) : d(d) {}
314 };
315 std::ostream& operator<<(std::ostream&, const short_date_holder&);
316
318 explicit long_date_holder(const Date& d) : d(d) {}
320 };
321 std::ostream& operator<<(std::ostream&, const long_date_holder&);
322
324 explicit iso_date_holder(const Date& d) : d(d) {}
326 };
327 std::ostream& operator<<(std::ostream&, const iso_date_holder&);
328
330 formatted_date_holder(const Date& d, std::string f) : d(d), f(std::move(f)) {}
332 std::string f;
333 };
334 std::ostream& operator<<(std::ostream&,
335 const formatted_date_holder&);
336
337#ifdef QL_HIGH_RESOLUTION_DATE
338 struct iso_datetime_holder {
339 explicit iso_datetime_holder(const Date& d) : d(d) {}
340 Date d;
341 };
342 std::ostream& operator<<(std::ostream&, const iso_datetime_holder&);
343#endif
344 }
345
346 namespace io {
347
349
350 detail::short_date_holder short_date(const Date&);
351
353
354 detail::long_date_holder long_date(const Date&);
355
357
358 detail::iso_date_holder iso_date(const Date&);
359
361
362 detail::formatted_date_holder formatted_date(const Date&,
363 const std::string& fmt);
364
365#ifdef QL_HIGH_RESOLUTION_DATE
367
368 detail::iso_datetime_holder iso_datetime(const Date&);
369#endif
370
371 }
372
373 #ifdef QL_NULL_AS_FUNCTIONS
374
376 template <>
377 inline Date Null<Date>() {
378 return {};
379 }
380
381 #else
382
383 template <>
384 class Null<Date> {
385 public:
386 Null() = default;
387 operator Date() const { return {}; }
388 };
389
390 #endif
391
392#ifndef QL_HIGH_RESOLUTION_DATE
393 // inline definitions
394
395 inline Weekday Date::weekday() const {
396 Integer w = serialNumber_ % 7;
397 return Weekday(w == 0 ? 7 : w);
398 }
399
400 inline Day Date::dayOfMonth() const {
401 return dayOfYear() - monthOffset(month(),isLeap(year()));
402 }
403
404 inline Day Date::dayOfYear() const {
405 return serialNumber_ - yearOffset(year());
406 }
407
409 return serialNumber_;
410 }
411
413 return Date(serialNumber_+days);
414 }
415
417 return Date(serialNumber_-days);
418 }
419
420 inline Date Date::operator+(const Period& p) const {
421 return advance(*this,p.length(),p.units());
422 }
423
424 inline Date Date::operator-(const Period& p) const {
425 return advance(*this,-p.length(),p.units());
426 }
427
428 inline Date Date::endOfMonth(const Date& d) {
429 Month m = d.month();
430 Year y = d.year();
431 return {monthLength(m, isLeap(y)), m, y};
432 }
433
434 inline bool Date::isEndOfMonth(const Date& d) {
435 return (d.dayOfMonth() == monthLength(d.month(), isLeap(d.year())));
436 }
437
438 inline Date::serial_type operator-(const Date& d1, const Date& d2) {
439 return d1.serialNumber()-d2.serialNumber();
440 }
441
442 inline Time daysBetween(const Date& d1, const Date& d2) {
443 return Time(d2-d1);
444 }
445
446 inline bool operator==(const Date& d1, const Date& d2) {
447 return (d1.serialNumber() == d2.serialNumber());
448 }
449
450 inline bool operator!=(const Date& d1, const Date& d2) {
451 return (d1.serialNumber() != d2.serialNumber());
452 }
453
454 inline bool operator<(const Date& d1, const Date& d2) {
455 return (d1.serialNumber() < d2.serialNumber());
456 }
457
458 inline bool operator<=(const Date& d1, const Date& d2) {
459 return (d1.serialNumber() <= d2.serialNumber());
460 }
461
462 inline bool operator>(const Date& d1, const Date& d2) {
463 return (d1.serialNumber() > d2.serialNumber());
464 }
465
466 inline bool operator>=(const Date& d1, const Date& d2) {
467 return (d1.serialNumber() >= d2.serialNumber());
468 }
469#endif
470}
471
472namespace std {
473 template<>
474 struct hash<QuantLib::Date> {
475 std::size_t operator()(const QuantLib::Date& d) const {
476 return QuantLib::hash_value(d);
477 }
478 };
479}
480
481#endif
Concrete date class.
Definition: date.hpp:125
Date & operator-=(Date::serial_type days)
decrement date by the given number of days
Definition: date.cpp:113
static Date::serial_type yearOffset(Year y)
Definition: date.cpp:283
static Date minDate()
earliest allowed date
Definition: date.cpp:766
Month month() const
Definition: date.cpp:82
Year year() const
Definition: date.cpp:93
Day dayOfMonth() const
Definition: date.hpp:400
static Date todaysDate()
today's date.
Definition: date.cpp:788
static Date::serial_type maximumSerialNumber()
Definition: date.cpp:753
Date operator-(Date::serial_type days) const
returns a new date decremented by the given number of days
Definition: date.hpp:416
Date::serial_type serialNumber_
Definition: date.hpp:253
Date operator+(Date::serial_type days) const
returns a new date incremented by the given number of days
Definition: date.hpp:412
Date & operator+=(Date::serial_type days)
increments date by the given number of days
Definition: date.cpp:101
Date & operator--()
1-day pre-decrement
Definition: date.cpp:132
static Date endOfMonth(const Date &d)
last day of the month to which the given date belongs
Definition: date.hpp:428
static Integer monthOffset(Month m, bool leapYear)
Definition: date.cpp:269
Weekday weekday() const
Definition: date.hpp:395
Date & operator++()
1-day pre-increment
Definition: date.cpp:125
static Integer monthLength(Month m, bool leapYear)
Definition: date.cpp:259
Day dayOfYear() const
One-based (Jan 1st = 1)
Definition: date.hpp:404
static bool isEndOfMonth(const Date &d)
whether a date is the last day of its month
Definition: date.hpp:434
std::int_fast32_t serial_type
serial number type
Definition: date.hpp:128
Date::serial_type serialNumber() const
Definition: date.hpp:408
static Date advance(const Date &d, Integer units, TimeUnit)
Definition: date.cpp:139
static Date::serial_type minimumSerialNumber()
Definition: date.cpp:749
static void checkSerialNumber(Date::serial_type serialNumber)
Definition: date.cpp:757
static Date nthWeekday(Size n, Weekday w, Month m, Year y)
n-th given weekday in the given month and year
Definition: date.cpp:802
static bool isLeap(Year y)
whether the given year is a leap one
Definition: date.cpp:187
static Date nextWeekday(const Date &d, Weekday w)
next given weekday following or equal to the given date
Definition: date.cpp:797
static Date maxDate()
latest allowed date
Definition: date.cpp:771
Date()
Default constructor returning a null date.
Definition: date.cpp:58
template class providing a null value for a given type.
Definition: null.hpp:76
TimeUnit units() const
Definition: period.hpp:51
Integer length() const
Definition: period.hpp:50
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
Month
Month names.
Definition: date.hpp:57
@ Jul
Definition: date.hpp:74
@ December
Definition: date.hpp:68
@ August
Definition: date.hpp:64
@ Jan
Definition: date.hpp:69
@ January
Definition: date.hpp:57
@ Mar
Definition: date.hpp:71
@ Nov
Definition: date.hpp:78
@ Aug
Definition: date.hpp:75
@ Oct
Definition: date.hpp:77
@ Feb
Definition: date.hpp:70
@ Jun
Definition: date.hpp:73
@ July
Definition: date.hpp:63
@ Dec
Definition: date.hpp:79
@ May
Definition: date.hpp:61
@ March
Definition: date.hpp:59
@ February
Definition: date.hpp:58
@ April
Definition: date.hpp:60
@ November
Definition: date.hpp:67
@ October
Definition: date.hpp:66
@ Apr
Definition: date.hpp:72
@ June
Definition: date.hpp:62
@ Sep
Definition: date.hpp:76
@ September
Definition: date.hpp:65
detail::short_date_holder short_date(const Date &d)
output dates in short format (mm/dd/yyyy)
Definition: date.cpp:983
detail::long_date_holder long_date(const Date &d)
output dates in long format (Month ddth, yyyy)
Definition: date.cpp:987
detail::formatted_date_holder formatted_date(const Date &d, const std::string &f)
output dates in user defined format using boost date functionality
Definition: date.cpp:995
detail::iso_date_holder iso_date(const Date &d)
output dates in ISO format (yyyy-mm-dd)
Definition: date.cpp:991
Real Time
continuous quantity with 1-year units
Definition: types.hpp:62
QL_INTEGER Integer
integer number
Definition: types.hpp:35
std::size_t Size
size of a container
Definition: types.hpp:58
std::ostream & operator<<(std::ostream &out, const short_date_holder &holder)
Definition: date.cpp:894
Definition: any.hpp:35
Quantity operator-(const Quantity &m1, const Quantity &m2)
Definition: quantity.hpp:171
bool operator==(const Currency &c1, const Currency &c2)
Definition: currency.hpp:191
bool operator<(const Quantity &m1, const Quantity &m2)
Definition: quantity.cpp:125
bool operator>=(const Quantity &m1, const Quantity &m2)
Definition: quantity.hpp:201
std::ostream & operator<<(std::ostream &out, GFunctionFactory::YieldCurveModel type)
bool operator!=(const Currency &c1, const Currency &c2)
Definition: currency.hpp:196
bool operator>(const Quantity &m1, const Quantity &m2)
Definition: quantity.hpp:197
Real days(const Period &p)
Definition: period.cpp:330
std::size_t hash_value(const Date &d)
Definition: date.cpp:846
bool operator<=(const Quantity &m1, const Quantity &m2)
Definition: quantity.cpp:144
Time daysBetween(const Date &d1, const Date &d2)
Definition: date.hpp:442
STL namespace.
formatted_date_holder(const Date &d, std::string f)
Definition: date.hpp:330
iso_date_holder(const Date &d)
Definition: date.hpp:324
long_date_holder(const Date &d)
Definition: date.hpp:318
std::size_t operator()(const QuantLib::Date &d) const
Definition: date.hpp:475