Beispiel #1
0
 private LocalTimePattern(string patternText, NodaFormatInfo formatInfo, LocalTime templateValue, IPartialPattern <LocalTime> pattern)
 {
     PatternText       = patternText;
     FormatInfo        = formatInfo;
     TemplateValue     = templateValue;
     UnderlyingPattern = pattern;
 }
Beispiel #2
0
 private AnnualDatePattern(string patternText, NodaFormatInfo formatInfo, AnnualDate templateValue,
                           IPartialPattern <AnnualDate> pattern)
 {
     PatternText       = patternText;
     FormatInfo        = formatInfo;
     TemplateValue     = templateValue;
     UnderlyingPattern = pattern;
 }
Beispiel #3
0
 private YearMonthPattern(string patternText, NodaFormatInfo formatInfo, YearMonth templateValue,
                          IPartialPattern <YearMonth> pattern)
 {
     PatternText       = patternText;
     FormatInfo        = formatInfo;
     TemplateValue     = templateValue;
     UnderlyingPattern = pattern;
 }
        private IPartialPattern <Offset> ParsePartialPattern(string patternText, NodaFormatInfo formatInfo)
        {
            // Nullity check is performed in OffsetPattern.
            if (patternText.Length == 0)
            {
                throw new InvalidPatternException(Messages.Parse_FormatStringEmpty);
            }

            if (patternText.Length == 1)
            {
                switch (patternText)
                {
                case "g":
                    return(CreateGeneralPattern(formatInfo));

                case "G":
                    return(new ZPrefixPattern(CreateGeneralPattern(formatInfo)));

                case "l":
                    patternText = formatInfo.OffsetPatternLong;
                    break;

                case "m":
                    patternText = formatInfo.OffsetPatternMedium;
                    break;

                case "s":
                    patternText = formatInfo.OffsetPatternShort;
                    break;

                default:
                    throw new InvalidPatternException(Messages.Parse_UnknownStandardFormat, patternText, typeof(Offset));
                }
            }
            // This is the only way we'd normally end up in custom parsing land for Z on its own.
            if (patternText == "%Z")
            {
                throw new InvalidPatternException(Messages.Parse_EmptyZPrefixedOffsetPattern);
            }

            // Handle Z-prefix by stripping it, parsing the rest as a normal pattern, then building a special pattern
            // which decides whether or not to delegate.
            bool zPrefix = patternText.StartsWith("Z");

            var patternBuilder = new SteppedPatternBuilder <Offset, OffsetParseBucket>(formatInfo, () => new OffsetParseBucket());

            patternBuilder.ParseCustomPattern(zPrefix ? patternText.Substring(1) : patternText, PatternCharacterHandlers);
            // No need to validate field combinations here, but we do need to do something a bit special
            // for Z-handling.
            IPartialPattern <Offset> pattern = patternBuilder.Build(Offset.FromHoursAndMinutes(5, 30));

            return(zPrefix ? new ZPrefixPattern(pattern) : pattern);
        }
        private IPartialPattern <Offset> ParsePartialPattern(string patternText, NodaFormatInfo formatInfo)
        {
            // Nullity check is performed in OffsetPattern.
            if (patternText.Length == 0)
            {
                throw new InvalidPatternException(Messages.Parse_FormatStringEmpty);
            }

            if (patternText.Length == 1)
            {
                char patternCharacter = patternText[0];
                if (patternCharacter == 'n')
                {
                    return(new NumberPattern(formatInfo));
                }
                if (patternCharacter == 'g')
                {
                    return(CreateGeneralPattern(formatInfo));
                }
                if (patternCharacter == 'G')
                {
                    return(new ZPrefixPattern(CreateGeneralPattern(formatInfo)));
                }
                patternText = ExpandStandardFormatPattern(patternCharacter, formatInfo);
                if (patternText == null)
                {
                    throw new InvalidPatternException(Messages.Parse_UnknownStandardFormat, patternCharacter, typeof(Offset));
                }
            }
            // This is the only way we'd normally end up in custom parsing land for Z on its own.
            if (patternText == "%Z")
            {
                throw new InvalidPatternException(Messages.Parse_EmptyZPrefixedOffsetPattern);
            }

            // Handle Z-prefix by stripping it, parsing the rest as a normal pattern, then building a special pattern
            // which decides whether or not to delegate.
            bool zPrefix = patternText.StartsWith("Z");

            var patternBuilder = new SteppedPatternBuilder <Offset, OffsetParseBucket>(formatInfo, () => new OffsetParseBucket());

            patternBuilder.ParseCustomPattern(zPrefix ? patternText.Substring(1) : patternText, PatternCharacterHandlers);
            // No need to validate field combinations here, but we do need to do something a bit special
            // for Z-handling.
            IPartialPattern <Offset> pattern = patternBuilder.Build();

            return(zPrefix ? new ZPrefixPattern(pattern) : pattern);
        }
