Skip to content

Instantly share code, notes, and snippets.

@coderanger
Created March 23, 2013 22:49
Show Gist options
  • Save coderanger/5229641 to your computer and use it in GitHub Desktop.
Save coderanger/5229641 to your computer and use it in GitHub Desktop.
Find streaks of days that overlap no holidays
# coding=utf-8
import datetime
from dateutil.parser import parse
# From http://www.huffingtonpost.com/2013/01/01/religious-holidays-2013_n_2372650.html
raw_holidays = """
Baha'i Holidays 2013:
Jan 20 - World Religion Day
Mar 2-20 - Baha'i Fast
Mar 20 - Nowruz (Baha'i, Zoroastrian, Iranian New Year)
Apr 21- May 2 - Ridván
May 29 - Ascension of Baha'u'llah
July 9 - The Martyrdom of the Bab
Oct 20 - Birth of the Bab
Nov 12 - Birth of Baha'u'llah
Buddhist Holidays 2013:
Jan 27-30: Mahayana New Year
Feb 14 - Nirvana Day
Apr 6 - Theravada New Year
May 25 - Wesak
Dec 8 - Bodhi Day
Christian Holidays 2013 (all denominations):
Jan 1 - Solemnity of Mary, Mother of God (Catholic)
Jan 1 - Feast of St. Basil (Orthodox Christian)
Jan 6 - Epiphany
Jan 6 - Feast of Theophany (Orthodox Christian)
Jan 6 - Nativity of Jesus (Armenian Orthodox)
Jan 7 - Christmas Day (Orthodox Christian)
Jan 19 - Timkat (Ethiopian Orthodox Christian)
Jan 25 - Conversion of St. Paul
Feb 2 - Candlemas
Feb 11 - Feast Day of Our Lady of Lourdes (Catholic)
Feb 13 - Ash Wednesday
Mar 17 - St. Patrick's Day (Catholic, Orthodox, Lutheran)
Mar 18 - Clean Monday (Orthodox Christian)
Mar 24 - Palm Sunday
Mar 28 - Maundy Thursday
Mar 29 - Good Friday
Mar 30 - Holy Saturday
Mar 31 - Easter (Protestant, Catholic)
Apr 8 - Annunciation
May 5 - Easter (Orthodox Christian)
May 19 - Pentecost
May 26 - Trinity Sunday
May 29 - Ascension of Christ
May 30 - Corpus Christi (Catholic)
June 29 - St. Peter and St. Paul's Day
July 22 - Feast of Mary Magdalene
Aug 6 - Transfiguration of Jesus
Aug 15 - Assumption of the Blessed Virgin Mary (Catholic)
Aug 15 - Dormition of the Theotokos (Orthodox Christian)
Sept 21 - Nativity of the Theotokos / Birth of Virgin Mary
Nov 1 - All Saints Day / All Hallows' Day
Nov 2 - All Souls' Day
Nov 15 - Nativity Fast starts
Dec 1 - Advent Sunday
Dec 8 - Feast of Immaculate Conception (Catholic)
Dec 12 - Feast of Our Lady of Guadalupe
Dec 24 - Christmas Eve (Western Churches)
Dec 25 - Christmas Day (Western Churches)
Hindu Holidays 2013:
Jan 14 - Makar Sankranti / Pongal
Feb 15 - Saraswati Puja
Mar 10 - Shivratri
Mar 27 - Holi
Apr 20 - Ram Navami
Apr 25 - Hanuman Jayanti
Apr 11 - Akshaya Tritiya
Jul 3 - Guru Purunima
Aug 21 - Raksha Bandhan
Sept 13 - Janmasthami
Sept 16 - Onam
Sept 23 - Radhasthami
Oct 5-13 - Navratri
Oct 9-13 - Durga Puja
Oct. 14 - Dussehra
Nov 1-5 - Diwali
Jain Holidays 2013:
Apr 11 - Akshaya Tritiya
Apr 23 - Mahavir Jayanti
Nov 1-5 - Diwali
Jewish Holidays 2013:
Jan 26 - Tu B'Shevat
Feb 24 - Purim
Mar 26-Apr 2 - Passover
May 15-16 - Shavuot
July 16 - Tisha B'Av
Sept 5-6 - Rosh Hashanah
Sept 14 - Yom Kippur
Sept 19-25 - Sukkot
Sept. 27 - Simchat Torah
Nov. 27-Dec 5 - Hanukkah
Muslim Holidays 2013:
Jan 24 - Mawlid al-Nabi
July 9 - Ramadan Starts
Aug 3 - Lailat al Qadr
Aug 8 - Eid-ul-Fitr
Oct 15 - Eid-ul-Adha
Nov 4 - Al-Hijira (Islamic New Year)
Nov 14 - Ashura
Pagan Holidays 2013:
Feb 2 - Imbolc
Mar 20 - Spring Equinox
May 1 - Beltane
June 20 - Summer Solstice
Aug 1 - Lughnasadh (Lamamas)
Sept 22 - Autumn Equinox
Oct 31 - Samhain
Dec 21 - Winter Solstice (Yule)
Shinto Holidays 2013:
Jan 1 - Gantan-Sai
Feb 3 - Setsubun
Sikh Holidays 2013:
Jan 5 - Guru Gobind Singh Jayanti
Jan 13 - Lohri
Feb 15 - Vasant Panchami
Mar 28 - Hola Mohalla
Apr 14 - Vaisakhi
Nov 3 - Bandi Chhor Divas
Nov 21 - Birthday of Guru Nanak
Nov 24 - Martyrdom of Guru Tegh Bahadur
"""
holidays = {}
def daterange(start, end):
while start < end:
yield start
start += datetime.timedelta(days=1)
for line in raw_holidays.splitlines():
line = line.strip()
if not line or ':' in line:
continue
raw_date, name = line.split(' - ')
if '-' in raw_date:
first_raw_date, second_raw_date = raw_date.split('-')
first_date = parse(first_raw_date)
second_date = parse(second_raw_date, default=first_date)
for date in daterange(first_date, second_date):
holidays.setdefault(date, []).append(name)
else:
holidays.setdefault(parse(raw_date), []).append(name)
streaks = []
streak_start = None
for date in daterange(datetime.datetime(2013, 1, 1), datetime.datetime(2014, 1, 1)):
if date in holidays and streak_start:
# Ending a streak
streaks.append((streak_start, date))
streak_start = None
elif date not in holidays and not streak_start:
streak_start = date
for start, end in streaks:
print '{len} days: from {start} to {end}'.format(start=start.date(), end=end.date(), len=(end-start).days)
3 days: from 2013-01-02 to 2013-01-05
5 days: from 2013-01-08 to 2013-01-13
4 days: from 2013-01-15 to 2013-01-19
3 days: from 2013-01-21 to 2013-01-24
6 days: from 2013-01-27 to 2013-02-02
7 days: from 2013-02-04 to 2013-02-11
1 days: from 2013-02-12 to 2013-02-13
8 days: from 2013-02-16 to 2013-02-24
5 days: from 2013-02-25 to 2013-03-02
3 days: from 2013-03-21 to 2013-03-24
1 days: from 2013-03-25 to 2013-03-26
4 days: from 2013-04-02 to 2013-04-06
1 days: from 2013-04-07 to 2013-04-08
2 days: from 2013-04-09 to 2013-04-11
2 days: from 2013-04-12 to 2013-04-14
5 days: from 2013-04-15 to 2013-04-20
3 days: from 2013-05-02 to 2013-05-05
9 days: from 2013-05-06 to 2013-05-15
3 days: from 2013-05-16 to 2013-05-19
5 days: from 2013-05-20 to 2013-05-25
2 days: from 2013-05-27 to 2013-05-29
20 days: from 2013-05-31 to 2013-06-20
8 days: from 2013-06-21 to 2013-06-29
3 days: from 2013-06-30 to 2013-07-03
5 days: from 2013-07-04 to 2013-07-09
6 days: from 2013-07-10 to 2013-07-16
5 days: from 2013-07-17 to 2013-07-22
9 days: from 2013-07-23 to 2013-08-01
1 days: from 2013-08-02 to 2013-08-03
2 days: from 2013-08-04 to 2013-08-06
1 days: from 2013-08-07 to 2013-08-08
6 days: from 2013-08-09 to 2013-08-15
5 days: from 2013-08-16 to 2013-08-21
14 days: from 2013-08-22 to 2013-09-05
7 days: from 2013-09-06 to 2013-09-13
1 days: from 2013-09-15 to 2013-09-16
2 days: from 2013-09-17 to 2013-09-19
2 days: from 2013-09-25 to 2013-09-27
7 days: from 2013-09-28 to 2013-10-05
1 days: from 2013-10-13 to 2013-10-14
4 days: from 2013-10-16 to 2013-10-20
10 days: from 2013-10-21 to 2013-10-31
7 days: from 2013-11-05 to 2013-11-12
1 days: from 2013-11-13 to 2013-11-14
5 days: from 2013-11-16 to 2013-11-21
2 days: from 2013-11-22 to 2013-11-24
2 days: from 2013-11-25 to 2013-11-27
3 days: from 2013-12-05 to 2013-12-08
3 days: from 2013-12-09 to 2013-12-12
8 days: from 2013-12-13 to 2013-12-21
2 days: from 2013-12-22 to 2013-12-24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment