开发者

Holiday Calendars, File Formats, et al

开发者 https://www.devze.com 2022-12-15 08:35 出处:网络
I\'m looking for a way to figure out whether a given date is a \"holiday,\" given some holiday calendar.

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 examp开发者_如何学编程le, 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.


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.


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).


Another good, comprehensive source of holidays data is KDE's kholidays:

See more on http://community.kde.org/KDE_PIM/KHolidays


Read Calendrical Calculations: http://emr.cs.uiuc.edu/home/reingold/calendar-book/index.shtml

The various rules for US holidays are provided in details, with algorithms.

The "Celebrate" business (moving the Christmas Holiday to the 24th because the 25th falls on a Saturday) is a simple matter of checking the US Federal Holidays list.

http://www.opm.gov/Operating_Status_Schedules/fedhol/2009.asp

Individual states vary, of course, as do businesses. But that's a start.

0

精彩评论

暂无评论...
验证码 换一张
取 消