| 1 | /* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
| 2 | |
| 3 | /* |
| 4 | Copyright (C) 2008 J. Erik Radmall |
| 5 | |
| 6 | This file is part of QuantLib, a free-software/open-source library |
| 7 | for financial quantitative analysts and developers - http://quantlib.org/ |
| 8 | |
| 9 | QuantLib is free software: you can redistribute it and/or modify it |
| 10 | under the terms of the QuantLib license. You should have received a |
| 11 | copy of the license along with this program; if not, please email |
| 12 | <quantlib-dev@lists.sf.net>. The license is also available online at |
| 13 | <http://quantlib.org/license.shtml>. |
| 14 | |
| 15 | This program is distributed in the hope that it will be useful, but WITHOUT |
| 16 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 17 | FOR A PARTICULAR PURPOSE. See the license for more details. |
| 18 | */ |
| 19 | |
| 20 | /*! \file dateinterval.hpp |
| 21 | \brief Date interval |
| 22 | */ |
| 23 | |
| 24 | #ifndef quantlib_date_interval_hpp |
| 25 | #define quantlib_date_interval_hpp |
| 26 | |
| 27 | #include <ql/time/date.hpp> |
| 28 | #include <ql/errors.hpp> |
| 29 | #include <algorithm> |
| 30 | |
| 31 | namespace QuantLib { |
| 32 | |
| 33 | //! Date interval described by a number of a given time unit |
| 34 | /*! \ingroup datetime */ |
| 35 | class DateInterval { |
| 36 | friend std::ostream& operator<<(std::ostream&, const DateInterval&); |
| 37 | |
| 38 | private: |
| 39 | Date startDate_; |
| 40 | Date endDate_; |
| 41 | public: |
| 42 | DateInterval() = default; |
| 43 | DateInterval(const Date& startDate, const Date& endDate) |
| 44 | : startDate_(startDate), endDate_(endDate) { |
| 45 | QL_REQUIRE(endDate_ >= startDate_, |
| 46 | "end date must be >= start date" ); |
| 47 | } |
| 48 | const Date& startDate() const { return startDate_; } |
| 49 | const Date& endDate() const { return endDate_; } |
| 50 | |
| 51 | bool isDateBetween(Date date, |
| 52 | bool includeFirst = true, |
| 53 | bool includeLast = true) const { |
| 54 | if (includeFirst && !(date >= startDate_)) |
| 55 | return false; |
| 56 | else if (!(date > startDate_)) |
| 57 | return false; |
| 58 | if (includeLast && !(date <= endDate_)) |
| 59 | return false; |
| 60 | else if (!(date < endDate_)) |
| 61 | return false; |
| 62 | return true; |
| 63 | } |
| 64 | |
| 65 | DateInterval intersection(const DateInterval& di) const { |
| 66 | if ((startDate_ < di.startDate_ && endDate_ < di.startDate_) || |
| 67 | (startDate_ > di.endDate_ && endDate_ > di.endDate_)) |
| 68 | return {}; |
| 69 | return {std::max(a: startDate_, b: di.startDate_), std::min(a: endDate_, b: di.endDate_)}; |
| 70 | } |
| 71 | |
| 72 | bool operator==(const DateInterval& rhs) const { |
| 73 | return startDate_ == rhs.startDate_ && endDate_ == rhs.endDate_; |
| 74 | } |
| 75 | |
| 76 | bool operator!=(const DateInterval& rhs) const { |
| 77 | return !(*this == rhs); |
| 78 | } |
| 79 | }; |
| 80 | |
| 81 | } |
| 82 | |
| 83 | #endif |
| 84 | |