/src/quantlib/ql/time/period.hpp
Line | Count | Source |
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 | | <https://www.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 | | |
35 | | namespace 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 | 24.8M | Period() = default; |
47 | | Period(Integer n, TimeUnit units) |
48 | 51.5M | : length_(n), units_(units) {} |
49 | | explicit Period(Frequency f); |
50 | 178M | Integer length() const { return length_; } |
51 | 79.6M | TimeUnit units() const { return units_; } |
52 | | Frequency frequency() const; |
53 | | Period& operator+=(const Period&); |
54 | | Period& operator-=(const Period&); |
55 | | Period& operator*=(Integer); |
56 | | Period& operator/=(Integer); |
57 | | void normalize(); |
58 | | Period normalized() const; |
59 | | private: |
60 | | Integer length_ = 0; |
61 | | TimeUnit units_ = Days; |
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 */ |
82 | | Period operator*(Integer n, const Period&); |
83 | | /*! \relates Period */ |
84 | | Period operator*(const Period&, Integer n); |
85 | | |
86 | | /*! \relates Period */ |
87 | | Period operator/(const Period&, Integer n); |
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 | | |
112 | | struct long_period_holder { |
113 | 0 | explicit long_period_holder(const Period& p) : p(p) {} |
114 | | Period p; |
115 | | }; |
116 | | std::ostream& operator<<(std::ostream&, const long_period_holder&); |
117 | | |
118 | | struct short_period_holder { |
119 | 1.18k | explicit short_period_holder(Period p) : p(p) {} |
120 | | Period p; |
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 */ |
130 | | detail::long_period_holder long_period(const Period&); |
131 | | |
132 | | //! output periods in short format (e.g. "2w") |
133 | | /*! \ingroup manips */ |
134 | | detail::short_period_holder short_period(const Period&); |
135 | | |
136 | | } |
137 | | |
138 | | // inline definitions |
139 | | |
140 | 0 | inline Period Period::normalized() const { |
141 | 0 | Period p = *this; |
142 | 0 | p.normalize(); |
143 | 0 | return p; |
144 | 0 | } |
145 | | |
146 | | template <typename T> |
147 | 25.5M | inline Period operator*(T n, TimeUnit units) { |
148 | 25.5M | return {Integer(n), units}; |
149 | 25.5M | } QuantLib::Period QuantLib::operator*<int>(int, QuantLib::TimeUnit) Line | Count | Source | 147 | 25.5M | inline Period operator*(T n, TimeUnit units) { | 148 | 25.5M | return {Integer(n), units}; | 149 | 25.5M | } |
Unexecuted instantiation: QuantLib::Period QuantLib::operator*<unsigned int>(unsigned int, QuantLib::TimeUnit) Unexecuted instantiation: QuantLib::Period QuantLib::operator*<double>(double, QuantLib::TimeUnit) Unexecuted instantiation: QuantLib::Period QuantLib::operator*<long>(long, QuantLib::TimeUnit) Unexecuted instantiation: QuantLib::Period QuantLib::operator*<unsigned long>(unsigned long, QuantLib::TimeUnit) |
150 | | |
151 | | template <typename T> |
152 | | inline Period operator*(TimeUnit units, T n) { |
153 | | return {Integer(n), units}; |
154 | | } |
155 | | |
156 | 0 | inline Period operator-(const Period& p) { return {-p.length(), p.units()}; } |
157 | | |
158 | 25.3M | inline Period operator*(Integer n, const Period& p) { return {n * p.length(), p.units()}; } |
159 | | |
160 | 549k | inline Period operator*(const Period& p, Integer n) { return {n * p.length(), p.units()}; } |
161 | | |
162 | 25.2M | inline bool operator==(const Period& p1, const Period& p2) { |
163 | 25.2M | return !(p1 < p2 || p2 < p1); |
164 | 25.2M | } |
165 | | |
166 | 24.7M | inline bool operator!=(const Period& p1, const Period& p2) { |
167 | 24.7M | return !(p1 == p2); |
168 | 24.7M | } |
169 | | |
170 | 0 | inline bool operator>(const Period& p1, const Period& p2) { |
171 | 0 | return p2 < p1; |
172 | 0 | } |
173 | | |
174 | 0 | inline bool operator<=(const Period& p1, const Period& p2) { |
175 | 0 | return !(p1 > p2); |
176 | 0 | } |
177 | | |
178 | 71.0k | inline bool operator>=(const Period& p1, const Period& p2) { |
179 | 71.0k | return !(p1 < p2); |
180 | 71.0k | } |
181 | | |
182 | | } |
183 | | |
184 | | #endif |