Esempio n. 1
0
 private static string GetRegexFromStringLiteral(string str, IRegexBuilderHook hook)
 {
     if (hook != null)
     {
         return(hook.GetRegexFromStringLiteral(str));
     }
     return(Regex.Escape(str));
 }
Esempio n. 2
0
        static ParsedDateTimeFormat?ParseStandardDateTimeFormatHelper(string formatString, CultureInfo culture, IRegexBuilderHook hook)
        {
            if (formatString.Length != 1)
            {
                return(null);
            }

            switch (formatString[0])
            {
            case 'd':
                return(ParseCustomDateTimeFormat(culture.DateTimeFormat.ShortDatePattern, culture, hook));

            case 'D':
                return(ParseCustomDateTimeFormat(culture.DateTimeFormat.LongDatePattern, culture, hook));

            case 'f':
                return(ParseCustomDateTimeFormat(culture.DateTimeFormat.LongDatePattern + " " + culture.DateTimeFormat.ShortTimePattern, culture, hook));

            case 'F':
                return(ParseCustomDateTimeFormat(culture.DateTimeFormat.FullDateTimePattern, culture, hook));

            case 'g':
                return(ParseCustomDateTimeFormat(culture.DateTimeFormat.ShortDatePattern + " " + culture.DateTimeFormat.ShortTimePattern, culture, hook));

            case 'G':
                return(ParseCustomDateTimeFormat(culture.DateTimeFormat.ShortDatePattern + " " + culture.DateTimeFormat.LongTimePattern, culture, hook));

            case 'M':
            case 'm':
                return(ParseCustomDateTimeFormat(culture.DateTimeFormat.MonthDayPattern, culture, hook));

            case 'O':
            case 'o':
                return(ParseCustomDateTimeFormat(@"yyyy\-MM\-dd\THH\:mm\:ss\.fffffffK", culture, hook));

            case 'R':
            case 'r':
                return(ParseCustomDateTimeFormat(culture.DateTimeFormat.RFC1123Pattern, CultureInfo.InvariantCulture, hook));

            case 's':
                return(ParseCustomDateTimeFormat(culture.DateTimeFormat.SortableDateTimePattern, CultureInfo.InvariantCulture, hook));

            case 't':
                return(ParseCustomDateTimeFormat(culture.DateTimeFormat.ShortTimePattern, culture, hook));

            case 'T':
                return(ParseCustomDateTimeFormat(culture.DateTimeFormat.LongTimePattern, culture, hook));

            case 'u':
                return(ParseCustomDateTimeFormat(culture.DateTimeFormat.UniversalSortableDateTimePattern, CultureInfo.InvariantCulture, hook));

            case 'U':
                return(ParseCustomDateTimeFormat(culture.DateTimeFormat.FullDateTimePattern, culture, hook));

            case 'Y':
            case 'y':
                return(ParseCustomDateTimeFormat(culture.DateTimeFormat.YearMonthPattern, culture, hook));

            default:
                return(null);
            }
        }
Esempio n. 3
0
        static ParsedDateTimeFormat?ParseStandardDateTimeFormat(string formatString, CultureInfo culture, IRegexBuilderHook hook)
        {
            var ret = ParseStandardDateTimeFormatHelper(formatString, culture, hook);

            if (ret == null)
            {
                return(ret);
            }
            var val = ret.Value;

            val.Flags |= DateTimeFormatFlag.IsCultureDependent;
            return(val);
        }
Esempio n. 4
0
        public static bool TryParseStandardDateTimeFormat(string formatString, CultureInfo culture, out ParsedDateTimeFormat fmt, IRegexBuilderHook hook = null)
        {
            if (culture == null)
            {
                throw new ArgumentNullException("culture");
            }
            if (formatString == null)
            {
                throw new ArgumentNullException("formatString");
            }

            var tmp = ParseStandardDateTimeFormat(formatString, culture, hook);

            if (tmp != null)
            {
                fmt = tmp.Value;
                return(true);
            }
            else
            {
                fmt = new ParsedDateTimeFormat();
                return(false);
            }
        }
