Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Holiday Calendars, File Formats, et al

I'm looking for a way to figure out whether a given date is a "holiday," given some holiday calendar.

Specifically, you might say is_holiday (datetime.date, "USA") which would answer whether the given date is a holiday for the calendar named "USA."

I recognize that there's no trivial way of doing this for all holidays for all years. For example, while Christmas is always the 25th of December, for federal government purposes, sometimes we celebrate (i.e., it's not a business day) on the 24th of December, and sometimes on the 26th. Similary, Jewish holidays (for example) migrate yearly (relative to the Gregorian Calendar).

I'm not looking for calculations for each holiday. I'm wondering more if there's some accepted, standard file format that lists holidays by year, and if so, if there's any modules - in Python, specifically, but I'm flexible - that understand how to read said format. Similarly, the file format would define weekends - which are normally Saturday and Sunday, but in certain regions it may be Friday and Saturday, etc.

like image 815
FreeMemory Avatar asked Dec 31 '09 17:12

FreeMemory


2 Answers

Mozilla has a set of user-contributed holiday files in .ics format. That may get you started. Unfortunately they go out to different date ranges. iCalShare also has holiday calendars in .ics format.

Note:

Some holidays are state or region-related, rather than country-related.

I've found in the past that a hierarchical approach works. e.g. check for a city holiday calendar, then a state calendar, then a country calender.

There's a Python module for reading .ics files called icalendar(also on pypi). I can't vouch for its quality, unfortunately.

like image 131
Brian Agnew Avatar answered Oct 19 '22 05:10

Brian Agnew


The classic holiday.py module (part of an old dateutil package -- old enough to rely on module time rather than the relatively recent datetime!) does a good job of calendric calculations (including Jewish <-> Gregorian calendar translations) but does not deal with persistence (reading or writing).

As @Brian's response suggests, ics is the dominant format for "calendars" in a file (and there are holiday files, among others, in such format). To read and write ics file in Python you can for example use the iCalendar package, which does precisely that task, or vobject, which is a much broader package supporting the range of vCard and vCalendar formats (including iCalendar).

like image 2
Alex Martelli Avatar answered Oct 19 '22 06:10

Alex Martelli