PublicHoliday C# Library

repository·master·Indexed 42 days ago

https://github.com/martinjw/holiday

A C# library for calculating public holidays and working days across various countries and regions. It provides the IPublicHoliday interface to check for holidays, calculate business days, and handle regional variations for countries including Canada, Germany, the UK, Switzerland, Australia, and France.

Tokens
1.8K
Snippets
8
Records
8
Agent score
4%

What's inside PublicHoliday

  1. Configure regional holidays for Canada

    master

    In Canada, provincial holidays vary by region. You can specify a province by passing its ISO Code into the CanadaPublicHoliday constructor.

    // Retrieve a list of holidays in Saskatchewan for 2016
    IList<DateTime> result = new CanadaPublicHoliday("SK").PublicHolidays(2016);
  2. Configure regional variations for the United Kingdom

    master

    The UKBankHoliday class defaults to England/Wales. To use Scotland or Northern Ireland, set the UkCountry property using the UKBankHoliday.UkCountries enum.

    // Calendar for Scotland
    var calendar = new UKBankHoliday { UkCountry = UKBankHoliday.UkCountries.Scotland };
    IList<DateTime> result = calendar.PublicHolidays(2022);
  3. Configure regional holidays for France

    master

    In France, you can retrieve holidays for the whole country or specific regions. To target a specific region, set the Region property using the FrancePublicHoliday.Regions enum.

    // Get list of public holidays for 2024
    IList<DateTime> result = new FrancePublicHoliday().PublicHolidays(2024);
    
    // Get list of public holiday names for 2024
    IList<string> names = new FrancePublicHoliday().PublicHolidayNames(2024);
    
    // Get list of public holiday names and dates for 2024
    IDictionary<DateTime, string> dict = new FrancePublicHoliday().PublicHolidayNames(2024);
    
    // Get holidays for a specific region (e.g., Martinique)
    var calendar = new FrancePublicHoliday { Region = FrancePublicHoliday.Regions.Martinique };
    var hols = calendar.PublicHolidays(2024);
  4. Configure regional holidays for Germany

    master

    In Germany, specify the state using the State property with the GermanPublicHoliday.States enum (using ISO codes).

    // Calendar for Saxony
    var calendar = new GermanPublicHoliday { State = GermanPublicHoliday.States.SN };
    IList<DateTime> result = calendar.PublicHolidays(2017);
  5. Configure regional holidays for Australia

    master

    Most Australian holidays are defined by state or territory. Specify the state using the State property with the AustraliaPublicHoliday.States enum (ISO code).

    Note: Some holidays (like Victoria's AFL Grand Final Day or Western Australia's Queen's Birthday) do not have fixed rules and may not be calculated algorithmically. Local holidays (e.g., Royal Queensland Show day) are also not included.

    // Calendar for Western Australia
    var calendar = new AustraliaPublicHoliday { State = AustraliaPublicHoliday.States.WA };
    var westernAustraliaDay = new DateTime(2017, 6, 5);
    var isHoliday = calendar.IsPublicHoliday(westernAustraliaDay);
  6. Configure regional holidays for Switzerland

    master

    Switzerland holidays can be configured by setting the Canton property. You can also customize the calendar by setting boolean flags in the constructor for specific holidays like hasSecondJanuary, hasLaborDay, hasCorpusChristi, hasChristmasEve, or hasNewYearsEve.

    To get holidays for all cantons combined, set Canton = SwitzerlandPublicHoliday.Cantons.ALL.

    // Calendar for a specific Canton
    var calendar = new SwitzerlandPublicHoliday { Canton = SwitzerlandPublicHoliday.Cantons.OW };
    
    // Get all public holidays for all cantons combined
    var holidayCalendar = new SwitzerlandPublicHoliday { Canton = SwitzerlandPublicHoliday.Cantons.ALL };
    IDictionary<DateTime, string> hols = holidayCalendar.PublicHolidays(2024);
    
    // Get detailed holiday objects including region names
    var holidayCalendar = new SwitzerlandPublicHoliday();
    IList<Holiday> hols = holidayCalendar.PublicHolidaysComplete(2024);
  7. Use the IPublicHoliday interface

    master

    All country-specific holiday classes implement the IPublicHoliday interface. This interface provides methods to check for holidays, calculate working days, and retrieve holiday lists for specific years or date ranges.

    Key methods include:

    • IsPublicHoliday(DateTime): Checks if a specific date is a public holiday.
    • IsWorkingDay(DateTime): Checks if a date is a working day (not a public holiday, Saturday, or Sunday).
    • NextWorkingDay(DateTime): Returns the next available working day.
    • BusinessDaysAdd(DateTime, int): Adds a specific number of business days (Mon-Fri, excluding holidays) to a date.
    • BusinessDaysBetween(DateTime, DateTime): Calculates the number of Mon-Fri working days between two dates.
    • PublicHolidays(int year): Returns a list of all holidays for a given year.
    • PublicHolidayNames(int year): Returns names of holidays for a given year.
    • GetHolidaysInDateRange(DateTime, DateTime): Retrieves holidays within a specific range.
    // Example: Get a list of all holidays for 2017
    IList<DateTime> result = new USAPublicHoliday().PublicHolidays(2017);
    
    // Example: Get the next working day
    DateTime dayAfterColumbus = new USAPublicHoliday().NextWorkingDay(new DateTime(2006, 10, 8));
    
    // Example: Check if a day is a bank holiday (UK specific)
    bool isHoliday = new UKBankHoliday().IsBankHoliday(new DateTime(2006, 1, 2));
    
    // Example: Add 3 business days to a date
    DateTime deadline = calendar.BusinessDaysAdd(new DateTime(2023, 1, 1), 3);