/// <summary>Gets the start and end date of the timeframe. /// </summary> /// <param name="referenceDate">A reference date (can be a business day or a holiday).</param> /// <param name="holidayCalendar">The (settlement) holiday calendar.</param> /// <param name="fixingLag">The fixing lag, i.e. a method used to calculate the fixing date with respect to the period. Will be applied to the IMM Date (even if the IMM date is not a business day).</param> /// <param name="startDate">The start date of the time span with respect to <paramref name="referenceDate"/> (output).</param> /// <param name="endDate">The end date of the time span with respect to <paramref name="referenceDate"/> (output).</param> /// <param name="tenor">The tenor that represents the time span; if the end date of the current object is not specified by its <see cref="TenorTimeSpan"/> representation, <paramref name="tenorRoundingRule"/> /// will be applied to <paramref name="startDate"/> and <paramref name="endDate"/> for the calculation of a <see cref="TenorTimeSpan"/> representation.</param> /// <param name="tenorRoundingRule"> A rounding rule which will be take into account if and only if the end date of the current object is not specified by its <see cref="TenorTimeSpan"/> representation.</param> /// <param name="logger">An optional logger.</param> public DateTime GetStartAndEndDate(DateTime referenceDate, IHolidayCalendar holidayCalendar, IFixingLag fixingLag, out DateTime startDate, out DateTime endDate, out TenorTimeSpan tenor, TenorTimeSpan.RoundingRule tenorRoundingRule = TenorTimeSpan.RoundingRule.NearestMonth, ILogger logger = null) { tenor = Tenor; return(GetStartAndEndDate(referenceDate, holidayCalendar, fixingLag, out startDate, out endDate, logger)); }
/// <summary>Gets a <see cref="TenorTimeSpan"/> representation of a specific time span. /// </summary> /// <param name="startDate">The start date of the time span.</param> /// <param name="endDate">The end date of the time span.</param> /// <param name="roundingRule">A rounding rule to take into account. Often one is interest in a year and month representation only.</param> /// <returns>A <see cref="TenorTimeSpan"/> representation of the time span between <paramref name="startDate"/> and <paramref name="endDate"/> where /// years and months are taken into account only.</returns> public static TenorTimeSpan GetTimeSpanInBetween(DateTime startDate, DateTime endDate, TenorTimeSpan.RoundingRule roundingRule = RoundingRule.Exact) { int sign = (endDate >= startDate) ? 1 : -1; /* 1.) compute the number of years, i.e. adjust the start date: */ int numberOfYears = endDate.Year - startDate.Year; var tempStartDate = startDate.AddYears(numberOfYears); if ((sign == 1) && (tempStartDate > endDate)) { numberOfYears = numberOfYears - 1; } else if ((sign == -1) && (tempStartDate < endDate)) { numberOfYears = numberOfYears + 1; } startDate = startDate.AddYears(numberOfYears); /* 2.) compute the number of months, i.e. adjust the start date: */ int numberOfMonths = endDate.Month - startDate.Month; if (sign * startDate.Month > sign * endDate.Month) { numberOfMonths += sign * 12; } tempStartDate = startDate.AddMonths(numberOfMonths); if ((sign == 1) && (tempStartDate > endDate)) { numberOfMonths = Math.Max(0, numberOfMonths - 1); } else if ((sign == -1) && (tempStartDate < endDate)) { numberOfMonths = Math.Min(12, numberOfMonths + 1); } startDate = startDate.AddMonths(numberOfMonths); /* 3.) compute the number of days and apply the 'rounding rule': */ int numberOfDays = endDate.Subtract(startDate).Days; switch (roundingRule) { case RoundingRule.NearestMonth: if (Math.Abs(numberOfDays) > 15) { return(new TenorTimeSpan(numberOfYears, numberOfMonths + sign, 0)); } return(new TenorTimeSpan(numberOfYears, numberOfMonths, 0)); case RoundingRule.Exact: return(new TenorTimeSpan(numberOfYears, numberOfMonths, numberOfDays)); case RoundingRule.NearestWeek: int r = Math.Abs(numberOfDays) % 7; if (r <= 3) { return(new TenorTimeSpan(numberOfYears, numberOfMonths, 7 * (numberOfDays / 7))); } else { return(new TenorTimeSpan(numberOfYears, numberOfMonths, 7 * (numberOfDays / 7) + sign * 7)); } default: throw new NotImplementedException(); } }
/// <summary>Gets the start and end date of the timeframe. /// </summary> /// <param name="referenceDate">A reference date (can be a business day or a holiday).</param> /// <param name="holidayCalendar">The (settlement) holiday calendar.</param> /// <param name="startDate">The start date of the time span with respect to <paramref name="referenceDate"/> (output).</param> /// <param name="endDate">The end date of the time span with respect to <paramref name="referenceDate"/> (output).</param> /// <param name="tenor">The tenor that represents the time span; if the end date of the current object is not specified by its <see cref="TenorTimeSpan"/> representation, <paramref name="tenorRoundingRule"/> /// will be applied to <paramref name="startDate"/> and <paramref name="endDate"/> for the calculation of a <see cref="TenorTimeSpan"/> representation.</param> /// <param name="tenorRoundingRule"> A rounding rule which will be take into account if and only if the end date of the current object is not specified by its <see cref="TenorTimeSpan"/> representation.</param> /// <param name="logger">An optional logger.</param> public void GetStartAndEndDate(DateTime referenceDate, IHolidayCalendar holidayCalendar, out DateTime startDate, out DateTime endDate, out TenorTimeSpan tenor, TenorTimeSpan.RoundingRule tenorRoundingRule = TenorTimeSpan.RoundingRule.NearestMonth, ILogger logger = null) { tenor = Tenor; GetStartAndEndDate(referenceDate, holidayCalendar, out startDate, out endDate, logger); }
public TenorTimeSpan GetTimeSpanInBetween_TestCase(DateTime startDate, DateTime endDate, TenorTimeSpan.RoundingRule roundingRule) { return(TenorTimeSpan.GetTimeSpanInBetween(startDate, endDate, roundingRule)); }