From 00f540cbc10ae243c3fb5e1ea365dccedca1ae20 Mon Sep 17 00:00:00 2001 From: Vlad Hirnyk Date: Sat, 2 Nov 2024 13:58:20 +0200 Subject: [PATCH] added ua culture --- .../UK_UANaturalTimeStrategy.cs | 63 +++++++++ .../CultureStrategies/UK_UAHolidayStrategy.cs | 121 ++++++++++++++++++ .../UAHolidaysTests.cs | 83 ++++++++++++ .../UANaturalTimeTests.cs | 93 ++++++++++++++ 4 files changed, 360 insertions(+) create mode 100644 src/DateTimeExtensions/NaturalText/CultureStrategies/UK_UANaturalTimeStrategy.cs create mode 100644 src/DateTimeExtensions/WorkingDays/CultureStrategies/UK_UAHolidayStrategy.cs create mode 100644 tests/DateTimeExtensions.Tests/UAHolidaysTests.cs create mode 100644 tests/DateTimeExtensions.Tests/UANaturalTimeTests.cs diff --git a/src/DateTimeExtensions/NaturalText/CultureStrategies/UK_UANaturalTimeStrategy.cs b/src/DateTimeExtensions/NaturalText/CultureStrategies/UK_UANaturalTimeStrategy.cs new file mode 100644 index 0000000..f84a08a --- /dev/null +++ b/src/DateTimeExtensions/NaturalText/CultureStrategies/UK_UANaturalTimeStrategy.cs @@ -0,0 +1,63 @@ +using DateTimeExtensions.Common; + +namespace DateTimeExtensions.NaturalText.CultureStrategies +{ + [Locale("uk-UA")] + public class UK_UANaturalTimeStrategy : NaturalTimeStrategyBase + { + protected override string YearText + { + get { return "рік"; } + } + + protected override string MonthText + { + get { return "місяць"; } + } + + protected override string DayText + { + get { return "день"; } + } + + protected override string HourText + { + get { return "година"; } + } + + protected override string MinuteText + { + get { return "хвилина"; } + } + + protected override string SecondText + { + get { return "секунда"; } + } + + protected override string Pluralize(string text, int value) + { + if (text.EndsWith("рік")) + { + return "роки"; + } + + if (text.EndsWith("день")) + { + return "дні"; + } + + if (text.EndsWith("ь")) + { + return text.Substring(0, text.Length - 1) + "і"; + } + + if (text.EndsWith("а")) + { + return text.Substring(0, text.Length - 1) + "и"; + } + + return text; + } + } +} \ No newline at end of file diff --git a/src/DateTimeExtensions/WorkingDays/CultureStrategies/UK_UAHolidayStrategy.cs b/src/DateTimeExtensions/WorkingDays/CultureStrategies/UK_UAHolidayStrategy.cs new file mode 100644 index 0000000..7e22335 --- /dev/null +++ b/src/DateTimeExtensions/WorkingDays/CultureStrategies/UK_UAHolidayStrategy.cs @@ -0,0 +1,121 @@ +using DateTimeExtensions.Common; + +namespace DateTimeExtensions.WorkingDays.CultureStrategies +{ + [Locale("uk-UA")] + public class UK_UAHolidayStrategy : HolidayStrategyBase, IHolidayStrategy + { + public UK_UAHolidayStrategy() + { + this.InnerHolidays.Add(GlobalHolidays.NewYear); + this.InnerHolidays.Add(WomensDay); + this.InnerHolidays.Add(ChristianHolidays.Easter); + this.InnerHolidays.Add(WorkersDay); + this.InnerHolidays.Add(VictoryInEuropeDay); + this.InnerHolidays.Add(ChristianHolidays.Pentecost); + this.InnerHolidays.Add(ConstitutionDay); + this.InnerHolidays.Add(StatehoodDay); + this.InnerHolidays.Add(IndependenceDayOfUkraine); + this.InnerHolidays.Add(DefendersDay); + this.InnerHolidays.Add(ChristianHolidays.Christmas); + } + + private static Holiday womensDay; + + public static Holiday WomensDay + { + get + { + if (womensDay == null) + { + womensDay = new FixedHoliday("International Women's Day", 3, 8); + } + return womensDay; + } + } + + private static Holiday workersDay; + + public static Holiday WorkersDay + { + get + { + if (workersDay == null) + { + workersDay = new FixedHoliday("International Workers' Day", 5, 1); + } + return workersDay; + } + } + + private static Holiday victoryInEuropeDay; + + public static Holiday VictoryInEuropeDay + { + get + { + if (victoryInEuropeDay == null) + { + victoryInEuropeDay = new FixedHoliday("Victory in Europe Day", 5, 8); + } + return victoryInEuropeDay; + } + } + + private static Holiday constitutionDay; + + public static Holiday ConstitutionDay + { + get + { + if (constitutionDay == null) + { + constitutionDay = new FixedHoliday("Constitution Day", 6, 28); + } + return constitutionDay; + } + } + + private static Holiday statehoodDay; + + public static Holiday StatehoodDay + { + get + { + if (statehoodDay == null) + { + statehoodDay = new FixedHoliday("Statehood Day", 7, 15); + } + return statehoodDay; + } + } + + private static Holiday independenceDayOfUkraine; + + public static Holiday IndependenceDayOfUkraine + { + get + { + if (independenceDayOfUkraine == null) + { + independenceDayOfUkraine = new FixedHoliday("Independence Day of Ukraine", 8, 24); + } + return independenceDayOfUkraine; + } + } + + private static Holiday defendersDay; + + public static Holiday DefendersDay + { + get + { + if (defendersDay == null) + { + defendersDay = new FixedHoliday("Defenders Day", 10, 1); + } + return defendersDay; + } + } + } +} \ No newline at end of file diff --git a/tests/DateTimeExtensions.Tests/UAHolidaysTests.cs b/tests/DateTimeExtensions.Tests/UAHolidaysTests.cs new file mode 100644 index 0000000..e2d09f2 --- /dev/null +++ b/tests/DateTimeExtensions.Tests/UAHolidaysTests.cs @@ -0,0 +1,83 @@ +using System; +using System.IO; +using System.Linq; + +using DateTimeExtensions.Export; +using DateTimeExtensions.WorkingDays; +using DateTimeExtensions.WorkingDays.CultureStrategies; + +using NUnit.Framework; + +#pragma warning disable NUnit2005 + +namespace DateTimeExtensions.Tests +{ + [TestFixture] + public class UAHolidaysTests + { + private WorkingDayCultureInfo dateTimeCulture = new WorkingDayCultureInfo("uk-UA"); + + [Test] + public void UA_has_11_main_holidays() + { + Assert.AreEqual(11, dateTimeCulture.Holidays.Count()); + } + + [Test] + public void Easter2017_is_16_april() + { + Assert.That(new DateTime(2017, 4, 16).IsHoliday(dateTimeCulture)); + } + + [Test] + public void Easter2018_is_1_april() + { + Assert.That(new DateTime(2018, 4, 1).IsHoliday(dateTimeCulture)); + } + + [Test] + public void Test2() + { + var holidaysStrategy = dateTimeCulture.LocateHolidayStrategy(dateTimeCulture.Name, null); + + Assert.AreEqual(typeof(UK_UAHolidayStrategy), holidaysStrategy.GetType()); + var workingDaysStrategy = dateTimeCulture.LocateWorkingDayOfWeekStrategy(dateTimeCulture.Name, null); + Assert.AreEqual(typeof(DefaultWorkingDayOfWeekStrategy), workingDaysStrategy.GetType()); + } + + [Test] + public void can_identify_VictoryInEuropeDay() + { + var holiday = UK_UAHolidayStrategy.VictoryInEuropeDay; + var day = new DateTime(2024, 5, 8); + TestHoliday(holiday, day); + } + + [Test] + public void test_export() + { + var easter = ChristianHolidays.Easter; + var easter_1800 = easter.GetInstance(1800); + var easter_2017 = easter.GetInstance(2017); + var easter_2018 = easter.GetInstance(2018); + var easter_2022 = easter.GetInstance(2022); + var easter_2023 = easter.GetInstance(2023); + var easter_2024 = easter.GetInstance(2024); + var easter_2025 = easter.GetInstance(2025); + + TextWriter textwriter = new StringWriter(); + var exporter = ExportHolidayFormatLocator.LocateByType(ExportType.OfficeHolidays); + exporter.Export(dateTimeCulture, 2024, textwriter); + var s = textwriter.ToString(); + Assert.NotNull(s); + } + + private void TestHoliday(Holiday holiday, DateTime dateOnGregorian) + { + var holidayInstance = holiday.GetInstance(dateOnGregorian.Year); + Assert.True(holidayInstance.HasValue); + Assert.AreEqual(dateOnGregorian, holidayInstance.Value); + Assert.True(holiday.IsInstanceOf(dateOnGregorian)); + } + } +} \ No newline at end of file diff --git a/tests/DateTimeExtensions.Tests/UANaturalTimeTests.cs b/tests/DateTimeExtensions.Tests/UANaturalTimeTests.cs new file mode 100644 index 0000000..23a35ce --- /dev/null +++ b/tests/DateTimeExtensions.Tests/UANaturalTimeTests.cs @@ -0,0 +1,93 @@ +using System; + +using DateTimeExtensions.NaturalText; + +using NUnit.Framework; + +#pragma warning disable NUnit2005 + +namespace DateTimeExtensions.Tests +{ + [TestFixture] + public class UK_UANaturalTimeTests + { + private NaturalTextCultureInfo ua_ci = new NaturalTextCultureInfo("uk-UA"); + private DateTime fromTime = new DateTime(2024, 5, 8, 9, 41, 0); + + [Test] + public void can_tranlate_to_natural_text() + { + var toTime = fromTime.AddHours(2).AddMinutes(45); + + var naturalText = fromTime.ToNaturalText(toTime, false, ua_ci); + + Assert.IsNotNull(naturalText); + Assert.IsNotEmpty(naturalText); + Assert.AreEqual("2 години", naturalText); + } + + [Test] + public void can_tranlate_to_natural_text_rounded() + { + var toTime = fromTime.AddHours(2).AddMinutes(45); + + var naturalText = fromTime.ToNaturalText(toTime, true, ua_ci); + + Assert.IsNotNull(naturalText); + Assert.IsNotEmpty(naturalText); + Assert.AreEqual("3 години", naturalText); + } + + [Test] + public void can_tranlate_to_exact_natural_text_full() + { + var toTime = fromTime + .AddSeconds(6) + .AddMinutes(5) + .AddHours(4) + .AddDays(3) + .AddMonths(2) + .AddYears(2); + + var naturalText = fromTime.ToExactNaturalText(toTime, ua_ci); + + Assert.NotNull(naturalText); + Assert.IsNotEmpty(naturalText); + Assert.AreEqual("2 роки, 2 місяці, 3 дні, 4 години, 5 хвилини, 6 секунди", naturalText); + } + + [Test] + public void can_pluralize_years() + { + var toTime_plural = fromTime.AddYears(2); + var toTime_single = fromTime.AddYears(1); + + var naturalText_plural = fromTime.ToNaturalText(toTime_plural, true, ua_ci); + var naturalText_single = fromTime.ToNaturalText(toTime_single, true, ua_ci); + + Assert.IsNotNull(naturalText_plural); + Assert.IsNotEmpty(naturalText_plural); + Assert.IsNotNull(naturalText_single); + Assert.IsNotEmpty(naturalText_single); + Assert.AreEqual("2 роки", naturalText_plural); + Assert.AreEqual("1 рік", naturalText_single); + } + + [Test] + public void can_pluralize_months() + { + var toTime_plural = fromTime.AddMonths(2); + var toTime_single = fromTime.AddMonths(1); + + var naturalText_plural = fromTime.ToNaturalText(toTime_plural, true, ua_ci); + var naturalText_single = fromTime.ToNaturalText(toTime_single, true, ua_ci); + + Assert.IsNotNull(naturalText_plural); + Assert.IsNotEmpty(naturalText_plural); + Assert.IsNotNull(naturalText_single); + Assert.IsNotEmpty(naturalText_single); + Assert.AreEqual("2 місяці", naturalText_plural); + Assert.AreEqual("1 місяць", naturalText_single); + } + } +} \ No newline at end of file