Coverage Report

Created: 2026-03-11 06:44

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/quantlib/ql/experimental/commodities/dateinterval.hpp
Line
Count
Source
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
 <https://www.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
0
        : startDate_(startDate), endDate_(endDate) {
45
0
            QL_REQUIRE(endDate_ >= startDate_,
46
0
                       "end date must be >= start date");
47
0
        }
48
0
        const Date& startDate() const { return startDate_; }
49
0
        const Date& endDate() const { return endDate_; }
50
51
        bool isDateBetween(Date date,
52
                           bool includeFirst = true,
53
0
                           bool includeLast = true) const {
54
0
            if (includeFirst && !(date >= startDate_))
55
0
                return false;
56
0
            else if (!(date > startDate_))
57
0
                return false;
58
0
            if (includeLast && !(date <= endDate_))
59
0
                return false;
60
0
            else if (!(date < endDate_))
61
0
                return false;
62
0
            return true;
63
0
        }
64
65
0
        DateInterval intersection(const DateInterval& di) const {
66
0
            if ((startDate_ < di.startDate_ && endDate_ < di.startDate_) ||
67
0
                (startDate_ > di.endDate_ && endDate_ > di.endDate_))
68
0
                return {};
69
0
            return {std::max(startDate_, di.startDate_), std::min(endDate_, di.endDate_)};
70
0
        }
71
72
0
        bool operator==(const DateInterval& rhs) const {
73
0
            return startDate_ == rhs.startDate_ && endDate_ == rhs.endDate_;
74
0
        }
75
76
0
        bool operator!=(const DateInterval& rhs) const {
77
0
            return !(*this == rhs);
78
0
        }
79
    };
80
81
}
82
83
#endif