Skip to content

Commit

Permalink
Version 2 of Nager.Date (#601)
Browse files Browse the repository at this point in the history
Project structure changed, changes in the DateSystem this has been split into two parts, there is now a WeekendSystem and a HolidaySystem. Class renaming,...

The `Date` of a holiday now returns the original date of the holiday if the date on which it is celebrated is needed there is now an additional property `ObservedDate`.
  • Loading branch information
tinohager authored Mar 5, 2024
1 parent 07b2f47 commit 7663de7
Show file tree
Hide file tree
Showing 296 changed files with 22,275 additions and 11,304 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/dotnet.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ jobs:
if: github.event_name != 'pull_request' && github.actor != 'dependabot[bot]'
working-directory: ./src
run: |
sed -i 's/Thank you for supporting open source projects/${{secrets.LICENSE_KEY}}/g' Nager.Date/DateSystem.cs
sed -i 's/Thank you for supporting open source projects/${{secrets.LICENSE_KEY}}/g' Nager.Date/HolidaySystem.cs
sed -i 's/Thank you for supporting open source projects/${{secrets.LICENSE_KEY}}/g' Nager.Date.UnitTest/AssemblyInitialize.cs
- name: Restore dependencies
working-directory: ./src
Expand Down
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,16 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).

## [2.0.0-alpha] - 2024-03-05

### Added
- Add a second date for holiday observed
### Changed
- Move Day finding logic to DateHelper
- Change project structure
### Removed
- Remove bridge day logic part

## [1.48.0] - 2024-02-12

### Added
Expand Down
47 changes: 23 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -181,51 +181,50 @@ PM> install-package Nager.Date

### Set the license key
```cs
DateSystem.LicenseKey = "LicenseKey1234";
HolidaySystem.LicenseKey = "LicenseKey1234";
```

### Get all publicHolidays of a country and year
### Get all holidays of a country and year
```cs
var publicHolidays = DateSystem.GetPublicHolidays(2021, "DE");
foreach (var publicHoliday in publicHolidays)
var holidays = HolidaySystem.GetHolidays(2024, "DE");
foreach (var holiday in holidays)
{
//publicHoliday...
//publicHoliday.Date -> The date
//publicHoliday.LocalName -> The local name
//publicHoliday.Name -> The english name
//publicHoliday.Fixed -> Is this public holiday every year on the same date
//publicHoliday.Global -> Is this public holiday in every county (federal state)
//publicHoliday.Counties -> Is the public holiday only valid for a special county ISO-3166-2 - Federal states
//publicHoliday.Type -> Public, Bank, School, Authorities, Optional, Observance
//holiday...
//holiday.Date -> The date
//holiday.LocalName -> The local name
//holiday.EnglishName -> The english name
//holiday.NationalHoliday -> Is this public holiday in every county (federal state)
//holiday.SubdivisionCodes -> Is the public holiday only valid for a special county ISO-3166-2 - Federal states
//holiday.HolidayTypes -> Public, Bank, School, Authorities, Optional, Observance
}
```

### Get all publicHolidays for a date range
### Get all holidays for a date range
```cs
var startDate = new DateTime(2016, 5, 1);
var endDate = new DateTime(2021, 5, 31);
var publicHolidays = DateSystem.GetPublicHolidays(startDate, endDate, CountryCode.DE);
foreach (var publicHoliday in publicHolidays)
var endDate = new DateTime(2024, 5, 31);
var holidays = HolidaySystem.GetHolidays(startDate, endDate, CountryCode.DE);
foreach (var holiday in holidays)
{
//publicHoliday...
//holiday...
}
```

### Check if a date is a public holiday
```cs
var date = new DateTime(2021, 1, 1);
if (DateSystem.IsPublicHoliday(date, CountryCode.DE))
var date = new DateTime(2024, 1, 1);
if (HolidaySystem.IsPublicHoliday(date, CountryCode.DE))
{
Console.WriteLine("Is public holiday");
Console.WriteLine("Is a public holiday");
}
```

