def get_variable_days(self, year): days = super().get_variable_days(year) delta = timedelta(days=1) current_date = date(year, 1, 1) while current_date.year == year: hebrew_date = GregorianDate( year=current_date.year, month=current_date.month, day=current_date.day, ).to_heb() jewish_year = hebrew_date.year month = hebrew_date.month day = hebrew_date.day if month == 7: if day == 1: days.append((current_date - delta, "Rosh Hashana Eve")) days.append((current_date, "Rosh Hashana")) days.append((current_date + delta, "Rosh Hashana")) elif day == 10: days.append((current_date - delta, "Yom Kippur Eve")) days.append((current_date, "Yom Kippur")) elif day == 15: days.append((current_date - delta, "Sukkot Eve")) days.append((current_date, "Sukkot")) elif day == 22: days.append((current_date - delta, "Shmini Atzeres Eve")) days.append((current_date, "Shmini Atzeres")) elif month == 1: if day == 15: days.append((current_date - delta, "Pesach Eve")) days.append((current_date, "Pesach")) elif day == 21: days.append((current_date - delta, "7th of Pesach Eve")) days.append((current_date, "7th of Pesach")) elif month == 2: if day == 5: independence_date = current_date if hebrew_date.weekday() == 6: independence_date = HebrewDate(jewish_year, month, 4).to_pydate() elif hebrew_date.weekday() == 7: independence_date = HebrewDate(jewish_year, month, 3).to_pydate() elif hebrew_date.weekday() == 2: independence_date = HebrewDate(jewish_year, month, 6).to_pydate() days.append( (independence_date - delta, "Independence Day Eve")) days.append((independence_date, "Independence Day")) elif month == 3 and day == 6: days.append((current_date - delta, "Shavout Eve")) days.append((current_date, "Shavout")) current_date += delta return days
def get_variable_days(self, year): days = super().get_variable_days(year) hebrew_date = GregorianDate(year=year, month=1, day=1).to_heb() jewish_year = hebrew_date.year holidays_hebrew_dates = [ (HebrewDate(jewish_year, 6, 29), "Rosh Hashana Eve"), (HebrewDate(jewish_year + 1, 7, 1), "Rosh Hashana"), (HebrewDate(jewish_year + 1, 7, 2), "Rosh Hashana"), (HebrewDate(jewish_year + 1, 7, 9), "Yom Kippur Eve"), (HebrewDate(jewish_year + 1, 7, 10), "Yom Kippur"), (HebrewDate(jewish_year + 1, 7, 14), "Sukkot Eve"), (HebrewDate(jewish_year + 1, 7, 15), "Sukkot"), (HebrewDate(jewish_year + 1, 7, 21), "Shmini Atzeres Eve"), (HebrewDate(jewish_year + 1, 7, 22), "Shmini Atzeres"), (HebrewDate(jewish_year, 1, 14), "Pesach Eve"), (HebrewDate(jewish_year, 1, 15), "Pesach"), (HebrewDate(jewish_year, 1, 20), "7th of Pesach Eve"), (HebrewDate(jewish_year, 1, 21), "7th of Pesach"), (HebrewDate(jewish_year, 3, 5), "Shavout Eve"), (HebrewDate(jewish_year, 3, 6), "Shavout"), ] holidays_hebrew_dates += self.get_hebrew_independence_day(jewish_year) for holiday_hebrew_date, holiday_name in holidays_hebrew_dates: days.append((holiday_hebrew_date.to_pydate(), holiday_name)) return days
def _is_hag_or_shabbat(date, diaspora=False): if date.weekday() == 5: return 'Shabbat' # Returns the name of the Hag hebYear, hebMonth, hebDay = GregorianDate(date.year, date.month, date.day).to_heb().tuple() # Holidays in Nisan if hebDay == 15 and hebMonth == 1: return "Pesach" if hebDay == 21 and hebMonth == 1: return "Pesach" # Holidays in Sivan if hebDay == 6 and hebMonth == 3: return "Shavuot" # Holidays in Elul if hebDay == 1 and hebMonth == 7: return "Rosh Hashana I" if hebDay == 2 and hebMonth == 7: return "Rosh Hashana II" # Holidays in Tishri if hebDay == 10 and hebMonth == 7: return "Yom Kippur" if hebDay == 15 and hebMonth == 7: return "Sukkot" if hebDay == 22 and hebMonth == 7: return "Simchat Torah"
def get_hags(date=datetime.date.today(), days_range_future=0, days_range_past=0, return_shabbat=True): holidays_dict = get_holidays(date) if not holidays_dict: if return_shabbat and date.weekday() == 5: # 5 == Shabbat holidays_dict = {9: 'Shabbat'} # lowest priority ############################################################################################################################################### # The marked code includes parashot and special shabbatot (Para, Hodesh, etc), but has a bug that 2020-05-30 does not have a parasha # ############################################################################################################################################### # # import date_utils.calendar_util # from jewish_dates.parasha import getTorahSections # shabbat = date #+ datetime.timedelta(days=1) # julian = date_utils.calendar_util.gregorian_to_jd(shabbat.year, shabbat.month, shabbat.day) # hebYear, hebMonth, hebDay = date_utils.calendar_util.jd_to_hebrew(julian) # str = getTorahSections(hebMonth, hebDay, hebYear, False) # if str: # folders_dict.update({8: str}) # second-lowest priority # TODO: Add special shabbatot: http://individual.utoronto.ca/kalendis/hebrew/parshah.htm # TODO: maybe ;ppk at this package: https://pypi.org/project/convertdate/ parashot = parshios.getparsha(GregorianDate( date.year, date.month, date.day), israel=True) if parashot: for index, parash in enumerate(parashot): holidays_dict.update({8 + index / 100: PARSHIOT[parash] }) # second-lowest priority else: for i in range(1, max(days_range_future, days_range_past) * 2 + 2): # check if any holidays next/past x days day_offset = i // 2 * ( 1, -1 )[i % 2] # returns this order of values: 0, +1, -1, +2, -2, +3, -3, etc... if day_offset > days_range_future or day_offset < -days_range_past: continue offset_date = date + datetime.timedelta(days=day_offset) holidays_dict = get_holidays(offset_date) if not holidays_dict: holidays_dict = get_personal(offset_date) if holidays_dict: break # export list of holidays, in order of priority sorted_folders = [] if holidays_dict and len(holidays_dict) > 0: for key in sorted(holidays_dict): sorted_folders += [holidays_dict[key]] return sorted_folders
def get_season(date=datetime.date.today()): hebYear, hebMonth, hebDay = GregorianDate(date.year, date.month, date.day).to_heb().tuple() if hebMonth >= 10: return [ 'Winter', ] elif hebMonth >= 7: return [ 'Fall', ] elif hebMonth >= 4: return [ 'Summer', ] else: return [ 'Spring', ]
def get_hebrew_calendar(cls, gregorian_year): """ Build and cache the Hebrew calendar for the given Gregorian Year. """ if gregorian_year not in cls.hebrew_calendars: # Build the hebrew calendar for year days = [] current_date = date(gregorian_year, 1, 1) while current_date.year == gregorian_year: hebrew_date = GregorianDate( year=current_date.year, month=current_date.month, day=current_date.day, ).to_heb() days.append((hebrew_date, current_date)) current_date += timedelta(days=1) # Store it in the class property cls.hebrew_calendars[gregorian_year] = days # Return the hebrew calendar return cls.hebrew_calendars[gregorian_year]
def get_personal(date=datetime.date.today()): hebYear, hebMonth, hebDay = GregorianDate(date.year, date.month, date.day).to_heb().tuple() if date.month == 5 and date.day == 4: # May the 4th be With You return {3: 'StarWars'}
def test_is_leap(): assert GregorianDate(2020, 10, 26).is_leap() == True assert GregorianDate(2021, 10, 26).is_leap() == False
def test_from_pydate(): date = datetime.date(2018, 8, 27) assert date == GregorianDate.from_pydate(date).to_jd().to_pydate() assert date == HebrewDate.from_pydate(date).to_pydate() assert date == JulianDay.from_pydate(date).to_pydate()
def test_weekday(): assert GregorianDate(2017, 8, 7).weekday() == 2 assert HebrewDate(5777, 6, 1).weekday() == 4 assert JulianDay(2458342.5).weekday() == 1
def test_greg_str(self): date = GregorianDate(2018, 8, 22) assert str(date) == '2018-08-22' assert str(GregorianDate(2008, 12, 2)) == '2008-12-02' assert str(GregorianDate(1, 1, 1)) == '0001-01-01'
def test_GregorianDate_errors(self): for datetuple in [(2018, 0, 3), (2018, -2, 8), (2018, 13, 9), (2018, 2, 0), (2018, 2, 29), (2012, 2, 30)]: with pytest.raises(ValueError): GregorianDate(*datetuple)
def get_variable_days(self, year): days = super(Israel, self).get_variable_days(year) delta = timedelta(days=1) current_date = date(year, 1, 1) while current_date.year == year: hebrew_date = GregorianDate( year=current_date.year, month=current_date.month, day=current_date.day, ).to_heb() jewish_year = hebrew_date.year month = hebrew_date.month day = hebrew_date.day if month == 7: if day in {1, 2, 3}: days.append((current_date, "Rosh Hashana")) elif day == 10: days.append((current_date, "Yom Kippur")) elif day in range(15, 22): days.append((current_date, "Sukkot")) elif day == 22: days.append((current_date, "Shmini Atzeres")) elif month == 12: leap = HebrewDate._is_leap(jewish_year) if day == 14: days.append((current_date, "Purim")) if day == 15 and not leap: days.append((current_date, "Purim")) elif month == 13: if day == 14: days.append((current_date, "Purim")) elif day == 15: days.append((current_date, "Shushan Purim")) elif month == 1 and day in {15, 21}: days.append((current_date, "Pesach")) elif month == 2: if day == 5: if hebrew_date.weekday() == 6: days.append( ( HebrewDate(jewish_year, month, 4).to_pydate(), "Independence Day", ) ) elif hebrew_date.weekday() == 7: days.append( ( HebrewDate(jewish_year, month, 3).to_pydate(), "Independence Day", ) ) elif hebrew_date.weekday() == 2: days.append( ( HebrewDate(jewish_year, month, 6).to_pydate(), "Independence Day", ) ) else: days.append((current_date, "Independence Day")) elif month == 3 and day == 6: days.append((current_date, "Shavout")) current_date += delta return days
def get_variable_days(self, year): days = super().get_variable_days(year) delta = timedelta(days=1) current_date = date(year, 1, 1) while current_date.year == year: hebrew_date = GregorianDate( year=current_date.year, month=current_date.month, day=current_date.day, ).to_heb() jewish_year = hebrew_date.year month = hebrew_date.month day = hebrew_date.day if month == 7: if day in {1, 2, 3}: days.append((current_date, "Rosh Hashana")) elif day == 10: days.append((current_date, "Yom Kippur")) elif day in range(15, 22): days.append((current_date, "Sukkot")) elif day == 22: days.append((current_date, "Shmini Atzeres")) elif month == 12 and not HebrewDate._is_leap(jewish_year): if day == 14: days.append((current_date, "Purim")) if day == 15: days.append((current_date, "Shushan Purim")) elif month == 13: if day == 14: days.append((current_date, "Purim")) elif day == 15: days.append((current_date, "Shushan Purim")) elif month == 1 and day in {15, 21}: days.append((current_date, "Pesach")) elif month == 2: if day == 5: if hebrew_date.weekday() == 6: days.append(( HebrewDate(jewish_year, month, 4).to_pydate(), "Independence Day", )) elif hebrew_date.weekday() == 7: days.append(( HebrewDate(jewish_year, month, 3).to_pydate(), "Independence Day", )) elif hebrew_date.weekday() == 2: days.append(( HebrewDate(jewish_year, month, 6).to_pydate(), "Independence Day", )) else: days.append((current_date, "Independence Day")) elif month == 3 and day == 6: days.append((current_date, "Shavout")) current_date += delta return days
def test_is_leap(): assert GregorianDate(2020, 10, 26).is_leap() == True assert GregorianDate(2021, 10, 26).is_leap() == False assert HebrewDate(5781, 10, 26).is_leap() == False assert HebrewDate(5782, 10, 26).is_leap() == True
def get_holidays(date=datetime.date.today(), diaspora=False): hebYear, hebMonth, hebDay = GregorianDate(date.year, date.month, date.day).to_heb().tuple() # Holidays in Nisan if hebDay == 15 and hebMonth == 1: return {0: 'Pesach', 3: 'Hag', 4: 'Shabbat'} if hebDay == 16 and hebMonth == 1: return {0: 'Pesach', 3: 'Hag', 4: 'Shabbat'} if hebDay == 17 and hebMonth == 1: return {0: 'Pesach', 3: 'Hag', 4: 'Shabbat'} if hebDay == 18 and hebMonth == 1: return {0: 'Pesach', 3: 'Hag', 4: 'Shabbat'} if hebDay == 19 and hebMonth == 1: return {0: 'Pesach', 3: 'Hag', 4: 'Shabbat'} if hebDay == 20 and hebMonth == 1: return {0: 'Pesach', 3: 'Hag', 4: 'Shabbat'} if hebDay == 21 and hebMonth == 1: return {0: 'Pesach', 3: 'Hag', 4: 'Shabbat'} # if hebDay == 22 and hebMonth == 1 and diaspora: # return {0: 'Pesach'}) # Yom Hashoah if HebrewDate(hebYear, 1, 27).weekday() == 6 and hebDay == 26 and hebMonth == 1: return {0: 'YomHashoah'} elif hebYear >= 5757 and HebrewDate( hebYear, 1, 27).weekday() == 1 and hebDay == 28 and hebMonth == 1: return {0: 'YomHashoah'} elif hebDay == 27 and hebMonth == 1: return {0: 'YomHashoah'} # Holidays in Iyar # Yom Hazikaron if HebrewDate(hebYear, 2, 4).weekday( ) == 6 and hebDay == 2 and hebMonth == 2: # If 4th of Iyar is a Thursday then Yom Hazikaron is on 2th of Iyar return {2: 'YomHazikaron'} elif HebrewDate(hebYear, 2, 4).weekday() == 5 and hebDay == 3 and hebMonth == 2: return {2: 'YomHazikaron'} elif hebYear >= 5764 and HebrewDate( hebYear, 2, 4).weekday() == 1 and hebDay == 5 and hebMonth == 2: return {2: 'YomHazikaron'} elif hebDay == 4 and hebMonth == 2: return {2: 'YomHazikaron'} # Yom Ha'Azmaut if HebrewDate(hebYear, 2, 5).weekday() == 7 and hebDay == 3 and hebMonth == 2: return {1: 'YomHaatzmaut', 3: 'Hag', 4: 'Shabbat'} elif HebrewDate(hebYear, 2, 5).weekday() == 6 and hebDay == 4 and hebMonth == 2: return {1: 'YomHaatzmaut', 3: 'Hag', 4: 'Shabbat'} elif hebYear >= 5764 and HebrewDate( hebYear, 2, 4).weekday() == 1 and hebDay == 6 and hebMonth == 2: return {1: 'YomHaatzmaut', 3: 'Hag', 4: 'Shabbat'} elif hebDay == 5 and hebMonth == 2: return {1: 'YomHaatzmaut', 3: 'Hag', 4: 'Shabbat'} if hebDay == 18 and hebMonth == 2: return {0: 'LagBaomer'} if hebDay == 28 and hebMonth == 2: return {0: 'YomYerushalayim'} # Holidays in Sivan if hebDay == 6 and hebMonth == 3: return {0: 'Shavuot', 3: 'Hag', 4: 'Shabbat'} # Holidays in Av if HebrewDate(hebYear, 5, 9).weekday() == 7 and hebDay == 10 and hebMonth == 5: return {0: 'TishaBAv'} elif hebDay == 9 and hebMonth == 5: return {0: 'TishaBAv'} if hebDay == 15 and hebMonth == 5: return {0: 'TuBAv'} # Holidays in Tishrei if hebDay == 1 and hebMonth == 7: return {0: 'RoshHashana', 1: 'ShabbatShuva', 3: 'Hag', 4: 'Shabbat'} if hebDay == 2 and hebMonth == 7: return {0: 'RoshHashana', 1: 'ShabbatShuva', 3: 'Hag', 4: 'Shabbat'} if hebDay == 3 and hebMonth == 7: return {1: 'ShabbatShuva'} if hebDay == 4 and hebMonth == 7: return {1: 'ShabbatShuva'} if hebDay == 5 and hebMonth == 7: return {1: 'ShabbatShuva'} if hebDay == 6 and hebMonth == 7: return {1: 'ShabbatShuva'} if hebDay == 7 and hebMonth == 7: return {1: 'ShabbatShuva'} if hebDay == 8 and hebMonth == 7: return {1: 'ShabbatShuva'} if hebDay == 9 and hebMonth == 7: return {1: 'ShabbatShuva'} if hebDay == 10 and hebMonth == 7: return {1: 'YomKippur', 2: 'ShabbatShuva', 3: 'Hag', 4: 'Shabbat'} if hebDay == 15 and hebMonth == 7: return {1: 'Sukkot', 3: 'Hag', 4: 'Shabbat'} if hebDay == 16 and hebMonth == 7: return {1: 'Sukkot', 3: 'Hag', 4: 'Shabbat'} if hebDay == 17 and hebMonth == 7: return {1: 'Sukkot', 3: 'Hag', 4: 'Shabbat'} if hebDay == 18 and hebMonth == 7: return {1: 'Sukkot', 3: 'Hag', 4: 'Shabbat'} if hebDay == 19 and hebMonth == 7: return {1: 'Sukkot', 3: 'Hag', 4: 'Shabbat'} if hebDay == 20 and hebMonth == 7: return {1: 'Sukkot', 3: 'Hag', 4: 'Shabbat'} if hebDay == 21 and hebMonth == 7: return {1: 'Sukkot', 3: 'Hag', 4: 'Shabbat'} if hebDay == 22 and hebMonth == 7: return {1: 'SimchatTorah', 3: 'Hag', 4: 'Shabbat'} # Holidays in Kislev if hebDay == 25 and hebMonth == 9: return {0: 'Hanukka'} if hebDay == 26 and hebMonth == 9: return {0: 'Hanukka'} if hebDay == 27 and hebMonth == 9: return {0: 'Hanukka'} if hebDay == 28 and hebMonth == 9: return {0: 'Hanukka'} if hebDay == 29 and hebMonth == 9: return {0: 'Hanukka'} if len(Month(hebYear, 9)) == 30: if hebDay == 30 and hebMonth == 9: return {0: 'Hanukka'} elif hebDay == 1 and hebMonth == 10: return {0: 'Hanukka'} elif hebDay == 2 and hebMonth == 10: return {0: 'Hanukka'} else: # if len(Month(hebYear, 9)) == 29: if hebDay == 1 and hebMonth == 10: return {0: 'Hanukka'} elif hebDay == 2 and hebMonth == 10: return {0: 'Hanukka'} elif hebDay == 3 and hebMonth == 10: return {0: 'Hanukka'} # Holidays in Shevat if hebDay == 15 and hebMonth == 11: return {0: 'TuBShvat'} # Holidays in Adar (I)/Adar II if Year(hebYear).leap: monthEsther = 13 else: monthEsther = 12 if hebDay == 14 and hebMonth == monthEsther: return {0: 'Purim', 3: 'Hag', 4: 'Shabbat'} elif hebDay == 15 and hebMonth == monthEsther: return {0: 'Purim', 3: 'Hag', 4: 'Shabbat'} # Shushan Purim
def test_isoweekday(): assert GregorianDate(2020, 9, 20).isoweekday() == 7 assert GregorianDate(2020, 10, 3).isoweekday() == 6 assert GregorianDate(2020, 10, 5).isoweekday() == 1 assert JulianDay(2458342.5).isoweekday() == 7