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

source code of quantlib/ql/time/daycounters/actual365fixed.cpp