Coverage Report

Created: 2025-08-28 06:30

/src/quantlib/ql/utilities/dataparsers.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) 2002, 2003 Decillion Pty(Ltd)
5
 Copyright (C) 2006 Joseph Wang
6
 Copyright (2) 2009 Mark Joshi
7
 Copyright (2) 2009 StatPro Italia srl
8
9
 This file is part of QuantLib, a free-software/open-source library
10
 for financial quantitative analysts and developers - http://quantlib.org/
11
12
 QuantLib is free software: you can redistribute it and/or modify it
13
 under the terms of the QuantLib license.  You should have received a
14
 copy of the license along with this program; if not, please email
15
 <quantlib-dev@lists.sf.net>. The license is also available online at
16
 <http://quantlib.org/license.shtml>.
17
18
 This program is distributed in the hope that it will be useful, but WITHOUT
19
 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
20
 FOR A PARTICULAR PURPOSE.  See the license for more details.
21
*/
22
23
#include <ql/utilities/dataparsers.hpp>
24
#include <ql/utilities/null.hpp>
25
#include <ql/time/period.hpp>
26
#include <ql/errors.hpp>
27
#ifndef QL_PATCH_SOLARIS
28
#include <boost/algorithm/string/case_conv.hpp>
29
#include <boost/date_time/gregorian/gregorian.hpp>
30
#endif
31
#include <string>
32
#include <locale>
33
#include <cctype>
34
#if defined(BOOST_NO_STDC_NAMESPACE)
35
    namespace std { using ::toupper; }
36
#endif
37
38
namespace QuantLib {
39
40
0
    Period PeriodParser::parse(const std::string& str) {
41
0
        QL_REQUIRE(str.length()>1, "period string length must be at least 2");
42
43
0
        std::vector<std::string > subStrings;
44
0
        std::string reducedString = str;
45
46
0
        Size iPos, reducedStringDim = 100000, max_iter = 0;
47
0
        while (reducedStringDim>0) {
48
0
            iPos = reducedString.find_first_of("DdWwMmYy");
49
0
            Size subStringDim = iPos+1;
50
0
            reducedStringDim = reducedString.length()-subStringDim;
51
0
            subStrings.push_back(reducedString.substr(0, subStringDim));
52
0
            reducedString = reducedString.substr(iPos+1, reducedStringDim);
53
0
            ++max_iter;
54
0
            QL_REQUIRE(max_iter<str.length(), "unknown '" << str << "' unit");
55
0
        }
56
57
0
        Period result = parseOnePeriod(subStrings[0]);
58
0
        for (Size i=1; i<subStrings.size(); ++i)
59
0
            result += parseOnePeriod(subStrings[i]);
60
0
        return result;
61
0
    }
62
63
0
    Period PeriodParser::parseOnePeriod(const std::string& str) {
64
0
        QL_REQUIRE(str.length()>1, "single period require a string of at "
65
0
                   "least 2 characters");
66
67
0
        Size iPos = str.find_first_of("DdWwMmYy");
68
0
        QL_REQUIRE(iPos==str.length()-1, "unknown '" <<
69
0
                   str.substr(str.length()-1, str.length()) << "' unit");
70
0
        TimeUnit units = Days;
71
0
        char abbr = static_cast<char>(std::toupper(str[iPos]));
72
0
        if      (abbr == 'D') units = Days;
73
0
        else if (abbr == 'W') units = Weeks;
74
0
        else if (abbr == 'M') units = Months;
75
0
        else if (abbr == 'Y') units = Years;
76
77
0
        Size nPos = str.find_first_of("-+0123456789");
78
0
        QL_REQUIRE(nPos<iPos, "no numbers of " << units << " provided");
79
0
        Integer n;
80
0
        try {
81
0
            n = std::stoi(str.substr(nPos,iPos));
82
0
        } catch (std::exception& e) {
83
0
            QL_FAIL("unable to parse the number of units of " << units <<
84
0
                    " in '" << str << "'. Error:" << e.what());
85
0
        }
86
87
0
        return {n, units};
88
0
    }
89
90
    Date DateParser::parseFormatted(const std::string& str,
91
489
                                    const std::string& fmt) {
92
489
        #ifndef QL_PATCH_SOLARIS
93
489
        using namespace boost::gregorian;
94
95
489
        date boostDate;
96
489
        std::istringstream is(str);
97
489
        is.imbue(std::locale(std::locale(), new date_input_facet(fmt)));
98
489
        is >> boostDate;
99
489
        date_duration noDays = boostDate - date(1901, 1, 1);
100
489
        return Date(1, January, 1901) + noDays.days();
101
        #else
102
        QL_FAIL("DateParser::parseFormatted not supported under Solaris");
103
        #endif
104
489
    }
105
106
155
    Date DateParser::parseISO(const std::string& str) {
107
155
        QL_REQUIRE(str.size() == 10 && str[4] == '-' && str[7] == '-',
108
66
                   "invalid format");
109
66
        Integer year = std::stoi(str.substr(0, 4));
110
66
        Month month = static_cast<Month>(std::stoi(str.substr(5, 2)));
111
66
        Integer day = std::stoi(str.substr(8, 2));
112
113
66
        return {day, month, year};
114
155
    }
115
116
}