private static void AssertValidNodaPattern(CultureInfo culture, string pattern) { PatternCursor cursor = new PatternCursor(pattern); while (cursor.MoveNext()) { if (cursor.Current == '\'') { cursor.GetQuotedString('\''); } else { // We'll never do anything "special" with non-ascii characters anyway, // so we don't mind if they're not quoted. if (cursor.Current < '\u0080') { Assert.IsTrue(ExpectedCharacters.Contains(cursor.Current), "Pattern '" + pattern + "' contains unquoted, unexpected characters"); } } } // Check that the pattern parses LocalTimePattern.Create(pattern, culture); }
public void Create_NullFormatInfo() { Assert.Throws <ArgumentNullException>(() => LocalTimePattern.Create("HH", null)); }
/// <summary> /// Handles date, time and date/time embedded patterns. /// </summary> internal void AddEmbeddedLocalPartial( PatternCursor pattern, Func <TBucket, LocalDatePatternParser.LocalDateParseBucket> dateBucketExtractor, Func <TBucket, LocalTimePatternParser.LocalTimeParseBucket> timeBucketExtractor, Func <TResult, LocalDate> dateExtractor, Func <TResult, LocalTime> timeExtractor, // null if date/time embedded patterns are invalid Func <TResult, LocalDateTime> dateTimeExtractor) { // This will be d (date-only), t (time-only), or < (date and time) // If it's anything else, we'll see the problem when we try to get the pattern. var patternType = pattern.PeekNext(); if (patternType == 'd' || patternType == 't') { pattern.MoveNext(); } string embeddedPatternText = pattern.GetEmbeddedPattern(); var sampleBucket = CreateSampleBucket(); var templateTime = timeBucketExtractor(sampleBucket).TemplateValue; var templateDate = dateBucketExtractor(sampleBucket).TemplateValue; switch (patternType) { case '<': if (dateTimeExtractor == null) { throw new InvalidPatternException(Messages.Parse_InvalidEmbeddedPatternType); } AddField(PatternFields.EmbeddedDate, 'l'); AddField(PatternFields.EmbeddedTime, 'l'); AddEmbeddedPattern( LocalDateTimePattern.Create(embeddedPatternText, FormatInfo, templateDate + templateTime).UnderlyingPattern, (bucket, value) => { var dateBucket = dateBucketExtractor(bucket); var timeBucket = timeBucketExtractor(bucket); dateBucket.Calendar = value.Calendar; dateBucket.Year = value.Year; dateBucket.MonthOfYearNumeric = value.Month; dateBucket.DayOfMonth = value.Day; timeBucket.Hours24 = value.Hour; timeBucket.Minutes = value.Minute; timeBucket.Seconds = value.Second; timeBucket.FractionalSeconds = value.NanosecondOfSecond; }, dateTimeExtractor); break; case 'd': AddField(PatternFields.EmbeddedDate, 'l'); AddEmbeddedPattern( LocalDatePattern.Create(embeddedPatternText, FormatInfo, templateDate).UnderlyingPattern, (bucket, value) => { var dateBucket = dateBucketExtractor(bucket); dateBucket.Calendar = value.Calendar; dateBucket.Year = value.Year; dateBucket.MonthOfYearNumeric = value.Month; dateBucket.DayOfMonth = value.Day; }, dateExtractor); break; case 't': AddField(PatternFields.EmbeddedTime, 'l'); AddEmbeddedPattern( LocalTimePattern.Create(embeddedPatternText, FormatInfo, templateTime).UnderlyingPattern, (bucket, value) => { var timeBucket = timeBucketExtractor(bucket); timeBucket.Hours24 = value.Hour; timeBucket.Minutes = value.Minute; timeBucket.Seconds = value.Second; timeBucket.FractionalSeconds = value.NanosecondOfSecond; }, timeExtractor); break; default: throw new InvalidOperationException("Bug in Noda Time: embedded pattern type wasn't date, time, or date+time"); } }