def test_sof_zman_kiddush_levana_between_moldos(self):
     calendar = JewishCalendar(5776, 8, 1)
     next_month = JewishCalendar(5776, 9, 1)
     first_molad = calendar.molad_as_datetime()
     second_molad = next_month.molad_as_datetime()
     expected_offset = (second_molad -
                        first_molad).total_seconds() * 10**6 / 2.0
     expected_time = first_molad + timedelta(microseconds=expected_offset)
     # round for floating microsecond precision inconsistency
     self.assertEqual(
         calendar.sof_zman_kiddush_levana_between_moldos().toordinal(),
         expected_time.toordinal())
Example #2
0
def get_next_rosh_chodesh(date_: date = None) -> RoshChodesh:
    calendar = JewishCalendar(date_ or dt.now())

    if calendar.is_rosh_chodesh():
        return get_next_rosh_chodesh(date_ + timedelta(days=-1))

    if calendar.jewish_month == 6:
        return get_next_rosh_chodesh(date_ + timedelta(
            days=calendar.days_in_jewish_month()))

    month_length = calendar.days_in_jewish_month()
    days_until_rh = 30 - calendar.jewish_day
    calendar.forward(days_until_rh)

    rh_dates = [calendar.gregorian_date.isoformat()]
    if month_length == 30:
        rh_dates.append(calendar.forward().gregorian_date.isoformat())

    molad = calendar.molad()
    molad_iso = dt(molad.gregorian_year, molad.gregorian_month,
                   molad.gregorian_day, molad.molad_hours, molad.molad_minutes)

    rh_data = {
        'month_name': calendar.jewish_month_name(),
        'days': rh_dates,
        'duration': 1 if month_length == 29 else 2,
        'molad': [molad_iso, molad.molad_chalakim]
    }
    settings = SimpleSettings(date=date_ or dt.now())
    return RoshChodesh(settings=settings, **rh_data)
    def test_day_of_chanukah_for_non_chaseirim_years(self):
        year = test.test_helper.standard_tuesday_kesidran()

        expected_days = map(lambda date: date.split('-'),
                            self.standard_significant_days()['chanukah'])
        result = map(
            lambda date: JewishCalendar(year, int(date[0]), int(date[1])).
            day_of_chanukah(), expected_days)
        self.assertEqual(list(result), list(range(1, 9)))
    def test_day_of_chanukah_for_chaseirim_years(self):
        year = test.test_helper.standard_monday_chaseirim()

        expected_days = map(lambda date: date.split('-'),
                            self.chanukah_for_chaseirim())
        result = map(
            lambda date: JewishCalendar(year, int(date[0]), int(date[1])).
            day_of_chanukah(), expected_days)
        self.assertEqual(list(result), list(range(1, 9)))
Example #5
0
 def is_assur_bemelacha(self, current_time: datetime, tzais=None, in_israel: Optional[bool]=False):
     if tzais is None:
         tzais_time = self.tzais()
     elif isinstance(tzais, dict):
         tzais_time = self.tzais(tzais)
     else:
         tzais_time = tzais
     jewish_calendar = JewishCalendar(current_time.date())
     jewish_calendar.in_israel = in_israel
     return (current_time <= tzais_time and jewish_calendar.is_assur_bemelacha()) or \
            (current_time >= self.elevation_adjusted_sunset() and jewish_calendar.is_tomorrow_assur_bemelacha())
