Coverage Report

Created: 2025-08-28 06:30

/src/quantlib/ql/time/daycounters/actual365fixed.cpp
Line
Count
Source (jump to first uncovered line)
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) 2013 BGC Partners L.P.
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
 <http://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
#include <ql/time/daycounters/actual365fixed.hpp>
22
#include <cmath>
23
24
namespace QuantLib {
25
26
    ext::shared_ptr<DayCounter::Impl>
27
0
    Actual365Fixed::implementation(Actual365Fixed::Convention c) {
28
0
        switch (c) {
29
0
          case Standard:
30
0
            return ext::shared_ptr<DayCounter::Impl>(new Impl);
31
0
          case Canadian:
32
0
            return ext::shared_ptr<DayCounter::Impl>(new CA_Impl);
33
0
          case NoLeap:
34
0
            return ext::shared_ptr<DayCounter::Impl>(new NL_Impl);
35
0
          default:
36
0
            QL_FAIL("unknown Actual/365 (Fixed) convention");
37
0
        }
38
0
    }
39
40
    Time Actual365Fixed::CA_Impl::yearFraction(const Date& d1,
41
                                               const Date& d2,
42
                                               const Date& refPeriodStart,
43
0
                                               const Date& refPeriodEnd) const {
44
0
        if (d1 == d2)
45
0
            return 0.0;
46
47
        // We need the period to calculate the frequency
48
0
        QL_REQUIRE(refPeriodStart != Date(), "invalid refPeriodStart");
49
0
        QL_REQUIRE(refPeriodEnd != Date(), "invalid refPeriodEnd");
50
51
0
        Time dcs = daysBetween(d1,d2);
52
0
        Time dcc = daysBetween(refPeriodStart,refPeriodEnd);
53
0
        auto months = Integer(std::lround(12 * dcc / 365));
54
0
        QL_REQUIRE(months != 0,
55
0
                   "invalid reference period for Act/365 Canadian; "
56
0
                   "must be longer than a month");
57
0
        auto frequency = Integer(12 / months);
58
0
        QL_REQUIRE(frequency != 0,
59
0
                   "invalid reference period for Act/365 Canadian; "
60
0
                   "must not be longer than a year");
61
62
0
        if (dcs < Integer(365/frequency))
63
0
            return dcs/365.0;
64
65
0
        return 1./frequency - (dcc-dcs)/365.0;
66
67
0
    }
68
69
    Date::serial_type Actual365Fixed::NL_Impl::dayCount(const Date& d1,
70
0
                                                        const Date& d2) const {
71
72
0
        static const Integer MonthOffset[] = {
73
0
            0,  31,  59,  90, 120, 151,  // Jan - Jun
74
0
            181, 212, 243, 273, 304, 334   // Jun - Dec
75
0
        };
76
77
0
        Date::serial_type s1 = d1.dayOfMonth()
78
0
                             + MonthOffset[d1.month()-1] + (d1.year() * 365);
79
0
        Date::serial_type s2 = d2.dayOfMonth()
80
0
                             + MonthOffset[d2.month()-1] + (d2.year() * 365);
81
82
0
        if (d1.month() == Feb && d1.dayOfMonth() == 29) {
83
0
            --s1;
84
0
        }
85
86
0
        if (d2.month() == Feb && d2.dayOfMonth() == 29) {
87
0
            --s2;
88
0
        }
89
90
0
        return s2 - s1;
91
0
    }
92
93
    Time Actual365Fixed::NL_Impl::yearFraction(const Date& d1,
94
                                               const Date& d2,
95
                                               const Date& d3,
96
0
                                               const Date& d4) const {
97
0
        return dayCount(d1, d2)/365.0;
98
0
    }
99
100
}
101