### Check if a date is a weekend day
### Checks if the given date falls on a weekend day
```cs
var date = new DateTime(2021, 1, 1);
if (DateSystem.IsWeekend(date, CountryCode.DE))
var date = new DateTime(2024, 1, 1);
if (WeekendSystem.IsWeekend(date, CountryCode.DE))
{
Console.WriteLine("Is weekend");
Console.WriteLine("The date is in the weekend");
}
```
</details>
Expand Down
45 changes: 22 additions & 23 deletions doc/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Nager.Date

Nager.Date is a popular project to query holidays. We currently support over 100 countries.
Nager.Date is a popular project for querying holidays, we currently support for over 100 countries.

## Country Support
The list of supported countries can be found [here](https://date.nager.at/Country/Coverage)
Expand All @@ -9,45 +9,44 @@ The list of supported countries can be found [here](https://date.nager.at/Countr

### Get all publicHolidays of a country and year
```cs
var publicHolidays = DateSystem.GetPublicHolidays(2021, "DE");
foreach (var publicHoliday in publicHolidays)
var holidays = HolidaySystem.GetHolidays(2024, "DE");
foreach (var holiday in holidays)
{
//publicHoliday...
//publicHoliday.Date -> The date
//publicHoliday.LocalName -> The local name
//publicHoliday.Name -> The english name
//publicHoliday.Fixed -> Is this public holiday every year on the same date
//publicHoliday.Global -> Is this public holiday in every county (federal state)
//publicHoliday.Counties -> Is the public holiday only valid for a special county ISO-3166-2 - Federal states
//publicHoliday.Type -> Public, Bank, School, Authorities, Optional, Observance
//holiday...
//holiday.Date -> The date
//holiday.LocalName -> The local name
//holiday.EnglishName -> The english name
//holiday.NationalHoliday -> Is this holiday in every county (federal state)
//holiday.SubdivisionCodes -> Is the public holiday only valid for a special county ISO-3166-2 - Federal states
//holiday.HolidayTypes -> Public, Bank, School, Authorities, Optional, Observance
}
```

### Get all publicHolidays for a date range
### Get all holidays for a date range
```cs
var startDate = new DateTime(2016, 5, 1);
var endDate = new DateTime(2021, 5, 31);
var publicHolidays = DateSystem.GetPublicHolidays(startDate, endDate, CountryCode.DE);
foreach (var publicHoliday in publicHolidays)
var endDate = new DateTime(2024, 5, 31);
var holidays = HolidaySystem.GetHolidays(startDate, endDate, CountryCode.DE);
foreach (var holiday in holidays)
{
//publicHoliday...
//holiday...
}
```

### Check if a date is a public holiday
```cs
var date = new DateTime(2021, 1, 1);
if (DateSystem.IsPublicHoliday(date, CountryCode.DE))
var date = new DateTime(2024, 1, 1);
if (HolidaySystem.IsPublicHoliday(date, CountryCode.DE))
{
Console.WriteLine("Is public holiday");
Console.WriteLine("Is a public holiday");
}
```

### Check if a date is a weekend day
### Checks if the given date falls on a weekend day
```cs
var date = new DateTime(2021, 1, 1);
if (DateSystem.IsWeekend(date, CountryCode.DE))
var date = new DateTime(2024, 1, 1);
if (WeekendSystem.IsWeekend(date, CountryCode.DE))
{
Console.WriteLine("Is weekend");
Console.WriteLine("The date is in the weekend");
}
```
11 changes: 6 additions & 5 deletions src/Nager.Date.Console/Program.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Nager.Date;

DateSystem.LicenseKey = "Thank you for supporting open source projects";
HolidaySystem.LicenseKey = "Thank you for supporting open source projects";

Console.WriteLine("Nager.Date");

Expand All @@ -19,11 +19,12 @@
}

Console.WriteLine($"Calculate holidays for {countryCode.ToUpper()} {year}");
Console.WriteLine("-------------------------------------------------------");
Console.WriteLine("--------------------------------------------------------------------------------------------------");
Console.WriteLine("Date Observed English Name Local Name Type");

var publicHolidays = DateSystem.GetPublicHolidays(year, countryCode);
var publicHolidays = HolidaySystem.GetHolidays(year, countryCode);
foreach (var publicHoliday in publicHolidays)
{
var counties = publicHoliday.Counties != null ? string.Join(',', publicHoliday.Counties) : "";
Console.WriteLine($"{publicHoliday.Date:d}{"",3}{publicHoliday.Name,30}{"",3}{publicHoliday.LocalName,30}{"",3}{publicHoliday.Type}{"",3}{counties}");
var counties = publicHoliday.SubdivisionCodes != null ? string.Join(',', publicHoliday.SubdivisionCodes) : "";
Console.WriteLine($"{publicHoliday.Date:d}{"",3}{publicHoliday.ObservedDate:d}{"",3}{publicHoliday.EnglishName,30}{"",3}{publicHoliday.LocalName,30}{"",3}{publicHoliday.HolidayTypes}{"",3}{counties}");
}
2 changes: 1 addition & 1 deletion src/Nager.Date.UnitTest/AssemblyInitialize.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ internal class AssemblyInitialize
[AssemblyInitialize]
public static void MyTestInitialize(TestContext testContext)
{
DateSystem.LicenseKey = "Thank you for supporting open source projects";
HolidaySystem.LicenseKey = "Thank you for supporting open source projects";
}
}
}
24 changes: 12 additions & 12 deletions src/Nager.Date.UnitTest/Common/CountryTest.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Nager.Date.Contract;
using Nager.Date.HolidayProviders;
using System;
using System.Collections.Generic;
using System.Linq;
Expand All @@ -14,7 +14,7 @@ public void CheckCountries()
{
foreach (CountryCode countryCode in Enum.GetValues(typeof(CountryCode)))
{
var provider = DateSystem.GetPublicHolidayProvider(countryCode);
var provider = HolidaySystem.GetHolidayProvider(countryCode);

var publicHolidays = provider.GetHolidays(2018);
if (!publicHolidays.Any())
Expand All @@ -36,28 +36,28 @@ public void CheckCounties()

foreach (CountryCode countryCode in Enum.GetValues(typeof(CountryCode)))
{
var provider = DateSystem.GetPublicHolidayProvider(countryCode);
var counties = provider is ICountyProvider countyProvider
? countyProvider.GetCounties()
var provider = HolidaySystem.GetHolidayProvider(countryCode);
var subdivisionCodes = provider is ISubdivisionCodesProvider subdivisionCodesProvider
? subdivisionCodesProvider.GetSubdivisionCodes()
: new Dictionary<string, string>();

var startYear = DateTime.Today.Year - 100;
var endYear = DateTime.Today.Year + 100;

for (var year = startYear; year <= endYear; year++)
{
var publicHolidays = DateSystem.GetPublicHolidays(year, countryCode);
var publicHolidays = HolidaySystem.GetHolidays(year, countryCode);
foreach (var publicHoliday in publicHolidays)
{
if (publicHoliday.Counties == null)
if (publicHoliday.SubdivisionCodes == null)
{
continue;
}

if (publicHoliday.Counties.Count(o => counties.Keys.Contains(o)) != publicHoliday.Counties.Length)
if (publicHoliday.SubdivisionCodes.Count(o => subdivisionCodes.Keys.Contains(o)) != publicHoliday.SubdivisionCodes.Length)
{
var diff = publicHoliday.Counties.Except(counties.Keys);
failures.Add($"Unknown county in {provider} \"{publicHoliday.Name}\" {string.Join(',', diff)}");
var diff = publicHoliday.SubdivisionCodes.Except(subdivisionCodes.Keys);
failures.Add($"Unknown subdivisionCode in {provider} \"{publicHoliday.EnglishName}\" {string.Join(',', diff)}");
}
}
}
Expand All @@ -77,15 +77,15 @@ public void CheckCounties()
[DataRow("DE")]
public void CheckCaseInsensitive(string countryCode)
{
var result = DateSystem.GetPublicHolidays(2018, countryCode);
var result = HolidaySystem.GetHolidays(2018, countryCode);

Assert.IsNotNull(result);
}

[TestMethod]
public void ThrowOnUndefinedEnum()
{
Assert.ThrowsException<ArgumentException>(() => DateSystem.GetPublicHolidays(2018, "1000"));
Assert.ThrowsException<ArgumentException>(() => HolidaySystem.GetHolidays(2018, "1000"));
}
}
}
Loading

0 comments on commit 7663de7

Please sign in to comment.