Skip to content

Commit

Permalink
Merge pull request #208 from HERN1k/ua_culture
Browse files Browse the repository at this point in the history
added ua culture
  • Loading branch information
joaomatossilva authored Nov 2, 2024
2 parents 2bc97a1 + 00f540c commit 6bf1022
Show file tree
Hide file tree
Showing 4 changed files with 360 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
}
83 changes: 83 additions & 0 deletions tests/DateTimeExtensions.Tests/UAHolidaysTests.cs
Original file line number Diff line number Diff line change
@@ -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()

This comment has been minimized.

Copy link
@angel66909

angel66909 Nov 3, 2024

En

{
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));
}
}
}
93 changes: 93 additions & 0 deletions tests/DateTimeExtensions.Tests/UANaturalTimeTests.cs
Original file line number Diff line number Diff line change
@@ -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);
}
}
}

0 comments on commit 6bf1022

Please sign in to comment.