QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
period.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) 2004, 2005, 2006, 2007, 2008 Ferdinando Ametrano
5 Copyright (C) 2006 Katiuscia Manzoni
6 Copyright (C) 2000, 2001, 2002, 2003 RiskMap srl
7 Copyright (C) 2003, 2004, 2005, 2006, 2007 StatPro Italia srl
8
9 This file is part of QuantLib, a free-software/open-source library
10 for financial quantitative analysts and developers - http://quantlib.org/
11
12 QuantLib is free software: you can redistribute it and/or modify it
13 under the terms of the QuantLib license. You should have received a
14 copy of the license along with this program; if not, please email
15 <quantlib-dev@lists.sf.net>. The license is also available online at
16 <http://quantlib.org/license.shtml>.
17
18 This program is distributed in the hope that it will be useful, but WITHOUT
19 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
20 FOR A PARTICULAR PURPOSE. See the license for more details.
21*/
22
23/*! \file period.hpp
24 \brief period- and frequency-related classes and enumerations
25*/
26
27#ifndef quantlib_period_hpp
28#define quantlib_period_hpp
29
30#include <ql/time/frequency.hpp>
31#include <ql/time/timeunit.hpp>
32#include <ql/types.hpp>
33
34
35namespace QuantLib {
36
37 /*! This class provides a Period (length + TimeUnit) class
38 and implements a limited algebra.
39
40 \ingroup datetime
41
42 \test self-consistency of algebra is checked.
43 */
44 class Period {
45 public:
46 Period() = default;
48 : length_(n), units_(units) {}
49 explicit Period(Frequency f);
50 Integer length() const { return length_; }
51 TimeUnit units() const { return units_; }
52 Frequency frequency() const;
53 Period& operator+=(const Period&);
54 Period& operator-=(const Period&);
57 void normalize();
58 Period normalized() const;
59 private:
62 };
63
64 /*! \relates Period */
65 Real years(const Period&);
66 /*! \relates Period */
67 Real months(const Period&);
68 /*! \relates Period */
69 Real weeks(const Period&);
70 /*! \relates Period */
71 Real days(const Period&);
72
73 /*! \relates Period */
74 template <typename T> Period operator*(T n, TimeUnit units);
75 /*! \relates Period */
76 template <typename T> Period operator*(TimeUnit units, T n);
77
78 /*! \relates Period */
79 Period operator-(const Period&);
80
81 /*! \relates Period */
83 /*! \relates Period */
85
86 /*! \relates Period */
88
89 /*! \relates Period */
90 Period operator+(const Period&, const Period&);
91 /*! \relates Period */
92 Period operator-(const Period&, const Period&);
93
94 /*! \relates Period */
95 bool operator<(const Period&, const Period&);
96 /*! \relates Period */
97 bool operator==(const Period&, const Period&);
98 /*! \relates Period */
99 bool operator!=(const Period&, const Period&);
100 /*! \relates Period */
101 bool operator>(const Period&, const Period&);
102 /*! \relates Period */
103 bool operator<=(const Period&, const Period&);
104 /*! \relates Period */
105 bool operator>=(const Period&, const Period&);
106
107 /*! \relates Period */
108 std::ostream& operator<<(std::ostream&, const Period&);
109
110 namespace detail {
111
113 explicit long_period_holder(const Period& p) : p(p) {}
115 };
116 std::ostream& operator<<(std::ostream&, const long_period_holder&);
117
121 };
122 std::ostream& operator<<(std::ostream&, const short_period_holder&);
123
124 }
125
126 namespace io {
127
128 //! output periods in long format (e.g. "2 weeks")
129 /*! \ingroup manips */
131
132 //! output periods in short format (e.g. "2w")
133 /*! \ingroup manips */
135
136 }
137
138 // inline definitions
139
140 inline Period Period::normalized() const {
141 Period p = *this;
142 p.normalize();
143 return p;
144 }
145
146 template <typename T>
147 inline Period operator*(T n, TimeUnit units) {
148 return {Integer(n), units};
149 }
150
151 template <typename T>
152 inline Period operator*(TimeUnit units, T n) {
153 return {Integer(n), units};
154 }
155
156 inline Period operator-(const Period& p) { return {-p.length(), p.units()}; }
157
158 inline Period operator*(Integer n, const Period& p) { return {n * p.length(), p.units()}; }
159
160 inline Period operator*(const Period& p, Integer n) { return {n * p.length(), p.units()}; }
161
162 inline bool operator==(const Period& p1, const Period& p2) {
163 return !(p1 < p2 || p2 < p1);
164 }
165
166 inline bool operator!=(const Period& p1, const Period& p2) {
167 return !(p1 == p2);
168 }
169
170 inline bool operator>(const Period& p1, const Period& p2) {
171 return p2 < p1;
172 }
173
174 inline bool operator<=(const Period& p1, const Period& p2) {
175 return !(p1 > p2);
176 }
177
178 inline bool operator>=(const Period& p1, const Period& p2) {
179 return !(p1 < p2);
180 }
181
182}
183
184#endif
Period & operator+=(const Period &)
Definition: period.cpp:134
Period(Integer n, TimeUnit units)
Definition: period.hpp:47
TimeUnit units() const
Definition: period.hpp:51
Period & operator/=(Integer)
Definition: period.cpp:228
Period()=default
Integer length() const
Definition: period.hpp:50
Period & operator*=(Integer)
Definition: period.cpp:223
Period normalized() const
Definition: period.hpp:140
Period & operator-=(const Period &)
Definition: period.cpp:219
void normalize()
Definition: period.cpp:108
Frequency frequency() const
Definition: period.cpp:69
TimeUnit units_
Definition: period.hpp:61
Integer length_
Definition: period.hpp:60
Frequency enumeration.
Frequency
Frequency of events.
Definition: frequency.hpp:37
TimeUnit
Units used to describe time periods.
Definition: timeunit.hpp:37
detail::short_period_holder short_period(const Period &p)
output periods in short format (e.g. "2w")
Definition: period.cpp:446
detail::long_period_holder long_period(const Period &p)
output periods in long format (e.g. "2 weeks")
Definition: period.cpp:442
QL_REAL Real
real number
Definition: types.hpp:50
QL_INTEGER Integer
integer number
Definition: types.hpp:35
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:231
Real weeks(const Period &p)
Definition: period.cpp:313
bool operator<(const Quantity &m1, const Quantity &m2)
Definition: quantity.cpp:125
Real months(const Period &p)
Definition: period.cpp:296
Quantity operator*(const Quantity &m, Real x)
Definition: quantity.hpp:177
Real years(const Period &p)
Definition: period.cpp:279
bool operator>=(const Quantity &m1, const Quantity &m2)
Definition: quantity.hpp:201
std::ostream & operator<<(std::ostream &out, GFunctionFactory::YieldCurveModel type)
Quantity operator+(const Quantity &m1, const Quantity &m2)
Definition: quantity.hpp:165
bool operator!=(const Currency &c1, const Currency &c2)
Definition: currency.hpp:236
bool operator>(const Quantity &m1, const Quantity &m2)
Definition: quantity.hpp:197
Real days(const Period &p)
Definition: period.cpp:330
bool operator<=(const Quantity &m1, const Quantity &m2)
Definition: quantity.cpp:144
Real operator/(const Quantity &m1, const Quantity &m2)
Definition: quantity.cpp:86
long_period_holder(const Period &p)
Definition: period.hpp:113
TimeUnit enumeration.
Custom types.