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);
        }