Click here to Skip to main content
15,867,686 members
Articles / Programming Languages / C#
Tip/Trick

Public Holidays

Rate me:
Please Sign up or sign in to vote.
5.00/5 (7 votes)
13 Feb 2019CPOL3 min read 15.3K   10   4
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

C#
using ShaNetHoliday.Engine.Standard;
using ShaNetHoliday.Models;

var countriesAvailable = HolidaySystem.Instance.CountriesAvailable;
foreach (Country country in countriesAvailable)
{
	//Do something...
}

Get SpecificDays for a country

C#
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

C#
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

C#
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

C#
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

C#
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

C#
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

C#
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

C#
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:

C#
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

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!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
France France
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionExpired Pin
Mohsin Azam24-Feb-21 4:10
Mohsin Azam24-Feb-21 4:10 
QuestionStill Available? Pin
Jeronymite13-Sep-20 11:10
Jeronymite13-Sep-20 11:10 
Questionvery useful Pin
Southmountain15-Feb-19 5:56
Southmountain15-Feb-19 5:56 
AnswerRe: very useful Pin
Shaenn17-Feb-19 22:25
Shaenn17-Feb-19 22:25 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.