/// <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> public DateToken Parse(Match match, IFormatProvider provider) { if (match == null) { throw new ArgumentNullException("match"); } if (!match.Success) { throw new FormatException(); } DateToken dateToken = this.ParseInternal(match, provider); if (!dateToken.IsValid) { throw new FormatException(); } return(dateToken); }
/// <summary> /// Initializes a new instance of the <see cref="DateTimeToken"/> class. /// </summary> /// <param name="dateToken">The date part of an instant in time.</param> /// <param name="timeToken">The time part of an instant in time.</param> public DateTimeToken(DateToken dateToken, TimeToken timeToken) { this.DateToken = dateToken; this.TimeToken = timeToken; }
/// <summary> /// Initializes a new instance of the <see cref="PatternDefinition"/> class. /// </summary> /// <param name="dateTokenParser">The <see cref="DateToken.Parser"/> for the date token part of the /// pattern.</param> /// <param name="timeTokenParser">The <see cref="TimeToken.Parser"/> for the time token part of the /// pattern.</param> /// <param name="pattern">The regular expression that matches a <see cref="DateTimeToken"/>.</param> public PatternDefinition(DateToken.Parser dateTokenParser, TimeToken.Parser timeTokenParser, string pattern) { this.DateTokenParser = dateTokenParser; this.TimeTokenParser = timeTokenParser; this.Pattern = pattern; }
/// <summary> /// Initializes a new instance of the <see cref="DateTimeToken"/> class. /// </summary> /// <param name="dateToken">The date part of an instant in time.</param> /// <param name="timeToken">The time part of an instant in time.</param> public DateTimeToken(DateToken dateToken, TimeToken timeToken) { this.DateToken = dateToken; this.TimeToken = timeToken; }
/// <summary> /// Returns a list of <see cref="PatternDefinition"/> objects representing the patterns supported by the /// combination of the specified <see cref="DateToken.Parser"/> and <see cref="TimeToken.Parser"/>. /// </summary> /// <param name="dateTokenParser">A <see cref="DateToken.Parser"/>.</param> /// <param name="timeTokenParser">A <see cref="TimeToken.Parser"/>.</param> /// <param name="provider">The <see cref="IFormatProvider"/> that will be used when parsing.</param> /// <returns>A list of <see cref="PatternDefinition"/> objects.</returns> private static List<PatternDefinition> GetDateTimePatternDefinitions(DateToken.Parser dateTokenParser, TimeToken.Parser timeTokenParser, IFormatProvider provider) { if (!dateTokenParser.IsCompatibleWith(timeTokenParser) || !timeTokenParser.IsCompatibleWith(dateTokenParser)) { return new List<PatternDefinition>(); } List<PatternDefinition> list = new List<PatternDefinition>(); foreach (string datePartPattern in dateTokenParser.GetPatterns(provider)) { foreach (string timePartPattern in timeTokenParser.GetPatterns(provider)) { string dateTimePattern = GetDateTimePattern(datePartPattern, timePartPattern, provider); list.Add(new PatternDefinition(dateTokenParser, timeTokenParser, dateTimePattern)); string timeDatePattern = GetTimeDatePattern(timePartPattern, datePartPattern, provider); list.Add(new PatternDefinition(dateTokenParser, timeTokenParser, timeDatePattern)); } } return list; }
/// <summary> /// Asserts that a <see cref="DateToken"/> is an instance of the <see cref="EmptyDateToken"/> class. /// </summary> /// <param name="actual">The actual <see cref="DateToken"/>.</param> private static void AssertIsEmpty(DateToken actual) { Assert.AreEqual(typeof(EmptyDateToken), actual.GetType()); }
/// <summary> /// Asserts that a <see cref="DateToken"/> is an instance of the <see cref="SpecialDateToken"/> class and that /// its value is equal to the specified parameters. /// </summary> /// <param name="actual">The actual <see cref="DateToken"/>.</param> /// <param name="expectedSpecialDate">The expected <see cref="SpecialDate"/>.</param> private static void AssertAreEqual(DateToken actual, SpecialDate expectedSpecialDate) { Assert.AreEqual(typeof(SpecialDateToken), actual.GetType()); Assert.AreEqual(expectedSpecialDate, ((SpecialDateToken)actual).SpecialDate); }
/// <summary> /// Asserts that a <see cref="DateToken"/> is an instance of the <see cref="RelativeDateToken"/> class and that /// its value is equal to the specified parameters. /// </summary> /// <param name="actual">The actual <see cref="DateToken"/>.</param> /// <param name="expectedRelativeDate">The expected <see cref="RelativeDate"/>.</param> private static void AssertAreEqual(DateToken actual, RelativeDate expectedRelativeDate) { Assert.AreEqual(typeof(RelativeDateToken), actual.GetType()); Assert.AreEqual(expectedRelativeDate, ((RelativeDateToken)actual).RelativeDate); }
/// <summary> /// Asserts that a <see cref="DateToken"/> is an instance of the <see cref="NormalDateToken"/> class and that /// its value is equal to the specified parameters. /// </summary> /// <param name="actual">The actual <see cref="DateToken"/>.</param> /// <param name="expectedYear">The expected year.</param> /// <param name="expectedMonth">The expected month.</param> /// <param name="expectedDay">The expected day.</param> private static void AssertAreEqual(DateToken actual, int? expectedYear = null, int? expectedMonth = null, int? expectedDay = null) { Assert.AreEqual(typeof(NormalDateToken), actual.GetType()); Assert.AreEqual(expectedYear, ((NormalDateToken)actual).Year); Assert.AreEqual(expectedMonth, ((NormalDateToken)actual).Month); Assert.AreEqual(expectedDay, ((NormalDateToken)actual).Day); }
/// <summary> /// Asserts that a <see cref="DateToken"/> is an instance of the <see cref="DayOfWeekDateToken"/> class and /// that its value is equal to the specified parameters. /// </summary> /// <param name="actual">The actual <see cref="DateToken"/>.</param> /// <param name="expectedDayOfWeek">The expected day of week.</param> /// <param name="expectedDayOfWeekRelation">The expected relation between the day of week and date.</param> private static void AssertAreEqual(DateToken actual, DayOfWeek? expectedDayOfWeek, DayOfWeekRelation? expectedDayOfWeekRelation) { Assert.AreEqual(typeof(DayOfWeekDateToken), actual.GetType()); Assert.AreEqual(expectedDayOfWeek, ((DayOfWeekDateToken)actual).DayOfWeek); Assert.AreEqual(expectedDayOfWeekRelation, ((DayOfWeekDateToken)actual).DayOfWeekRelation); }
/// <summary> /// Returns a value indicating whether this parser can be used in conjunction with a specified <see /// cref="DateToken.Parser"/>. /// </summary> /// <param name="dateTokenParser">A <see cref="DateToken.Parser"/>.</param> /// <returns>A value indicating whether this parser can be used in conjunction with the specified <see /// cref="DateToken.Parser"/>.</returns> public virtual bool IsCompatibleWith(DateToken.Parser dateTokenParser) { return true; }
/// <summary> /// Returns a value indicating whether this parser can be used in conjunction with a specified <see /// cref="DateToken.Parser"/>. /// </summary> /// <param name="dateTokenParser">A <see cref="DateToken.Parser"/>.</param> /// <returns>A value indicating whether this parser can be used in conjunction with the specified <see /// cref="DateToken.Parser"/>.</returns> public override bool IsCompatibleWith(DateToken.Parser dateTokenParser) { return !(dateTokenParser is EmptyDateToken.Parser); }