Coverage Report

Created: 2025-11-04 06:12

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/quantlib/ql/time/daycounter.hpp
Line
Count
Source
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, 2007 StatPro Italia srl
6
7
 This file is part of QuantLib, a free-software/open-source library
8
 for financial quantitative analysts and developers - http://quantlib.org/
9
10
 QuantLib is free software: you can redistribute it and/or modify it
11
 under the terms of the QuantLib license.  You should have received a
12
 copy of the license along with this program; if not, please email
13
 <quantlib-dev@lists.sf.net>. The license is also available online at
14
 <https://www.quantlib.org/license.shtml>.
15
16
 This program is distributed in the hope that it will be useful, but WITHOUT
17
 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18
 FOR A PARTICULAR PURPOSE.  See the license for more details.
19
*/
20
21
/*! \file daycounter.hpp
22
    \brief day counter class
23
*/
24
25
#ifndef quantlib_day_counter_hpp
26
#define quantlib_day_counter_hpp
27
28
#include <ql/errors.hpp>
29
#include <ql/time/date.hpp>
30
#include <utility>
31
32
namespace QuantLib {
33
34
    //! day counter class
35
    /*! This class provides methods for determining the length of a time
36
        period according to given market convention, both as a number
37
        of days and as a year fraction.
38
39
        The Bridge pattern is used to provide the base behavior of the
40
        day counter.
41
42
        \ingroup datetime
43
    */
44
    class DayCounter {
45
      protected:
46
        //! abstract base class for day counter implementations
47
        class Impl {
48
          public:
49
71.9k
            virtual ~Impl() = default;
50
            virtual std::string name() const = 0;
51
            //! to be overloaded by more complex day counters
52
            virtual Date::serial_type dayCount(const Date& d1,
53
0
                                               const Date& d2) const {
54
0
                return (d2-d1);
55
0
            }
56
            virtual Time yearFraction(const Date& d1,
57
                                      const Date& d2,
58
                                      const Date& refPeriodStart,
59
                                      const Date& refPeriodEnd) const = 0;
60
        };
61
        ext::shared_ptr<Impl> impl_;
62
        /*! This constructor can be invoked by derived classes which
63
            define a given implementation.
64
        */
65
71.9k
        explicit DayCounter(ext::shared_ptr<Impl> impl) : impl_(std::move(impl)) {}
66
67
      public:
68
        /*! The default constructor returns a day counter with a null
69
            implementation, which is therefore unusable except as a
70
            placeholder.
71
        */
72
214k
        DayCounter() = default;
73
        //! \name DayCounter interface
74
        //@{
75
        //!  Returns whether or not the day counter is initialized
76
        bool empty() const;
77
        //! Returns the name of the day counter.
78
        /*! \warning This method is used for output and comparison between
79
                day counters. It is <b>not</b> meant to be used for writing
80
                switch-on-type code.
81
        */
82
        std::string name() const;
83
        //! Returns the number of days between two dates.
84
        Date::serial_type dayCount(const Date&,
85
                                   const Date&) const;
86
        //! Returns the period between two dates as a fraction of year.
87
        Time yearFraction(const Date&, const Date&,
88
                          const Date& refPeriodStart = Date(),
89
                          const Date& refPeriodEnd = Date()) const;
90
        //@}
91
    };
92
93
    // comparison based on name
94
95
    /*! Returns <tt>true</tt> iff the two day counters belong to the same
96
        derived class.
97
        \relates DayCounter
98
    */
99
    bool operator==(const DayCounter&,
100
                    const DayCounter&);
101
102
    /*! \relates DayCounter */
103
    bool operator!=(const DayCounter&,
104
                    const DayCounter&);
105
106
    /*! \relates DayCounter */
107
    std::ostream& operator<<(std::ostream&,
108
                             const DayCounter&);
109
110
111
    // inline definitions
112
113
141k
    inline bool DayCounter::empty() const {
114
141k
        return !impl_;
115
141k
    }
116
117
0
    inline std::string DayCounter::name() const {
118
0
        QL_REQUIRE(impl_, "no day counter implementation provided");
119
0
        return impl_->name();
120
0
    }
Unexecuted instantiation: QuantLib::DayCounter::name() const
Unexecuted instantiation: QuantLib::DayCounter::name() const
121
122
    inline Date::serial_type DayCounter::dayCount(const Date& d1,
123
0
                                                  const Date& d2) const {
124
0
        QL_REQUIRE(impl_, "no day counter implementation provided");
125
0
        return impl_->dayCount(d1,d2);
126
0
    }
Unexecuted instantiation: QuantLib::DayCounter::dayCount(QuantLib::Date const&, QuantLib::Date const&) const
Unexecuted instantiation: QuantLib::DayCounter::dayCount(QuantLib::Date const&, QuantLib::Date const&) const
127
128
    inline Time DayCounter::yearFraction(const Date& d1, const Date& d2,
129
25.5M
        const Date& refPeriodStart, const Date& refPeriodEnd) const {
130
25.5M
            QL_REQUIRE(impl_, "no day counter implementation provided");
131
25.5M
            return impl_->yearFraction(d1,d2,refPeriodStart,refPeriodEnd);
132
25.5M
    }
133
134
135
0
    inline bool operator==(const DayCounter& d1, const DayCounter& d2) {
136
0
        return (d1.empty() && d2.empty())
137
0
            || (!d1.empty() && !d2.empty() && d1.name() == d2.name());
138
0
    }
139
140
0
    inline bool operator!=(const DayCounter& d1, const DayCounter& d2) {
141
0
        return !(d1 == d2);
142
0
    }
143
144
0
    inline std::ostream& operator<<(std::ostream& out, const DayCounter &d) {
145
0
        return out << d.name();
146
0
    }
147
148
}
149
150
#endif