Skip to content

Instantly share code, notes, and snippets.

@oskar456
Last active March 28, 2025 12:23
Show Gist options
  • Select an option

  • Save oskar456/e91ef3ff77476b0dbc4ac19875d0555e to your computer and use it in GitHub Desktop.

Select an option

Save oskar456/e91ef3ff77476b0dbc4ac19875d0555e to your computer and use it in GitHub Desktop.
Czech public holiday checker
#!/usr/bin/env python3
import datetime
def getholidays(year):
"""Return a set public holidays in CZ as (day, month) tuples."""
holidays = {
(1, 1), #Novy rok
(1, 5), #Svatek prace
(8, 5), #Den vitezstvi
(5, 7), #Cyril a Metodej
(6, 7), #Jan Hus
(28, 9), #Den Ceske statnosti
(28, 10), #Vznik CS statu
(17, 11), #Den boje za svobodu a dem.
(24, 12), #Stedry den
(25, 12), #1. svatek Vanocni
(26, 12), #2. svatek Vanocni
}
# Easter holiday LUT source http://www.whyeaster.com/customs/dateofeaster.shtml
easterlut = [(4, 14), (4, 3), (3, 23), (4, 11), (3, 31), (4, 18), (4, 8),
(3, 28), (4, 16), (4, 5), (3, 25), (4, 13), (4, 2), (3, 22),
(4, 10), (3, 30), (4, 17), (4, 7), (3, 27)]
easterday = datetime.date(year, *easterlut[year%19])
daystosunday = 6 - easterday.weekday()
easterday += datetime.timedelta(7 if daystosunday == 0 else daystosunday)
# print("Easter Sunday is on ", easterday)
holidays.update(((d.day, d.month) for d in [easterday - datetime.timedelta(2),
easterday + datetime.timedelta(1)]))
return holidays
def isholiday(date):
return (date.day, date.month) in getholidays(date.year)
if __name__ == "__main__":
for y in range(2016, 2030):
print("{}: {}".format(y, getholidays(y)))
testdates = [(2016, 3, 28), (2017, 3, 28), (2017, 4, 14)]
for date in testdates:
d = datetime.date(*date)
print(d.ctime(), isholiday(d))
#!/usr/bin/env python3
import datetime
def getholidays(year, goodfriday=True, liberation="everyfive"):
"""Return a set public holidays in NL as (day, month) tuples."""
holidays = {
(1, 1), # New year
(27, 4), # Koningsdag
(25, 12), # Kerst
(26, 12), # Kerst
}
if liberation == True or (liberation == "everyfive" and year % 5 == 0):
holidays.update([(5,5),])
# Easter holiday LUT source http://www.whyeaster.com/customs/dateofeaster.shtml
easterlut = [(4, 14), (4, 3), (3, 23), (4, 11), (3, 31), (4, 18), (4, 8),
(3, 28), (4, 16), (4, 5), (3, 25), (4, 13), (4, 2), (3, 22),
(4, 10), (3, 30), (4, 17), (4, 7), (3, 27)]
easterday = datetime.date(year, *easterlut[year % 19])
daystosunday = 6 - easterday.weekday()
easterday += datetime.timedelta(7 if daystosunday == 0 else daystosunday)
# print("Easter Sunday is on ", easterday)
if goodfriday:
holidays.update(((d.day, d.month) for d in [
easterday - datetime.timedelta(2),
]))
holidays.update(((d.day, d.month) for d in [
easterday, # Easter Sunday
easterday + datetime.timedelta(1), # Easter Monday
easterday + datetime.timedelta(39), # Ascension Day
easterday + datetime.timedelta(49), # Pentecost 1
easterday + datetime.timedelta(50), # Pentecost 2
]))
return holidays
def isholiday(date):
return (date.day, date.month) in getholidays(date.year)
if __name__ == "__main__":
for y in range(2020, 2031):
print("{}: {}".format(y, sorted(getholidays(y), key=lambda x: x[::-1],)))
testdates = [(2016, 3, 28), (2017, 3, 28), (2017, 4, 14)]
for date in testdates:
d = datetime.date(*date)
print(d.ctime(), isholiday(d))
@oskar456
Copy link
Copy Markdown
Author

To make it clear, this code is licensed as a public domain. Feel free to use it as you wish.

@oskar456
Copy link
Copy Markdown
Author

It turned out the calculation is broken for 2025. If the LUT lookup results in Sunday, Easter happens one week later not on that Sunday. I updated the code to fix this and also added a version for holidays in NL.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment