Exemple #1
0
        protected override void InitInternal(ICollection <string> locales, DateTimeFormatOptions options)
        {
            Hashtable     ht      = new Hashtable();
            LocaleMatcher matcher = options.LocaleMatcher;

            ht["ca"] = options.Calendar;
            ht["nu"] = options.NumberingSystem;

            bool?     hour12    = options.Hour12;
            HourCycle hourCycle = options.HourCycle;

            if (hour12 != default)
            {
                hourCycle = HourCycle.Unspecified;
            }
            ht["hc"]             = IntlProviderOptions.ToStringValue(hourCycle);
            this.Locale          = ResolveLocale(locales, matcher, relevantExtensionKeys, ht, out ht);
            this.Calendar        = (string)ht["ca"];
            this.NumberingSystem = (string)ht["nu"];

            string tz = options.TimeZone;

            if (tz != null)
            {
                if (!IntlUtility.IsValidTimeZoneName(tz))
                {
                    throw new EcmaRangeErrorException("Invalid time zone specified: {0}", tz);
                }
                this.TimeZone = IntlUtility.CanonicalizeTimeZoneName(tz);
            }
            else
            {
                this.TimeZone = IntlContext.DefaultTimeZone;
            }
            this.FormatMatcher = options.FormatMatcher;

            DateTimePartStyles styles         = new DateTimePartStyles(options);
            HourCycle          finalHourCycle = IntlProviderOptions.ParseEnum <HourCycle>((string)ht["hc"]);

            this.HourCycle = hour12 == default ? finalHourCycle : NormalizeHourCycle(finalHourCycle, hour12.Value);
            this.Hour12    = this.HourCycle == HourCycle.Hour11 || this.HourCycle == HourCycle.Hour12;

            this.format             = GetBestFormat(styles);
            this.calendar           = IntlUtility.SupportedCalendars[this.Calendar];
            this.DateTimePartStyles = format.Styles;
            this.BoundFormat        = Literal.FunctionLiteral(this.FormatInternal);
        }
Exemple #2
0
        private CldrDateTimeFormat GetBestFormat(DateTimePartStyles options)
        {
            string locale = IntlUtility.RemoveUnicodeExtensions(this.Locale);
            ReadOnlyCollection <CldrDateTimeFormat> formats;

            if (options.IsDateOnly)
            {
                formats = CldrCalendarInfo.Resolve(locale, this.Calendar).GetAvailableDateFormats();
            }
            else if (options.IsTimeOnly)
            {
                formats = CldrCalendarInfo.Resolve(locale, "generic").GetAvailableTimeFormats();
            }
            else
            {
                formats = CldrCalendarInfo.Resolve(locale, this.Calendar).GetAvailableDateTimeFormats();
            }
            bool isDateOnly = options.IsDateOnly;
            bool isHour12   = this.Hour12;
            int  bestScore  = Int32.MinValue;
            CldrDateTimeFormat bestFormat = null;

            foreach (CldrDateTimeFormat format in formats)
            {
                if (isDateOnly || isHour12 == format.Styles.IsHour12)
                {
                    int score = format.Styles.Match(options);
                    if (score > bestScore)
                    {
                        bestScore  = score;
                        bestFormat = format;
                    }
                }
            }
            return(bestFormat);
        }
Exemple #3
0
        private FormattedString FormatDateTime(DateTime dt, string patternStr)
        {
            DateTimePartStyles   styles  = this.DateTimePartStyles;
            FormattedString      pattern = parsedPatterns.GetOrAdd(patternStr, FormattedString.Parse);
            List <FormattedPart> parts   = new List <FormattedPart>(pattern);

            if (monthNames == null)
            {
                monthNames     = format.MonthNames;
                dayNames       = format.WeekdayNames;
                dayPeriodNames = format.DayPeriodNames;
                eraNames       = format.EraNames;
            }
            for (int i = parts.Count - 1; i >= 0; i--)
            {
                if (parts[i].Type == FormattedPartType.Placeholder)
                {
                    FormattedPart[] placeable = null;
                    switch (parts[i].Value)
                    {
                    case "{weekday}":
                        placeable = new[] { new FormattedPart(FormattedPartType.Weekday, dayNames[GetPartValue(FormattedPartType.Weekday, dt)]) };
                        break;

                    case "{era}":
                        placeable = new[] { new FormattedPart(FormattedPartType.Era, eraNames[GetPartValue(FormattedPartType.Era, dt)]) };
                        break;

                    case "{year}":
                        int year = GetPartValue(FormattedPartType.Year, dt);
                        placeable = FormatNumber(FormattedPartType.Year, styles.Year == NumericDateTimePartStyle.TwoDigit ? year % 100 : year, this.NumberingSystem, styles.Year == NumericDateTimePartStyle.TwoDigit ? "00" : "0");
                        break;

                    case "{month}":
                        int month = GetPartValue(FormattedPartType.Month, dt);
                        placeable = styles.Month == MonthStyle.Numeric || styles.Month == MonthStyle.TwoDigit
                ? FormatNumber(FormattedPartType.Month, month, this.NumberingSystem, styles.Month == MonthStyle.TwoDigit ? "00" : "0")
                : new[] { new FormattedPart(FormattedPartType.Month, monthNames[month]) };
                        break;

                    case "{day}":
                        placeable = FormatNumber(FormattedPartType.Day, GetPartValue(FormattedPartType.Day, dt), this.NumberingSystem, styles.Day == NumericDateTimePartStyle.TwoDigit ? "00" : "0");
                        break;

                    case "{hour}":
                        placeable = FormatNumber(FormattedPartType.Hour, GetPartValue(FormattedPartType.Hour, dt), this.NumberingSystem, styles.Hour == NumericDateTimePartStyle.TwoDigit ? "00" : "0");
                        break;

                    case "{minute}":
                        placeable = FormatNumber(FormattedPartType.Minute, GetPartValue(FormattedPartType.Minute, dt), this.NumberingSystem, styles.Minute == NumericDateTimePartStyle.TwoDigit ? "00" : "0");
                        break;

                    case "{second}":
                        placeable = FormatNumber(FormattedPartType.Second, GetPartValue(FormattedPartType.Second, dt), this.NumberingSystem, styles.Second == NumericDateTimePartStyle.TwoDigit ? "00" : "0");
                        break;

                    case "{ampm}":
                        placeable = new[] { new FormattedPart(FormattedPartType.DayPeriod, dayPeriodNames[GetPartValue(FormattedPartType.DayPeriod, dt)]) };
                        break;

                    case "{timeZoneName}":
                        placeable = new[] { new FormattedPart(FormattedPartType.TimeZoneName, CldrTimeZoneNames.Resolve(this.Locale, this.TimeZone, dt, styles.TimeZoneName)) };
                        break;
                    }
                    if (placeable != null)
                    {
                        parts.RemoveAt(i);
                        parts.InsertRange(i, placeable);
                    }
                }
            }
            return(new FormattedString(parts));
        }