Example #6
0
def _get_first_day_date(name: str,
                        date_: date,
                        diaspora: bool = True) -> JewishCalendar:
    day, month = HOLYDAYS_AND_FASTS_DATES[name]

    now = JewishCalendar(date_)
    calendar = JewishCalendar.from_jewish_date(now.jewish_year, month, day)

    if calendar < now:
        # in case of long holyday to protect from return next year holiday during current one
        # mooving backward to length of the holiday
        if name in LONG_HOLYDAYS:
            duration = LONG_HOLYDAYS[name][0] if diaspora else LONG_HOLYDAYS[
                name][1]
            if now.back(duration) < calendar:
                return _get_first_day_date(name,
                                           date_ - timedelta(days=duration),
                                           diaspora)

        calendar = JewishCalendar.from_jewish_date(calendar.jewish_year + 1,
                                                   month, day)

    if month == 12 and calendar.is_jewish_leap_year():
        calendar.forward(30)

    # if yom hashoa felt on Friday, moove it to Thursday
    if name == 'yom_hashoah' and calendar.day_of_week == 6:
        calendar.forward(-1)

    # if yom hashoa felt on Sunday, moove it to Monday
    if name == 'yom_hashoah' and calendar.day_of_week == 1:
        calendar.forward(1)

    # if yom hazikarom felt on thursday and yom haatzmaut on friday,
    # moove them one day to past
    if (name == 'yom_hazikaron' and calendar.day_of_week == 5) or \
       (name == 'yom_haatzmaut' and calendar.day_of_week == 6):
        calendar.forward(-1)

    # if yom hazikarom felt on friday and yom haatzmaut on shabbat,
    # moove them two days to past
    if (name == 'yom_hazikaron' and calendar.day_of_week == 6) or \
       (name == 'yom_haatzmaut' and calendar.day_of_week == 7):
        calendar.forward(-2)

    # if yom hazikarom felt on sunday and yom haatzmaut on monday,
    # moove them one day to future
    if (name == 'yom_hazikaron' and calendar.day_of_week == 1) or \
       (name == 'yom_haatzmaut' and calendar.day_of_week == 2):
        calendar.forward(1)

    return calendar
Example #7
0
def convert_heb_to_greg(date_: str) -> str:
    try:
        year, month, day = map(int, date_.split('-'))
    except ValueError:
        raise IncorrectJewishDateException

    try:
        calendar = JewishCalendar(year, month, day)
    except ValueError:
        raise IncorrectJewishDateException
    gr_date = calendar.gregorian_date
    resp = f'{gr_date.day} {MONTH_NAMES_GENETIVE[gr_date.month]} {gr_date.year}, {WEEKDAYS[gr_date.weekday()]}'
    return resp
    def test_day_of_omer(self):
        calendar = JewishCalendar(test.test_helper.standard_monday_chaseirim(),
                                  1, 16)
        found_days = []

        def find_day(c: JewishCalendar):
            found_days.append(c.day_of_omer())
            c.forward()

        for n in range(1, 50):
            find_day(calendar)

        self.assertEqual(found_days, list(range(1, 50)))
    def test_modern_holidays_for_nissan_starting_shabbos(self):
        year = test.test_helper.standard_thursday_kesidran()

        self.assertEqual(JewishCalendar(year, 1, 1).day_of_week, 7)
        all_days = test.test_helper.all_days_matching(
            year, lambda c: c.significant_day(), use_modern_holidays=True)
        expected = {
            'yom_hashoah': ['1-27'],
            'yom_hazikaron': ['2-3'],
            'yom_haatzmaut': ['2-4'],
            'yom_yerushalayim': ['2-28']
        }
        result = test.test_helper.specific_days_matching(
            all_days, expected.keys())
        self.assertEqual(result, expected)
Example #10
0
def convert_heb_to_greg(date_: str) -> Tuple[str, InlineKeyboardMarkup]:
    try:
        year, month, day = map(int, date_.split('-'))
    except ValueError:
        raise IncorrectJewishDateException

    try:
        calendar = JewishCalendar(year, month, day)
    except ValueError:
        raise IncorrectJewishDateException
    gr_date = calendar.gregorian_date
    resp = f'{gr_date.day} {MONTH_NAMES_GENETIVE[gr_date.month]} {gr_date.year}, {WEEKDAYS[gr_date.weekday()]}'

    kb = get_zmanim_by_date_buttons([gr_date])
    return resp, kb
