27#ifndef QL_PATCH_SOLARIS
28#include <boost/algorithm/string/case_conv.hpp>
29#include <boost/date_time/gregorian/gregorian.hpp>
34#if defined(BOOST_NO_STDC_NAMESPACE)
35 namespace std { using ::toupper; }
41 QL_REQUIRE(str.length()>1,
"period string length must be at least 2");
43 std::vector<std::string > subStrings;
44 std::string reducedString = str;
46 Size iPos, reducedStringDim = 100000, max_iter = 0;
47 while (reducedStringDim>0) {
48 iPos = reducedString.find_first_of(
"DdWwMmYy");
49 Size subStringDim = iPos+1;
50 reducedStringDim = reducedString.length()-subStringDim;
51 subStrings.push_back(reducedString.substr(0, subStringDim));
52 reducedString = reducedString.substr(iPos+1, reducedStringDim);
54 QL_REQUIRE(max_iter<str.length(),
"unknown '" << str <<
"' unit");
58 for (
Size i=1; i<subStrings.size(); ++i)
64 QL_REQUIRE(str.length()>1,
"single period require a string of at "
65 "least 2 characters");
67 Size iPos = str.find_first_of(
"DdWwMmYy");
68 QL_REQUIRE(iPos==str.length()-1,
"unknown '" <<
69 str.substr(str.length()-1, str.length()) <<
"' unit");
71 char abbr =
static_cast<char>(std::toupper(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;
77 Size nPos = str.find_first_of(
"-+0123456789");
78 QL_REQUIRE(nPos<iPos,
"no numbers of " << units <<
" provided");
81 n = std::stoi(str.substr(nPos,iPos));
82 }
catch (std::exception& e) {
83 QL_FAIL(
"unable to parse the number of units of " << units <<
84 " in '" << str <<
"'. Error:" << e.what());
91 const std::string& fmt) {
92 #ifndef QL_PATCH_SOLARIS
93 using namespace boost::gregorian;
96 std::istringstream is(str);
97 is.imbue(std::locale(std::locale(),
new date_input_facet(fmt)));
99 date_duration noDays = boostDate - date(1901, 1, 1);
102 QL_FAIL(
"DateParser::parseFormatted not supported under Solaris");
107 QL_REQUIRE(str.size() == 10 && str[4] ==
'-' && str[7] ==
'-',
109 Integer year = std::stoi(str.substr(0, 4));
110 Month month =
static_cast<Month>(std::stoi(str.substr(5, 2)));
111 Integer day = std::stoi(str.substr(8, 2));
113 return {day, month, year};
static Date parseISO(const std::string &str)
static Date parseFormatted(const std::string &str, const std::string &fmt)
Parses a string in a used-defined format.
static Period parse(const std::string &str)
static Period parseOnePeriod(const std::string &str)
Classes used to parse data for input.
Classes and functions for error handling.
#define QL_REQUIRE(condition, message)
throw an error if the given pre-condition is not verified
#define QL_FAIL(message)
throw an error (possibly with file and line information)
TimeUnit
Units used to describe time periods.
QL_INTEGER Integer
integer number
std::size_t Size
size of a container
period- and frequency-related classes and enumerations