Beispiel #6
0
 /// <summary>
 /// Adds parsing/formatting of an embedded pattern, e.g. an offset within a ZonedDateTime/OffsetDateTime.
 /// </summary>
 internal void AddEmbeddedPattern <TEmbedded>(
     IPartialPattern <TEmbedded> embeddedPattern,
     Action <TBucket, TEmbedded> parseAction,
     Func <TResult, TEmbedded> valueExtractor)
 {
     AddParseAction((value, bucket) =>
     {
         var result = embeddedPattern.ParsePartial(value);
         if (!result.Success)
         {
             return(result.ConvertError <TResult>());
         }
         parseAction(bucket, result.Value);
         return(null);
     });
     AddFormatAction((value, sb) => embeddedPattern.AppendFormat(valueExtractor(value), sb));
 }
Beispiel #7
0
 private OffsetPattern(string patternText, IPartialPattern <Offset> pattern)
 {
     this.PatternText       = patternText;
     this.UnderlyingPattern = pattern;
 }
Beispiel #8
0
 private OffsetPattern(string patternText, NodaFormatInfo formatInfo, IPartialPattern <Offset> pattern)
 {
     this.PatternText       = patternText;
     this.FormatInfo        = formatInfo;
     this.UnderlyingPattern = pattern;
 }
        private IPartialPattern <Offset> ParsePartialPattern(string patternText, NodaFormatInfo formatInfo)
        {
            // Nullity check is performed in OffsetPattern.
            if (patternText.Length == 0)
            {
                throw new InvalidPatternException(TextErrorMessages.FormatStringEmpty);
            }

            if (patternText.Length == 1)
            {
                switch (patternText[0])
                {
                case 'g':
                    return(new CompositePatternBuilder <Offset>
                    {
                        { ParsePartialPattern(formatInfo.OffsetPatternLong, formatInfo), offset => true },
                        { ParsePartialPattern(formatInfo.OffsetPatternMedium, formatInfo), HasZeroSeconds },
                        { ParsePartialPattern(formatInfo.OffsetPatternShort, formatInfo), HasZeroSecondsAndMinutes },
                    }.BuildAsPartial());

                case 'G':
                    return(new ZPrefixPattern(ParsePartialPattern("g", formatInfo)));

                case 'i':
                    return(new CompositePatternBuilder <Offset>
                    {
                        { ParsePartialPattern(formatInfo.OffsetPatternLongNoPunctuation, formatInfo), offset => true },
                        { ParsePartialPattern(formatInfo.OffsetPatternMediumNoPunctuation, formatInfo), HasZeroSeconds },
                        { ParsePartialPattern(formatInfo.OffsetPatternShortNoPunctuation, formatInfo), HasZeroSecondsAndMinutes },
                    }.BuildAsPartial());

                case 'I':
                    return(new ZPrefixPattern(ParsePartialPattern("i", formatInfo)));

                case 'l':
                    patternText = formatInfo.OffsetPatternLong;
                    break;

                case 'm':
                    patternText = formatInfo.OffsetPatternMedium;
                    break;

                case 's':
                    patternText = formatInfo.OffsetPatternShort;
                    break;

                case 'L':
                    patternText = formatInfo.OffsetPatternLongNoPunctuation;
                    break;

                case 'M':
                    patternText = formatInfo.OffsetPatternMediumNoPunctuation;
                    break;

                case 'S':
                    patternText = formatInfo.OffsetPatternShortNoPunctuation;
                    break;

                default:
                    throw new InvalidPatternException(TextErrorMessages.UnknownStandardFormat, patternText, typeof(Offset));
                }
            }
            // This is the only way we'd normally end up in custom parsing land for Z on its own.
            if (patternText == "%Z")
            {
                throw new InvalidPatternException(TextErrorMessages.EmptyZPrefixedOffsetPattern);
            }

            // Handle Z-prefix by stripping it, parsing the rest as a normal pattern, then building a special pattern
            // which decides whether or not to delegate.
            bool zPrefix = patternText.StartsWith("Z");

            var patternBuilder = new SteppedPatternBuilder <Offset, OffsetParseBucket>(formatInfo, () => new OffsetParseBucket());

            patternBuilder.ParseCustomPattern(zPrefix ? patternText.Substring(1) : patternText, PatternCharacterHandlers);
            // No need to validate field combinations here, but we do need to do something a bit special
            // for Z-handling.
            IPartialPattern <Offset> pattern = patternBuilder.Build(Offset.FromHoursAndMinutes(5, 30));

            return(zPrefix ? new ZPrefixPattern(pattern) : pattern);
        }
 internal ZPrefixPattern(IPartialPattern <Offset> fullPattern)
 {
     this.fullPattern = fullPattern;
 }