QuantLib: a free/open-source library for quantitative finance
Fully annotated sources - version 1.32
Loading...
Searching...
No Matches
yearfractiontodate.cpp
1/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3/*
4 Copyright (C) 2023 Klaus Spanderen
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#include <ql/math/comparison.hpp>
21#include "ql/time/daycounters/yearfractiontodate.hpp"
22
23#include <boost/numeric/conversion/cast.hpp>
24#include <cmath>
25
26namespace QuantLib {
27
29 const DayCounter& dayCounter, const Date& referenceDate, Time t) {
30 Date guessDate = referenceDate
31 + Period(boost::numeric_cast<Integer>(round(t * 365.25)), Days);
32 Time guessTime = dayCounter.yearFraction(referenceDate, guessDate);
33
34 guessDate += Period(boost::numeric_cast<Integer>(
35 round((t - guessTime)*365.25)), Days);
36 guessTime = dayCounter.yearFraction(referenceDate, guessDate);
37
38 if (close_enough(guessTime, t))
39 return guessDate;
40
41 const auto searchDirection = boost::numeric_cast<Integer>(copysign(1.0, t - guessTime));
42
43 t += searchDirection*100*QL_EPSILON;
44
45 Date nextDate;
46 for (TimeUnit u: {Years, Months, Days}) {
47 while (searchDirection*(
48 dayCounter.yearFraction(
49 referenceDate,
50 nextDate = guessDate + Period(searchDirection, u)) - t) < 0.0)
51 guessDate = nextDate;
52 }
53
54 guessTime = dayCounter.yearFraction(referenceDate, guessDate);
55 if (close_enough(guessTime, t)
56 || std::abs(dayCounter.yearFraction(referenceDate,
57 guessDate + Period(searchDirection, Days)) - t) >
58 std::abs(guessTime - t))
59 return guessDate;
60 else
61 return guessDate + Period(searchDirection, Days);
62 }
63}
64
Concrete date class.
Definition: date.hpp:125
day counter class
Definition: daycounter.hpp:44
Time yearFraction(const Date &, const Date &, const Date &refPeriodStart=Date(), const Date &refPeriodEnd=Date()) const
Returns the period between two dates as a fraction of year.
Definition: daycounter.hpp:128
TimeUnit
Units used to describe time periods.
Definition: timeunit.hpp:37
#define QL_EPSILON
Definition: qldefines.hpp:178
Real Time
continuous quantity with 1-year units
Definition: types.hpp:62
Definition: any.hpp:35
Date yearFractionToDate(const DayCounter &dayCounter, const Date &referenceDate, Time t)
bool close_enough(const Quantity &m1, const Quantity &m2, Size n)
Definition: quantity.cpp:182