26#include <boost/algorithm/string/case_conv.hpp>
29using boost::algorithm::to_upper_copy;
42 if (!mainCycle)
return true;
59 string str1(
"0123456789");
60 string::size_type loc = str1.find(in.substr(1,1), 0);
61 if (loc == string::npos)
64 if (mainCycle) str1 =
"hmzuHMZU";
65 else str1 =
"fghjkmnquvxzFGHJKMNQUVXZ";
66 loc = str1.find(in.substr(0,1), 0);
67 return loc != string::npos;
72 date <<
" is not an IMM date");
74 std::ostringstream IMMcode;
114 QL_FAIL(
"not an IMM month (and it should have been)");
117 #if defined(QL_EXTRA_SAFETY_CHECKS)
119 "the result " << IMMcode.str() <<
120 " is an invalid IMM code");
122 return IMMcode.str();
126 const Date& refDate) {
128 immCode <<
" is not a valid IMM code");
130 Date referenceDate = (refDate !=
Date() ?
134 std::string
code = to_upper_copy(immCode);
135 std::string ms =
code.substr(0,1);
139 else if (ms==
"H") m =
March;
140 else if (ms==
"J") m =
April;
141 else if (ms==
"K") m =
May;
142 else if (ms==
"M") m =
June;
143 else if (ms==
"N") m =
July;
144 else if (ms==
"Q") m =
August;
149 else QL_FAIL(
"invalid IMM month letter");
154 if (
y==0 && referenceDate.
year()<=1909)
y+=10;
155 Year referenceYear = (referenceDate.
year() % 10);
156 y += referenceDate.
year() - referenceYear;
158 if (result<referenceDate)
171 Size offset = mainCycle ? 3 : 1;
172 Size skipMonths = offset-(m%offset);
173 if (skipMonths != offset || refDate.
dayOfMonth() > 21) {
174 skipMonths +=
Size(m);
175 if (skipMonths<=12) {
191 const Date& referenceDate) {
192 Date immDate =
date(IMMcode, referenceDate);
193 return nextDate(immDate+1, mainCycle);
204 const Date& referenceDate) {
static Date nthWeekday(Size n, Weekday w, Month m, Year y)
n-th given weekday in the given month and year
static Settings & instance()
access to the unique instance
Classes used to parse data for input.
#define QL_ENSURE(condition, message)
throw an error if the given post-condition is not verified
#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)
std::size_t Size
size of a container
IMM-related date functions.
global repository for run-time library settings
static std::string nextCode(const Date &d=Date(), bool mainCycle=true)
next IMM code following the given date
static bool isIMMcode(const std::string &in, bool mainCycle=true)
returns whether or not the given string is an IMM code
static Date date(const std::string &immCode, const Date &referenceDate=Date())
static std::string code(const Date &immDate)
static bool isIMMdate(const Date &d, bool mainCycle=true)
returns whether or not the given date is an IMM date
static Date nextDate(const Date &d=Date(), bool mainCycle=true)
next IMM date following the given date