public object?Convert(object?[]?values, Type targetType, object?parameter, CultureInfo culture) { long ticks = long.MaxValue; foreach (var value in values ?? Enumerable.Empty <object?>()) { if (value is DateTime dt) { ticks = dt.Ticks; } else if (value is XmlLanguage language) { culture = language.GetSpecificCulture(); } } if (ticks == long.MaxValue) { return(null); } try { return(new DateTime(ticks).ToString(CalendarFormatInfo.FromCultureInfo(culture).YearMonthPattern, culture)); } catch (ArgumentOutOfRangeException) { return(new DateTime(ticks).ToString("Y", CultureInfo.InvariantCulture)); } }
public void TestFromCultureInfo(string cultureName, string yearMonth, string componentThree, string componentTwo, string componentOne) { var result = CalendarFormatInfo.FromCultureInfo(CultureInfo.GetCultureInfo(cultureName)); Assert.Equal(yearMonth, result.YearMonthPattern); Assert.Equal(componentOne, result.ComponentOnePattern); Assert.Equal(componentTwo, result.ComponentTwoPattern); Assert.Equal(componentThree, result.ComponentThreePattern); }
public void SettingYearPatternOfMultupleCultures() { string[] cultureNames = { "en-001", "en-150" }; CalendarFormatInfo.SetYearPattern(cultureNames, "B"); foreach (var cultureName in cultureNames) { var result = CalendarFormatInfo.FromCultureInfo(CultureInfo.GetCultureInfo(cultureName)); Assert.Equal("B", result.ComponentThreePattern); } }
public void SettingDayOfWeekStyleOfMultupleCultures() { string[] cultureNames = { "en-001", "en-150" }; CalendarFormatInfo.SetDayOfWeekStyle(cultureNames, new CalendarFormatInfo.DayOfWeekStyle("Z", "@", true)); foreach (var cultureName in cultureNames) { var result = CalendarFormatInfo.FromCultureInfo(CultureInfo.GetCultureInfo(cultureName)); Assert.Equal("d MMM", result.ComponentOnePattern); Assert.Equal("Z@", result.ComponentTwoPattern); } }
public void ResettingDayOfWeekStyle() { const string cultureName = "en-001"; CalendarFormatInfo.SetDayOfWeekStyle(cultureName, new CalendarFormatInfo.DayOfWeekStyle("Z", "@", true)); CalendarFormatInfo.ResetDayOfWeekStyle(cultureName); var result = CalendarFormatInfo.FromCultureInfo(CultureInfo.GetCultureInfo(cultureName)); Assert.Equal("d MMM", result.ComponentOnePattern); Assert.Equal("ddd,", result.ComponentTwoPattern); }
public void SettingYearPattern() { const string cultureName = "en-001"; CalendarFormatInfo.SetYearPattern(cultureName, "A"); var result = CalendarFormatInfo.FromCultureInfo(CultureInfo.GetCultureInfo(cultureName)); Assert.Equal("A", result.ComponentThreePattern); CalendarFormatInfo.SetYearPattern(cultureName, null); result = CalendarFormatInfo.FromCultureInfo(CultureInfo.GetCultureInfo(cultureName)); Assert.Equal("yyyy", result.ComponentThreePattern); }
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture) { long ticks = long.MaxValue; foreach (var value in values) { if (value is DateTime dt) { ticks = dt.Ticks; } else if (value is XmlLanguage language) { culture = language.GetSpecificCulture(); } } if (ticks == long.MaxValue) { return(null); } return(new DateTime(ticks).ToString(CalendarFormatInfo.FromCultureInfo(culture).YearMonthPattern, culture)); }