Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
441 views
in Technique[技术] by (71.8m points)

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...