Example #11
0
def all_days_matching(year,
                      matcher,
                      in_israel=False,
                      use_modern_holidays=False):
    calendar = JewishCalendar(year, 7, 1)
    calendar.in_israel = in_israel
    calendar.use_modern_holidays = use_modern_holidays
    collection = {}
    while calendar.jewish_year == year:
        sd = matcher(calendar)
        if sd:
            if sd not in collection:
                collection[sd] = []
            collection[sd] += [
                f'{calendar.jewish_month}-{calendar.jewish_day}'
            ]
        calendar.forward()
    return collection
    def test_molad_as_datetime(self):
        expected_offset = (2, 20, 56, 496000
                           )  # UTC is 2:20:56.496 behind Jerusalem Local Time
        calendar = JewishCalendar(5776, 8, 1)
        hours, minutes, chalakim = 5, 51, 10
        seconds = (chalakim * 10 / 3.0)
        seconds, microseconds = divmod(seconds * 10**6, 10**6)
        hours -= expected_offset[0]
        minutes -= expected_offset[1]
        seconds -= expected_offset[2]
        microseconds -= expected_offset[3]

        total_seconds = (hours * 3600) + (minutes * 60) + seconds
        total_microseconds = (total_seconds * 10**6) + microseconds

        expected_molad = datetime(
            2015, 10, 13, 0, 0, 0,
            tzinfo=tz.UTC) + timedelta(microseconds=total_microseconds)
        self.assertEqual(calendar.molad_as_datetime(), expected_molad)
 def test_day_of_omer_outside_of_omer(self):
     calendar = JewishCalendar(test.test_helper.standard_monday_chaseirim(),
                               7, 1)
     self.assertIsNone(calendar.day_of_omer())
 def test_has_delayed_candle_lighting_for_standard_erev_yom_tov(self):
     date = '2018-09-30'
     subject = JewishCalendar(parser.parse(date))
     self.assertFalse(subject.has_delayed_candle_lighting())
 def all_rosh_hashanas(self):
     return map(lambda y: JewishCalendar(y, 7, 1), self.all_year_types())
 def test_day_of_chanukah_for_non_chanukah_date(self):
     calendar = JewishCalendar(test.test_helper.standard_monday_chaseirim(),
                               7, 1)
     self.assertIsNone(calendar.day_of_chanukah())
 def test_has_delayed_candle_lighting_for_non_candle_lighting_day(self):
     date = '2018-09-13'
     subject = JewishCalendar(parser.parse(date))
     self.assertFalse(subject.has_delayed_candle_lighting())
 def test_has_delayed_candle_lighting_for_yom_tov_erev_shabbos(self):
     date = '2019-04-26'
     subject = JewishCalendar(parser.parse(date))
     self.assertFalse(subject.has_delayed_candle_lighting())
 def test_has_delayed_candle_lighting_for_shabbos_followed_by_yom_tov(self):
     date = '2019-06-08'
     subject = JewishCalendar(parser.parse(date))
     self.assertTrue(subject.has_delayed_candle_lighting())
 def test_has_delayed_candle_lighting_for_standard_first_day_yom_tov(self):
     date = '2018-10-01'
     subject = JewishCalendar(parser.parse(date))
     self.assertTrue(subject.has_delayed_candle_lighting())
