コード例 #1
0
        private ArchiveDatePoint ParsePoint(string text, bool dmy,
                                            DateMonthStyle monthStyle)
        {
            // senza data or s.d.
            if (_sineDataRegex.IsMatch(text))
            {
                return(null);
            }

            // century
            if (text.IndexOf("secolo", StringComparison.OrdinalIgnoreCase) > -1 ||
                text.IndexOf("sec.", StringComparison.CurrentCultureIgnoreCase) > -1)
            {
                return(ParseCenturyPoint(text));
            }

            // decade
            Match match = _decadeRegex.Match(text);

            if (match.Success)
            {
                return(ParseDecade(match.Groups[1].Value));
            }

            // DMY/YMD
            ArchiveDatePoint point = new ArchiveDatePoint();

            // about
            if (_aboutRegex.IsMatch(text))
            {
                point.Approximation = ApproximationType.About;
            }

            int   i = (int)(monthStyle == 0 ? 0 : monthStyle - 1);
            Regex r = dmy ? _dmyRegexes[i] : _ymdRegexes[i];

            match = r.Match(text);
            if (!match.Success)
            {
                return(null);
            }

            YmdToken y = YmdToken.Parse(match.Groups["y"].Value, 'y');
            YmdToken m = YmdToken.Parse(match.Groups["m"].Value, 'm');
            YmdToken d = YmdToken.Parse(match.Groups["d"].Value, 'd');

            // take square brackets into account: this depends on the DMY/YMD order
            MarkInferred(dmy ? new[] { d, m, y } : new[] { y, m, d });

            point.Value          = y.Value;
            point.IsYearInferred = y.IsInferred;

            if (m != null)
            {
                point.Month           = m.Value;
                point.IsMonthInferred = m.IsInferred;
            }

            if (d != null)
            {
                point.Day           = d.Value;
                point.IsDayInferred = d.IsInferred;
            }

            return(point);
        }
コード例 #2
0
        private void AdjustDmyForShortenedRange(Tuple <string, string> input,
                                                ArchiveDate date, DateMonthStyle monthStyle)
        {
            if (date.A != null || date.B?.ValueType != DateValueType.Year ||
                date.B.Month == 0)
            {
                return;
            }

            int   i     = (int)(monthStyle == 0 ? 0 : monthStyle - 1);
            Match match = _shortenedDmyRegexes[i].Match(input.Item1);

            if (match.Success)
            {
                // copy YM from Max
                date.A = new ArchiveDatePoint
                {
                    ValueType       = DateValueType.Year,
                    Value           = date.B.Value,
                    IsYearInferred  = date.B.IsYearInferred,
                    Month           = date.B.Month,
                    IsMonthInferred = date.B.IsMonthInferred
                };

                // style N has groups d/m or md
                if (i == 0)
                {
                    if (match.Groups["md"].Length > 0)
                    {
                        if (date.B.Day > 0)
                        {
                            date.A.Day = short.Parse(match.Groups["md"].Value,
                                                     CultureInfo.InvariantCulture);
                        }
                        else
                        {
                            date.A.Month = short.Parse(match.Groups["md"].Value,
                                                       CultureInfo.InvariantCulture);
                        }
                    }
                    else
                    {
                        date.A.Month = short.Parse(match.Groups["m"].Value,
                                                   CultureInfo.InvariantCulture);
                        date.A.Day = short.Parse(match.Groups["d"].Value,
                                                 CultureInfo.InvariantCulture);
                    }
                } // N
                else
                {
                    // styles S/F have groups m/d
                    if (match.Groups["m"].Length > 0)
                    {
                        YmdToken m = YmdToken.Parse(match.Groups["m"].Value, 'm');
                        date.A.Month = m.Value;
                    }
                    if (match.Groups["d"].Length > 0)
                    {
                        YmdToken d = YmdToken.Parse(match.Groups["d"].Value, 'd');
                        date.A.Day = d.Value;
                    }
                } // !N
            }
        }
コード例 #3
0
        /// <summary>
        /// Parses the specified text representing an YMD token, eventually
        /// surrounded by an opening and/or closing square bracket.
        /// </summary>
        /// <param name="text">The text.</param>
        /// <param name="type">The type of token to parse: <c>y</c>, <c>m</c>,
        /// <c>d</c>.</param>
        /// <returns>parsed token or null if invalid</returns>
        /// <exception cref="ArgumentNullException">null text</exception>
        public static YmdToken Parse(string text, char type)
        {
            if (text == null)
            {
                throw new ArgumentNullException(nameof(text));
            }

            text = text.Trim().ToLowerInvariant();
            Match m = _bracketedRegex.Match(text);

            if (!m.Success)
            {
                return(null);
            }

            YmdToken token = new YmdToken {
                Type = type
            };

            if (m.Groups["l"].Length > 0)
            {
                token.HasLeftBracket = true;
            }
            if (m.Groups["r"].Length > 0)
            {
                token.HasRightBracket = true;
            }
            string sValue = m.Groups["v"].Value.ToLowerInvariant();

            switch (char.ToLowerInvariant(type))
            {
            case 'y':
                token.Value = short.Parse(sValue, CultureInfo.InvariantCulture);
                break;

            case 'm':
                if (sValue.Any(char.IsLetter))
                {
                    short n = GetValueFromFullMonth(sValue);
                    if (n > 0)
                    {
                        token.Value = n;
                    }
                    else
                    {
                        string s = sValue.EndsWith(".", StringComparison.Ordinal)
                                ? sValue
                                : sValue + ".";
                        n = GetValueFromShortMonth(s);
                        if (n > 0)
                        {
                            token.Value = n;
                        }
                    }
                }
                else
                {
                    token.Value = short.Parse(sValue, CultureInfo.InvariantCulture);
                }
                break;

            case 'd':
                goto case 'y';
            }

            return(token);
        }