Esempio n. 5
0
        public static ParsedDateTimeFormat ParseCustomDateTimeFormat(string formatString, CultureInfo culture, IRegexBuilderHook hook = null)
        {
            if (formatString == null)
            {
                throw new ArgumentNullException("formatString");
            }

            DateTimeFormatFlag flags = DateTimeFormatFlag.None;
            StringBuilder      re    = new StringBuilder();

            re.AppendLine();
            foreach (var t in TokenizeCustomDatePattern(formatString))
            {
                if (!t.IsSpecifier)
                {
                    re.AppendFormat("  {0} # fixed string '{1}'{2}", GetRegexFromStringLiteral(t.Value, hook), t.Value, Environment.NewLine);
                }
                else
                {
                    switch (t.Value)
                    {
                    case "d":
                        re.AppendLine(@"  \d{1,2} # day of the month");
                        flags |= DateTimeFormatFlag.ContainsDay;
                        break;

                    case "dd":
                        re.AppendLine(@"  \d{2} # day of the month");
                        flags |= DateTimeFormatFlag.ContainsDay;
                        break;

                    case "ddd":
                        flags |= DateTimeFormatFlag.ContainsDay;
                        flags |= DateTimeFormatFlag.IsCultureDependent;
                        if (culture != null)
                        {
                            AppendMatcherForOneOf(re, culture.DateTimeFormat.AbbreviatedDayNames, hook);
                        }
                        else
                        {
                            re.Append(@"  \w+");
                        }
                        re.AppendLine(" # abbreviated name of the day of the week");
                        break;

                    case "dddd":
                        flags |= DateTimeFormatFlag.ContainsDay;
                        flags |= DateTimeFormatFlag.IsCultureDependent;
                        if (culture != null)
                        {
                            AppendMatcherForOneOf(re, culture.DateTimeFormat.DayNames, hook);
                        }
                        else
                        {
                            re.Append(@"  \w+");
                        }
                        re.AppendLine(" #  full name of the day of the week");
                        break;

                    case "f":
                    case "ff":
                    case "fff":
                    case "ffff":
                    case "fffff":
                    case "ffffff":
                    case "fffffff":
                        re.AppendFormat(@"  \d{0}{1}{2} # the most significant digits of the seconds fraction{3}", "{", t.Value.Length, "}", Environment.NewLine);
                        flags |= DateTimeFormatFlag.ContainsSecondFraction;
                        break;

                    case "F":
                    case "FF":
                    case "FFF":
                    case "FFFF":
                    case "FFFFF":
                    case "FFFFFF":
                    case "FFFFFFF":
                        re.AppendFormat(@"  (\d{0}{1}{2})? # the most significant digits of the seconds fraction (no trailing zeros){3}", "{", t.Value.Length, "}", Environment.NewLine);
                        flags |= DateTimeFormatFlag.ContainsSecondFraction;
                        break;

                    case "g":
                    case "gg":
                        flags |= DateTimeFormatFlag.IsCultureDependent;
                        if (culture != null)
                        {
                            AppendMatcherForOneOf(re, culture.Calendar.Eras.Select(era => culture.DateTimeFormat.GetAbbreviatedEraName(era)), hook);
                        }
                        else
                        {
                            re.Append(@"  .+");
                        }
                        re.AppendLine(@" # the era");
                        break;

                    case "h":
                    case "H":
                        re.AppendLine(@"  \d{1,2} # hours");
                        flags |= DateTimeFormatFlag.ContainsHour;
                        break;

                    case "hh":
                    case "HH":
                        re.AppendLine(@"  \d{2} # hours");
                        flags |= DateTimeFormatFlag.ContainsHour;
                        break;

                    case "m":
                        re.AppendLine(@"  \d{1,2} # minutes");
                        flags |= DateTimeFormatFlag.ContainsMinutes;
                        break;

                    case "mm":
                        re.AppendLine(@"  \d{2} # minutes");
                        flags |= DateTimeFormatFlag.ContainsMinutes;
                        break;

                    case "M":
                        re.AppendLine(@"  \d{1,2} # month");
                        flags |= DateTimeFormatFlag.ContainsMonth;
                        break;

                    case "MM":
                        re.AppendLine(@"  \d{2} # month");
                        flags |= DateTimeFormatFlag.ContainsMonth;
                        break;

                    case "MMM":
                        flags |= DateTimeFormatFlag.ContainsMonth;
                        flags |= DateTimeFormatFlag.IsCultureDependent;
                        if (culture != null)
                        {
                            AppendMatcherForOneOf(re, culture.DateTimeFormat.AbbreviatedMonthGenitiveNames.Union(culture.DateTimeFormat.AbbreviatedMonthNames).Where(s => s != "").Distinct(), hook);
                        }
                        else
                        {
                            re.Append(@"  \w+");
                        }
                        re.AppendLine(@" # abbreviated name of the month");
                        break;

                    case "MMMM":
                        flags |= DateTimeFormatFlag.ContainsMonth;
                        flags |= DateTimeFormatFlag.IsCultureDependent;
                        if (culture != null)
                        {
                            AppendMatcherForOneOf(re, culture.DateTimeFormat.MonthGenitiveNames.Union(culture.DateTimeFormat.MonthNames).Where(s => s != "").Distinct(), hook);
                        }
                        else
                        {
                            re.Append(@"  \w+");
                        }
                        re.AppendLine(@" # full name of the month");
                        break;

                    case "s":
                        re.AppendLine(@"  \d{1,2} # seconds");
                        flags |= DateTimeFormatFlag.ContainsSeconds;
                        break;

                    case "ss":
                        re.AppendLine(@"  \d{2} # seconds");
                        flags |= DateTimeFormatFlag.ContainsSeconds;
                        break;

                    case "t":
                        flags |= DateTimeFormatFlag.IsCultureDependent;
                        if (culture != null)
                        {
                            if (culture.DateTimeFormat.AMDesignator.Length > 0 && culture.DateTimeFormat.PMDesignator.Length > 0)
                            {
                                re.AppendFormat(@"  ({0}|{1})", culture.DateTimeFormat.AMDesignator[0], culture.DateTimeFormat.PMDesignator[0]);
                            }
                        }
                        else
                        {
                            re.Append(@"  \w?");
                        }
                        re.AppendLine(@" # the first character of the A.M./P.M. designator");
                        break;

                    case "tt":
                        flags |= DateTimeFormatFlag.IsCultureDependent;
                        if (culture != null)
                        {
                            AppendMatcherForOneOf(re, new string[] { culture.DateTimeFormat.AMDesignator, culture.DateTimeFormat.PMDesignator }, hook);
                        }
                        else
                        {
                            re.Append(@"  \w+");
                        }
                        re.AppendLine(@" # A.M./P.M. designator");
                        break;

                    case "y":
                        re.AppendLine(@"  \d{1,2} # year");
                        flags |= DateTimeFormatFlag.ContainsYear;
                        break;

                    case "yy":
                        re.AppendLine(@"  \d{2} # year");
                        flags |= DateTimeFormatFlag.ContainsYear;
                        break;

                    case "yyyy":
                        re.AppendLine(@"  \d{4} # year");
                        flags |= DateTimeFormatFlag.ContainsYear;
                        break;

                    case "z":
                        re.AppendLine(@"  [\+\-]\d{1,2} # time zone offset");
                        flags |= DateTimeFormatFlag.ContainsTimeZone;
                        break;

                    case "zz":
                        re.AppendLine(@"  [\+\-]\d{2} # time zone offset");
                        flags |= DateTimeFormatFlag.ContainsTimeZone;
                        break;

                    case "zzz":
                        re.AppendLine(@"  [\+\-]\d{2}\:\d{2} # time zone offset");
                        flags |= DateTimeFormatFlag.ContainsTimeZone;
                        break;

                    case "K":
                        re.AppendLine(@"  ([\+\-]\d{2}\:\d{2})? # time zone information");
                        flags |= DateTimeFormatFlag.ContainsTimeZone;
                        break;

                    case ":":
                        flags |= DateTimeFormatFlag.IsCultureDependent;
                        if (culture != null)
                        {
                            re.AppendFormat(@"  ({0})", GetRegexFromStringLiteral(culture.DateTimeFormat.TimeSeparator, hook));
                        }
                        else
                        {
                            re.Append(@"  .");
                        }
                        re.AppendLine(@" # time separator");
                        break;

                    case "/":
                        flags |= DateTimeFormatFlag.IsCultureDependent;
                        if (culture != null)
                        {
                            re.AppendFormat(@"  ({0})", GetRegexFromStringLiteral(culture.DateTimeFormat.DateSeparator, hook));
                        }
                        else
                        {
                            re.Append(@"  .");
                        }
                        re.AppendLine(@" # date separator");
                        break;
                    }
                }
            }
            return(new ParsedDateTimeFormat()
            {
                Regex = re.ToString(), Flags = flags
            });
        }
Esempio n. 6
0
 private static StringBuilder AppendMatcherForOneOf(StringBuilder regexBuilder, IEnumerable <string> options, IRegexBuilderHook hook)
 {
     return(regexBuilder.AppendFormat(@"  ({0})", options.Select(n => "(" + GetRegexFromStringLiteral(n, hook) + ")").Aggregate((ret, n) => ret + "|" + n)));
 }
Esempio n. 7
0
        public static ParsedDateTimeFormat ParseDateTimeFormat(string formatString, CultureInfo culture, IRegexBuilderHook hook = null)
        {
            if (formatString == null)
            {
                throw new ArgumentNullException("formatString");
            }
            if (culture == null)
            {
                throw new ArgumentNullException("culture");
            }

            ParsedDateTimeFormat?parsedStdFormat = ParseStandardDateTimeFormat(formatString, culture, hook);

            if (parsedStdFormat.HasValue)
            {
                return(parsedStdFormat.Value);
            }

            return(ParseCustomDateTimeFormat(formatString, culture, hook));
        }