double dateUtil::getAccrualFactor(date startDate,date endDate, enums::DayCountEnum dayCount){ double accrualFactor; int numBizDay=0; switch(dayCount){ case enums::thirty_360US: //This day count convention is also called Bond basis. if (endDate.getDay()==31 && (startDate.getDay()==30||31)) endDate.setDay(30); if (startDate.getDay()==31) startDate.setDay(30); accrualFactor = thirty_360(startDate, endDate); break; case enums::thirthE_360: if (startDate.getDay()==31) startDate.setDay(30); if (endDate.getDay()==31) endDate.setDay(30); accrualFactor = thirty_360(startDate, endDate); break; case enums::ACT_360: //This day count is also called Money Market basis or Actual 360 //This is the most used day count convention for money market instruments (maturity below one year). accrualFactor = (endDate.getJudianDayNumber()-startDate.getJudianDayNumber())/360.0; //cout<<"inside ACT_360"<<endl; break; case enums::ACT_365: //Also called English Money Market basis. //The number 365 is used even in a leap year. accrualFactor = (endDate.getJudianDayNumber()-startDate.getJudianDayNumber())/365.0; //cout<<"inside ACT_365"<<endl; break; case enums::ACT_ACT: if (startDate.getYear()==endDate.getYear()){ int numDays = endDate.getJudianDayNumber()-startDate.getJudianDayNumber(); accrualFactor = numDays/(isleapyear(startDate.getYear())?366.0:365.0); }else{ int numStartYearDays = date(startDate.getYear(),12,31).getJudianDayNumber() - startDate.getJudianDayNumber() + 1; double startYearFactor = numStartYearDays/(isleapyear(startDate.getYear())?366.0:365.0); int numEndYearDays = endDate.getJudianDayNumber() - date(endDate.getYear(),1,1).getJudianDayNumber(); double endYearFactor = numEndYearDays/(isleapyear(endDate.getYear())?366.0:365.0); accrualFactor = startYearFactor + endYearFactor +(endDate.getYear()-startDate.getYear()-1); } break; case enums::BUS_252: //Numerator is the number of business days (in a given calendar) from and including the start date up to and excluding the end date. numBizDay=0; for(long i = startDate.getJudianDayNumber();i<endDate.getJudianDayNumber();i++) if (isBizDay(i)) numBizDay++; accrualFactor = numBizDay/252.0; break; case enums::DayCountNull: default: accrualFactor = (endDate.getJudianDayNumber()-startDate.getJudianDayNumber())/numDaysInYear; } return accrualFactor; }
date dateUtil::adjustInvalidateDate(date aDate, bool forwardAdjust){ int monthLastDay = getMonthLastDay(aDate.getYear(), aDate.getMonth()); if (aDate.getDay()>monthLastDay){ if (forwardAdjust){ aDate.setMonth(aDate.getMonth()%12+1); aDate.setDay(1); } else{ aDate.setDay(monthLastDay); } } return aDate; }
date dateUtil::adjustInvalidateDate(date aDate, bool forwardAdjust){ unsigned short monthlen[]={31,28,31,30,31,30,31,31,30,31,30,31}; if (isleapyear(aDate.getYear()) && aDate.getMonth()==2) monthlen[1]++; if (aDate.getDay()>monthlen[aDate.getMonth()-1]){ if (forwardAdjust){ aDate.setMonth(aDate.getMonth()%12+1); aDate.setDay(1); } else aDate.setDay(monthlen[aDate.getMonth()-1]); } return aDate; }