private static string GetMetaZoneName(string locale, string metaZone, TimeZoneNameStyle style, bool isDaylightSaving) { XElement metazoneNames = timeZones.XPathSelectElement(String.Format("/root/timeZoneNames[@locale = '{0}']/*[(local-name() = 'metazone' or local-name() = 'zone') and @type = '{1}']", locale, metaZone)); if (metazoneNames != null) { if (style == TimeZoneNameStyle.Short) { metazoneNames = metazoneNames.Element("short") ?? metazoneNames.Element("long"); } else { metazoneNames = metazoneNames.Element("long"); } XElement name = metazoneNames.Element(isDaylightSaving ? "daylight" : "standard"); if (name != null) { return(name.Value); } } return(CldrUtility.GetParentPatterns(locale, s => GetMetaZoneName(s, metaZone, style, isDaylightSaving))); }
private static string ParseDateTimeFormat(string format, out DateTimePartStyles style) { WeekdayStyle weekday = default; EraStyle era = default; MonthStyle month = default; NumericDateTimePartStyle year = default; NumericDateTimePartStyle day = default; NumericDateTimePartStyle hour = default; NumericDateTimePartStyle minute = default; NumericDateTimePartStyle second = default; TimeZoneNameStyle timeZoneName = default; bool isHour12 = false; string pattern = Regex.Replace(format, "('([^']*)'|(?<d>[yMLdGEcabBhHkKmszv])(\\k<d>)*)", m => { switch (m.Value[0]) { case 'y': year = m.Length == 2 ? NumericDateTimePartStyle.TwoDigit : NumericDateTimePartStyle.Numeric; return("{year}"); case 'M': case 'L': switch (m.Length) { case 1: month = MonthStyle.Numeric; break; case 2: month = MonthStyle.TwoDigit; break; case 3: month = MonthStyle.Short; break; case 4: month = MonthStyle.Long; break; case 5: month = MonthStyle.Narrow; break; } return("{month}"); case 'd': day = m.Length == 2 ? NumericDateTimePartStyle.TwoDigit : NumericDateTimePartStyle.Numeric; return("{day}"); case 'E': case 'c': weekday = m.Length == 2 ? WeekdayStyle.Short : m.Length == 3 ? WeekdayStyle.Long : WeekdayStyle.Narrow; return("{weekday}"); case 'G': era = EraStyle.Long; return("{era}"); case 'a': case 'b': case 'B': isHour12 = true; return("{ampm}"); case 'h': case 'H': case 'k': case 'K': hour = m.Length == 2 ? NumericDateTimePartStyle.TwoDigit : NumericDateTimePartStyle.Numeric; return("{hour}"); case 'm': minute = m.Length == 2 ? NumericDateTimePartStyle.TwoDigit : NumericDateTimePartStyle.Numeric; return("{minute}"); case 's': second = m.Length == 2 ? NumericDateTimePartStyle.TwoDigit : NumericDateTimePartStyle.Numeric; return("{second}"); case 'z': case 'v': timeZoneName = m.Length == 1 ? TimeZoneNameStyle.Short : TimeZoneNameStyle.Long; return("{timeZoneName}"); } return(m.Length == 2 ? "'" : m.Groups[1].Value); }); style = new DateTimePartStyles(weekday, era, year, month, day, hour, minute, second, timeZoneName, isHour12); return(pattern); }
public static string Resolve(string locale, string ianaTimeZone, DateTime date, TimeZoneNameStyle style) { if (ianaTimeZone == "UTC" || ianaTimeZone == "Etc/UTC") { return(GetMetaZoneName(locale, "Etc/UTC", style, false)); } TimeZoneInfo info = TimeZoneConverter.TZConvert.GetTimeZoneInfo(ianaTimeZone); XElement metaZone = metaZones.XPathSelectElement(String.Format("/supplementalData/metaZones/metazoneInfo/timezone[@type = '{0}']", ianaTimeZone)); if (metaZone != null) { IEnumerable <XElement> child = metaZone.Elements("usesMetazone"); XElement usesMetazone = child.First(v => (v.Attribute("from") == null || date >= DateTime.Parse(v.Attribute("from").Value)) && (v.Attribute("to") == null || date < DateTime.Parse(v.Attribute("to").Value))); return(GetMetaZoneName(locale, usesMetazone.Attribute("mzone").Value, style, info.IsDaylightSavingTime(date))); } string hourFormat = GetHourFormat(locale); string format = hourFormat.Split(';')[info.BaseUtcOffset >= TimeSpan.Zero ? 0 : 1]; return(Regex.Replace(format, "H+|m+", m => { if (m.Value[0] == 'm') { return info.BaseUtcOffset.Minutes.ToString("00", CultureInfo.InvariantCulture); } return info.BaseUtcOffset.Hours.ToString(m.Length == 2 ? "00" : "0", CultureInfo.InvariantCulture); })); }
public DateTimePartStyles(WeekdayStyle weekday, EraStyle era, NumericDateTimePartStyle year, MonthStyle month, NumericDateTimePartStyle day, NumericDateTimePartStyle hour, NumericDateTimePartStyle minute, NumericDateTimePartStyle second, TimeZoneNameStyle timeZoneName, bool isHour12) { int value = 0; value |= (int)weekday << 0; value |= (int)era << 3; value |= (int)year << 6; value |= (int)month << 9; value |= (int)day << 12; value |= (int)hour << 15; value |= (int)minute << 18; value |= (int)second << 21; value |= (int)timeZoneName << 24; value |= isHour12 ? 1 << 27 : 0; this.value = value; }