Click here to Skip to main content
13,864,291 members
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

2.5K views
7 bookmarked
Posted 13 Feb 2019
Licenced CPOL

Public Holidays

, 13 Feb 2019
Rate this:
Please Sign up or sign in to vote.
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

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)

Share

About the Author

Shaenn
Software Developer
France France
No Biography provided

You may also be interested in...

Pro

Comments and Discussions

 
Questionvery useful Pin
Southmountain15-Feb-19 6:56
memberSouthmountain15-Feb-19 6:56 
AnswerRe: very useful Pin
Shaenn17-Feb-19 23:25
memberShaenn17-Feb-19 23: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.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web05 | 2.8.190214.1 | Last Updated 13 Feb 2019
Article Copyright 2019 by Shaenn
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid