/// <summary> /// Parses a <see cref="Match"/> into a <see cref="DateToken"/>. /// </summary> /// <param name="match">A <see cref="Match"/> representation of a <see cref="DateToken"/>.</param> /// <param name="provider">An <see cref="IFormatProvider"/>.</param> /// <returns>The <see cref="DateToken"/> parsed from the <see cref="Match"/>.</returns> /// <exception cref="ArgumentNullException">If <paramref name="match"/> or <paramref name="provider"/> is /// <c>null</c>.</exception> /// <exception cref="FormatException">If the <paramref name="match"/> is not a supported representation of /// a <see cref="DateToken"/>.</exception> protected override DateToken ParseInternal(Match match, IFormatProvider provider) { RelativeDateDefinition relativeDateDefinition = GetRelativeDateDefinitionForMatch(match); if (relativeDateDefinition == null) { throw new FormatException(); } return(new RelativeDateToken { RelativeDate = relativeDateDefinition.RelativeDate }); }
/// <summary> /// Returns the next date after <paramref name="minDate"/> that is represented by this token. /// </summary> /// <remarks> /// This method may return a date that is before <paramref name="minDate"/> if there is no date after <paramref /// name="minDate"/> that is represented by this token. /// </remarks> /// <param name="minDate">The minimum date to return. The time part is ignored.</param> /// <param name="inclusive">A value indicating whether the returned date should be on or after rather than /// strictly after <paramref name="minDate"/>.</param> /// <returns>The next date after <paramref name="minDate"/> that is represented by this token.</returns> /// <exception cref="InvalidOperationException">If this token is not valid.</exception> public override DateTime ToDateTime(DateTime minDate, bool inclusive) { this.ThrowIfNotValid(); RelativeDateDefinition relativeDateDefinition = this.GetRelativeDateDefinition(); DateTime date = minDate.Date; date = date.AddDays(relativeDateDefinition.DayDelta); date = date.AddMonths(relativeDateDefinition.MonthDelta); date = date.AddYears(relativeDateDefinition.YearDelta); return(date); }
/// <summary> /// Returns a string that represents the current object. /// </summary> /// <param name="provider">An <see cref="IFormatProvider"/> to use.</param> /// <returns>A string that represents the current object.</returns> public override string ToString(IFormatProvider provider) { try { this.ThrowIfNotValid(); RelativeDateDefinition relativeDateDefinition = this.GetRelativeDateDefinition(); return(relativeDateDefinition.GetName(provider)); } catch { return(this.GetType().ToString()); } }