def test_hdate_to_hdate_meuberet_adar(self, year): for day in range(1, 30 + 1): date = HebrewDate(year, Months.Adar_I, day) assert conv.jdn_to_hdate(conv.hdate_to_jdn(date)) == date for day in range(1, 29 + 1): date = HebrewDate(year, Months.Adar_II, day) assert conv.jdn_to_hdate(conv.hdate_to_jdn(date)) == date
def jdn_to_hdate(jdn): """Convert from the Julian day to the Hebrew day.""" # calculate Gregorian date date = jdn_to_gdate(jdn) # Guess Hebrew year is Gregorian year + 3760 year = date.year + 3760 jdn_tishrey1 = hdate_to_jdn(HebrewDate(year, Months.Tishrei, 1)) jdn_tishrey1_next_year = hdate_to_jdn( HebrewDate(year + 1, Months.Tishrei, 1)) # Check if computed year was underestimated if jdn_tishrey1_next_year <= jdn: year = year + 1 jdn_tishrey1 = jdn_tishrey1_next_year jdn_tishrey1_next_year = hdate_to_jdn( HebrewDate(year + 1, Months.Tishrei, 1)) size_of_year = get_size_of_hebrew_year(year) # days into this year, first month 0..29 days = jdn - jdn_tishrey1 # last 8 months always have 236 days if days >= (size_of_year - 236): # in last 8 months days = days - (size_of_year - 236) month = days * 2 // 59 day = days - (month * 59 + 1) // 2 + 1 month = month + 4 + 1 # if leap if size_of_year > 355 and month <= 6: month = month + 8 else: # in 4-5 first months # Special cases for this year if size_of_year % 10 > 4 and days == 59: # long Heshvan (day 30) month = 1 day = 30 elif size_of_year % 10 > 4 and days > 59: # long Heshvan month = (days - 1) * 2 // 59 day = days - (month * 59 + 1) // 2 elif size_of_year % 10 < 4 and days > 87: # short kislev month = (days + 1) * 2 // 59 day = days - (month * 59 + 1) // 2 + 2 else: # regular months month = days * 2 // 59 day = days - (month * 59 + 1) // 2 + 1 month = month + 1 return HebrewDate(year, Months(month), day)
def upcoming_yom_tov(self): """Find the next upcoming yom tov (i.e. no-melacha holiday). If it is currently the day of yom tov (irrespective of zmanim), returns that yom tov. """ if self.is_yom_tov: return self this_year = self.get_holidays_for_year([HolidayTypes.YOM_TOV]) next_rosh_hashana = HDate( heb_date=HebrewDate(self.hdate.year + 1, Months.Tishrei, 1), diaspora=self.diaspora, hebrew=self.hebrew, ) next_year = next_rosh_hashana.get_holidays_for_year( [HolidayTypes.YOM_TOV]) # Filter anything that's past. holidays_list = [ holiday_hdate for _, holiday_hdate in chain(this_year, next_year) if holiday_hdate >= self ] holidays_list.sort(key=lambda h: h.gdate) return holidays_list[0]
def omer_day(self): """Return the day of the Omer.""" first_omer_day = HebrewDate(self.hdate.year, Months.Nisan, 16) omer_day = self._jdn - conv.hdate_to_jdn(first_omer_day) + 1 if not 0 < omer_day < 50: return 0 return omer_day
def get_holidays_for_year(self, types=None): """Get all the actual holiday days for a given HDate's year. If specified, use the list of types to limit the holidays returned. """ _LOGGER.debug("Looking up holidays of types %s", types) # Filter any non-related holidays depending on Israel/Diaspora only holidays_list = [ holiday for holiday in htables.HOLIDAYS if (holiday.israel_diaspora == "") or ( holiday.israel_diaspora == "ISRAEL" and not self.diaspora) or ( holiday.israel_diaspora == "DIASPORA" and self.diaspora) ] if types: # Filter non-matching holiday types. holidays_list = [ holiday for holiday in holidays_list if holiday.type in types ] # Filter any special cases defined by True/False functions holidays_list = [ holiday for holiday in holidays_list if all( func(self) for func in holiday.date_functions_list) ] _LOGGER.debug( "Holidays after filters have been applied: %s", [holiday.name for holiday in holidays_list], ) def holiday_dates_cross_product(holiday): """Given a (days, months) pair, compute the cross product. If days and/or months are singletons, they are converted to a list. """ return product(*([x] if isinstance(x, (int, Months)) else x for x in holiday.date)) # Compute out every actual Hebrew date on which a holiday falls for # this year by exploding out the possible days for each holiday. holidays_list = [ ( holiday, HDate( heb_date=HebrewDate(self.hdate.year, date_instance[1], date_instance[0]), diaspora=self.diaspora, hebrew=self.hebrew, ), ) for holiday in holidays_list for date_instance in holiday_dates_cross_product(holiday) if len(holiday.date) >= 2 ] return holidays_list
def get_reading(self): """Return number of hebrew parasha.""" _year_type = (self.year_size() % 10) - 3 year_type = (self.diaspora * 1000 + self.rosh_hashana_dow() * 100 + _year_type * 10 + self.pesach_dow()) _LOGGER.debug("Year type: %d", year_type) # Number of days since rosh hashana rosh_hashana = HebrewDate(self.hdate.year, Months.Tishrei, 1) days = self._jdn - conv.hdate_to_jdn(rosh_hashana) # Number of weeks since rosh hashana weeks = (days + self.rosh_hashana_dow() - 1) // 7 _LOGGER.debug("Since Rosh Hashana - Days: %d, Weeks %d", days, weeks) # If it's currently Simchat Torah, return VeZot Haberacha. if weeks == 3: if (days <= 22 and self.diaspora and self.dow != 7 or days <= 21 and not self.diaspora): return 54 # Special case for Simchat Torah in diaspora. if weeks == 4 and days == 22 and self.diaspora: return 54 # Return the indexes for the readings of the given year def unpack_readings(readings): return list( chain(*([x] if isinstance(x, int) else x for x in readings))) reading_for_year = htables.READINGS[year_type] readings = unpack_readings(reading_for_year) # Maybe recompute the year type based on the upcoming shabbat. # This avoids an edge case where today is before Rosh Hashana but # Shabbat is in a new year afterwards. if (weeks >= len(readings) and self.hdate.year < self.upcoming_shabbat.hdate.year): return self.upcoming_shabbat.get_reading() return readings[weeks]
def test_hdate_to_hdate_meuberet_kislev(self, year): days_in_month = 30 if not year == 5749 else 29 for day in range(1, days_in_month + 1): date = HebrewDate(year, Months.Kislev, day) assert conv.jdn_to_hdate(conv.hdate_to_jdn(date)) == date
def test_hdate_to_hdate_meuberet_heshvan(self, year): days_in_month = 29 if not year == 5760 else 30 for day in range(1, days_in_month + 1): date = HebrewDate(year, Months.Marcheshvan, day) assert conv.jdn_to_hdate(conv.hdate_to_jdn(date)) == date
def test_hdate_to_hdate_meuberet_simple(self, year): for days_in_month, months in self.SIMPLE_MONTHS.items(): for month in months: for day in range(1, days_in_month + 1): date = HebrewDate(year, month, day) assert conv.jdn_to_hdate(conv.hdate_to_jdn(date)) == date
def test_hdate_to_hdate_pshuta_adar(self, year): for day in range(1, 29 + 1): date = HebrewDate(year, Months.Adar, day) assert conv.jdn_to_hdate(conv.hdate_to_jdn(date)) == date
def pesach_dow(self): """Return the first day of week for Pesach.""" jdn = conv.hdate_to_jdn(HebrewDate(self.hdate.year, Months.Nisan, 15)) return (jdn + 1) % 7 + 1
def rosh_hashana_dow(self): """Return the Hebrew day of week for Rosh Hashana.""" jdn = conv.hdate_to_jdn(HebrewDate(self.hdate.year, Months.Tishrei, 1)) return (jdn + 1) % 7 + 1
for holiday in holidays_list for date_instance in holiday_dates_cross_product(holiday) if len(holiday.date) >= 2 ] return holidays_list @property def upcoming_yom_tov(self): """Find the next upcoming yom tov (i.e. no-melacha holiday). If it is currently the day of yom tov (irrespective of zmanim), returns that yom tov. """ <<<<<<< HEAD this_year = self._get_holidays_for_year([HolidayTypes.YOM_TOV]) next_rosh_hashana = HDate(heb_date=HebrewDate( self.hdate.year + 1, Months.Tishrei, 1), diaspora=self.diaspora, hebrew=self.hebrew) next_year = next_rosh_hashana._get_holidays_for_year( ======= if self.is_yom_tov: return self this_year = self.get_holidays_for_year([HolidayTypes.YOM_TOV]) next_rosh_hashana = HDate( heb_date=HebrewDate(self.hdate.year + 1, Months.Tishrei, 1), diaspora=self.diaspora, hebrew=self.hebrew) next_year = next_rosh_hashana.get_holidays_for_year( >>>>>>> 888a38c997b1b0c73bcaa0fb2891e0fdc0d01661 [HolidayTypes.YOM_TOV]) # Filter anything that's past.
def test_hdate_to_hdate_pshuta_kislev(self, year): days_in_month = 30 if not year == 5753 else 29 for day in range(1, days_in_month + 1): date = HebrewDate(year, 3, day) assert conv.jdn_to_hdate(conv.hdate_to_jdn(date)) == date
def test_hdate_to_hdate_pshuta_heshvan(self, year): days_in_month = 29 if not year == 5756 else 30 for day in range(1, days_in_month + 1): date = HebrewDate(year, 2, day) assert conv.jdn_to_hdate(conv.hdate_to_jdn(date)) == date