| 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 | Period PeriodParser::parse(const std::string& str) { |
| 41 | QL_REQUIRE(str.length()>1, "period string length must be at least 2" ); |
| 42 | |
| 43 | std::vector<std::string > subStrings; |
| 44 | std::string reducedString = str; |
| 45 | |
| 46 | Size iPos, reducedStringDim = 100000, max_iter = 0; |
| 47 | while (reducedStringDim>0) { |
| 48 | iPos = reducedString.find_first_of(s: "DdWwMmYy" ); |
| 49 | Size subStringDim = iPos+1; |
| 50 | reducedStringDim = reducedString.length()-subStringDim; |
| 51 | subStrings.push_back(x: reducedString.substr(pos: 0, n: subStringDim)); |
| 52 | reducedString = reducedString.substr(pos: iPos+1, n: reducedStringDim); |
| 53 | ++max_iter; |
| 54 | QL_REQUIRE(max_iter<str.length(), "unknown '" << str << "' unit" ); |
| 55 | } |
| 56 | |
| 57 | Period result = parseOnePeriod(str: subStrings[0]); |
| 58 | for (Size i=1; i<subStrings.size(); ++i) |
| 59 | result += parseOnePeriod(str: subStrings[i]); |
| 60 | return result; |
| 61 | } |
| 62 | |
| 63 | Period PeriodParser::parseOnePeriod(const std::string& str) { |
| 64 | QL_REQUIRE(str.length()>1, "single period require a string of at " |
| 65 | "least 2 characters" ); |
| 66 | |
| 67 | Size iPos = str.find_first_of(s: "DdWwMmYy" ); |
| 68 | QL_REQUIRE(iPos==str.length()-1, "unknown '" << |
| 69 | str.substr(str.length()-1, str.length()) << "' unit" ); |
| 70 | TimeUnit units = Days; |
| 71 | char abbr = static_cast<char>(std::toupper(c: str[iPos])); |
| 72 | if (abbr == 'D') units = Days; |
| 73 | else if (abbr == 'W') units = Weeks; |
| 74 | else if (abbr == 'M') units = Months; |
| 75 | else if (abbr == 'Y') units = Years; |
| 76 | |
| 77 | Size nPos = str.find_first_of(s: "-+0123456789" ); |
| 78 | QL_REQUIRE(nPos<iPos, "no numbers of " << units << " provided" ); |
| 79 | Integer n; |
| 80 | try { |
| 81 | n = std::stoi(str: str.substr(pos: nPos,n: iPos)); |
| 82 | } catch (std::exception& e) { |
| 83 | QL_FAIL("unable to parse the number of units of " << units << |
| 84 | " in '" << str << "'. Error:" << e.what()); |
| 85 | } |
| 86 | |
| 87 | return {n, units}; |
| 88 | } |
| 89 | |
| 90 | Date DateParser::parseFormatted(const std::string& str, |
| 91 | const std::string& fmt) { |
| 92 | #ifndef QL_PATCH_SOLARIS |
| 93 | using namespace boost::gregorian; |
| 94 | |
| 95 | date boostDate; |
| 96 | std::istringstream is(str); |
| 97 | is.imbue(loc: std::locale(std::locale(), new date_input_facet(fmt))); |
| 98 | is >> boostDate; |
| 99 | date_duration noDays = boostDate - date(1901, 1, 1); |
| 100 | return Date(1, January, 1901) + noDays.days(); |
| 101 | #else |
| 102 | QL_FAIL("DateParser::parseFormatted not supported under Solaris" ); |
| 103 | #endif |
| 104 | } |
| 105 | |
| 106 | Date DateParser::parseISO(const std::string& str) { |
| 107 | QL_REQUIRE(str.size() == 10 && str[4] == '-' && str[7] == '-', |
| 108 | "invalid format" ); |
| 109 | Integer year = std::stoi(str: str.substr(pos: 0, n: 4)); |
| 110 | Month month = static_cast<Month>(std::stoi(str: str.substr(pos: 5, n: 2))); |
| 111 | Integer day = std::stoi(str: str.substr(pos: 8, n: 2)); |
| 112 | |
| 113 | return {day, month, year}; |
| 114 | } |
| 115 | |
| 116 | } |
| 117 | |