Example #21
0
def mishmar(event, context):
    zone = tz.gettz('America/New_York')
    time = datetime.now().astimezone(zone)

    cal = JewishCalendar(time.date())
    tomorrow = cal + 1
    sd = tomorrow.significant_day()

    slack_key = os.environ["SLACK_API_TOKEN"]
    sc = SlackClient(slack_key)

    if (time.hour == 21) and \
            (not tomorrow.is_assur_bemelacha()) and \
            (tomorrow.day_of_week == 6 or
             sd == 'erev_yom_kippur' or
             sd == 'erev_succos' or
             sd == 'purim_katan' or
             sd == 'pesach_sheni' or
             sd == 'tzom_gedalyah' or
             sd == 'tenth_of_teves' or
             sd == 'taanis_esther' or
             sd == 'seventeen_of_tammuz' or
             (tomorrow.jewish_month == 5 and tomorrow.jewish_day == 8) or
             sd == 'tu_beshvat' or
             (tomorrow.jewish_month == 9 and tomorrow.jewish_day == 25) or
             tomorrow.day_of_omer() == 33 or
             (tomorrow.jewish_month == 5 and tomorrow.jewish_day == 30)):

        modifier = None

        if tomorrow.jewish_month == 6 and tomorrow.jewish_day in range(22, 29):
            modifier = 'Rosh Hashana is next week'
        elif sd == 'tzom_gedalyah':
            modifier = 'Tzom Gedalyah is tonight'
        elif sd == 'erev_yom_kippur':
            modifier = 'Yom Kippur is tomorrow'
        elif sd == 'erev_succos':
            modifier = 'Succos begins tomorrow'
        elif tomorrow.jewish_month == 9 and tomorrow.jewish_day == 25:
            modifier = "Chanukah began tonight"
        elif tomorrow.jewish_month == 9 and tomorrow.jewish_day == 24:
            modifier = 'Chanukah begins tomorrow'
        elif sd == 'tenth_of_teves':
            modifier = "Asara B'Teves is tonight"
        elif sd == 'tu_beshvat':
            modifier = "Tu B'Shvat is tonight"
        elif tomorrow.jewish_month == 11 and tomorrow.jewish_day == 14:
            modifier = "Tu B'Shvat is approaching"
        elif sd == 'purim_katan':
            modifier = "Purim Katan is tonight"
        elif sd == 'taanis_esther':
            modifier = 'Purim is approaching'
        elif tomorrow.jewish_month == 1 and tomorrow.jewish_day in range(
                7, 14):
            modifier = 'Pesach begins next week'
        elif sd == 'pesach_sheni':
            modifier = 'Pesach Sheni is tonight'
        elif tomorrow.day_of_omer() == 33:
            modifier = "Lag B'Omer is tonight"
        elif tomorrow.day_of_omer() == 32:
            modifier = "Lag B'Omer is tomorrow"
        elif tomorrow.day_of_omer() in range(42, 49):
            modifier = 'Shavuos begins next week'
        elif sd == 'seventeen_of_tammuz':
            modifier = 'Three weeks have begun'
        elif tomorrow.jewish_month == 5 and tomorrow.jewish_day == 8 and not tomorrow.day_of_week == 6:
            modifier = "Tisha B'Av is tomorrow"
        elif tomorrow.jewish_month == 5 and tomorrow.jewish_day in [
                7, 8
        ] and tomorrow.day_of_week == 6:
            modifier = "Tisha B'Av is approaching"
        elif tomorrow.jewish_month == 5 and tomorrow.jewish_day == 30:
            modifier = "Rosh Chodesh Elul is tonight"
        elif tomorrow.jewish_month == 5 and tomorrow.jewish_day == 29:
            modifier = "Rosh Chodesh Elul is approaching"

        result1 = sc.api_call(
            'chat.postMessage',
            channel='#general',
            text=mishmarMessage('check out Mishmar in <#C3EP4TREX|torah>',
                                modifier),
            as_user=False,
            username='******',
            icon_url=
            'https://ca.slack-edge.com/T03DNU155-USLACKBOT-sv1444671949-72')

        result2 = sc.api_call(
            'chat.postMessage',
            channel='#torah',
            text=mishmarMessage("it's Mishmar time!", modifier),
            as_user=False,
            username='******',
            icon_url=
            'https://ca.slack-edge.com/T03DNU155-USLACKBOT-sv1444671949-72')

        body = {
            "message": "triggered",
            "input": event,
            "result": [result1, result2]
        }

    else:

        body = {"message": "not triggered", "input": event}

    response = {"statusCode": 200, "body": json.dumps(body)}

    return response

    # Use this code if you don't use the http event with the LAMBDA-PROXY
    # integration
    """