Public Holidays
C# Worldwide holiday calculation !
Introduction
For many years, I have been searching C# libs that handle public holidays of all countries. I've never found a project that satisfied me and I decided to make one that I wanted.
Today, I would like to share with you a set of libraries that I've made.
Background
In order to define a public holiday, there are so many rules.
Such as :
- Fixed Date
- Fixed Date at the beginning of a Month
- Movable Date
- Different start-time for Fixed Date other than midnight
- Different duration other than 24 hours
- Start-time of holiday changes per weekday
- Changes to different weekday from a given fixed Date
- Changes to different weekday from a given Month
- Change to a different weekday from a changed fixed Date
- Change to different weekday if date falls on a certain weekday
- Substitute a holiday if date falls on a certain weekday
- Observe the holiday as well as on a substitute day, if date falls on a certain weekday
- Enable Date only for odd/ even numbered years
- Enable Date only for certain periods of years
- Enable Date only for certain weekdays
- Enabling a rule since or in certain years
- Disabling a rule
- Moving a date
- Disabling a rule in states/ regions
So it's a little big complex, and I love that!
Using the Code
Get All Supported Countries
using ShaNetHoliday.Engine.Standard;
using ShaNetHoliday.Models;
var countriesAvailable = HolidaySystem.Instance.CountriesAvailable;
foreach (Country country in countriesAvailable)
{
//Do something...
}
Get SpecificDays for a country
using ShaNetHoliday.Engine.Standard;
using ShaNetHoliday.Models;
var specificDays = HolidaySystem.Instance.All(2019, "FR", RuleType.All);
foreach (SpecificDay day in specificDays)
{
//Do something...
}
Get SpecificDays for a Country Between Two Dates
using ShaNetHoliday.Engine.Standard;
using ShaNetHoliday.Models;
var specificDays = HolidaySystem.Instance.Between
(On.January.The1st, On.June.The30th,"FR", RuleType.All);
foreach (SpecificDay day in specificDays)
{
//Do something...
}
Check if Date Is Specific Day for a Country
using ShaNetHoliday.Engine.Standard;
using ShaNetHoliday.Models;
var specificDay = HolidaySystem.Instance.Single(On.January.The1st,"FR", RuleType.All);
if (specificDay != null)
//Do something...
Get Long WeekEnds for a Country
using ShaNetHoliday.Engine.Standard;
using ShaNetHoliday.Models;
var longWeekEnds = HolidaySystem.Instance.LongWeekEnds(2019,"FR");
foreach (LongWeekEnd longWeekEnd in longWeekEnds)
{
//Do something...
}
Get SpecificDays for a Country and State
using ShaNetHoliday.Engine.Standard;
using ShaNetHoliday.Models;
var specificDays = HolidaySystem.Instance.All(2019, "FR", "MQ", RuleType.All);
foreach (SpecificDay day in specificDays)
{
//Do something...
}
Get SpecificDays for a Country and State Between Two Dates
using ShaNetHoliday.Engine.Standard;
using ShaNetHoliday.Models;
specificDays = HolidaySystem.Instance.Between
(On.January.The1st, On.June.The30th, "FR", "MQ", RuleType.All);
foreach (SpecificDay day in specificDays)
{
//Do something...
}
Check if Date is Specific Day for a Country and State
using ShaNetHoliday.Engine.Standard;
using ShaNetHoliday.Models;
var specificDay = HolidaySystem.Instance.Single(On.January.The1st,"FR", "MQ", RuleType.All);
if (specificDay != null)
//Do something...
Get Long WeekEnds for a Country and State
using ShaNetHoliday.Engine.Standard;
using ShaNetHoliday.Models;
var longWeekEnds = HolidaySystem.Instance.LongWeekEnds(2019, "FR", "MQ");
foreach (LongWeekEnd longWeekEnd in longWeekEnds)
{
//Do something...
}
Points of Interest
Here, you can only see how to use the system, but what's behind the scenes!
In this set of libs, you can declare a country, a state of country and a region of state.
In each of country, state and region, you can add Rules (GregorianRule
, JulianRule
, HijriRule
, HebrewRule
and ChineseRule
).
All rules have an expression, and you can declare this in the most human style possible. This can be possible because I've created so many classes for that.
An example, in GB, the 1st January by Royal Proclamation, see one of the substitutes below if 1 January falls on Saturday or Sunday (https://en.wikipedia.org/wiki/Public_holidays_in_the_United_Kingdom).
So with my libs, you can declare this expression:
ExpressionTree.Substitute.Fix(On.January.The1st).If(Saturday).
Then.Next(Monday).Or.If(Sunday).Then.Next(Monday)
You can see all supported countries at this link:
Actually, my project supports 63 countries, you can participate if you find this useful.
The real benefit of this project resides in the creation of complex rules with coolest syntax and no date manipulation during rule's creation!
NuGet
PM> Install-Package ShaNetHoliday.Engine.Standard
https://www.nuget.org/packages/ShaNetHoliday.Engine.Standard/
Resources
- List of all rules that I've already included in libs: https://github.com/Shaenn/ShaNetHolidayResult/wiki/Rules
- List of all rules calculated for 2019: https://github.com/Shaenn/ShaNetHolidayResult/wiki/2019
- I've created an API in Azure for testing only: https://shanetholiday.azurewebsites.net/countries (you have all other queries available in readMe in https://github.com/Shaenn/ShaNetHoliday)
- Main project is here: https://github.com/Shaenn/ShaNetHoliday
- Wiki project is here: https://github.com/Shaenn/ShaNetHolidayResult/wiki
Supports
- 63 countries supported
- Expression for date in Gregorian, Julian, Hijri, Hebrew and Chinese (Solar & Lunar) calendar
- All countries, states, regions codes are ISO-3166-1 (alpha 2 and 3)
- All languages code are ISO 639-1
- Rule types for Public, Bank, School, Optional, and Observance
Thank's for reading, I hope this will help you!