bool operator<(const Period& p1, const Period& p2) { // special cases if (p1.length() == 0) return p2.length() > 0; if (p2.length() == 0) return p1.length() < 0; // exact comparisons if (p1.units() == p2.units()) return p1.length() < p2.length(); if (p1.units() == Months && p2.units() == Years) return p1.length() < 12*p2.length(); if (p1.units() == Years && p2.units() == Months) return 12*p1.length() < p2.length(); if (p1.units() == Days && p2.units() == Weeks) return p1.length() < 7*p2.length(); if (p1.units() == Weeks && p2.units() == Days) return 7*p1.length() < p2.length(); // inexact comparisons (handled by converting to days and using limits) std::pair<Integer, Integer> p1lim = daysMinMax(p1); std::pair<Integer, Integer> p2lim = daysMinMax(p2); if (p1lim.second < p2lim.first) return true; else if (p1lim.first > p2lim.second) return false; else QL_FAIL("undecidable comparison between " << p1 << " and " << p2); }
Time SwaptionVolatilityStructure::swapLength(const Period& p) const { QL_REQUIRE(p.length()>0, "non-positive swap tenor (" << p << ") given"); switch (p.units()) { case Months: return p.length()/12.0; case Years: return static_cast<Time>(p.length()); default: QL_FAIL("invalid Time Unit (" << p.units() << ") for swap length"); } }
Real days(const Period& p) { if (p.length()==0) return 0.0; switch (p.units()) { case Days: return p.length(); case Weeks: return p.length()*7.0; case Months: QL_FAIL("cannot convert Months into Days"); case Years: QL_FAIL("cannot convert Years into Days"); default: QL_FAIL("unknown time unit (" << Integer(p.units()) << ")"); } }
Date Calendar::advance(const Date & d, const Period & p, BusinessDayConvention c, bool endOfMonth) const { return advance(d, p.length(), p.units(), c, endOfMonth); }
inline Date Date::operator-(const Period& p) const { return advance(*this,-p.length(),p.units()); }