From d4d2bea4b085d219b2193b033243ff924c49dd24 Mon Sep 17 00:00:00 2001 From: Gergely Polonkai Date: Tue, 29 Mar 2022 08:18:11 +0200 Subject: [PATCH] =?UTF-8?q?[Refactor]=20Move=20R=C4=83huk=C4=83la=20calcul?= =?UTF-8?q?ations=20to=20a=20separate=20function?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- seasonal_clock/times.py | 56 +++++++++++++++++++++++++++-------------- 1 file changed, 37 insertions(+), 19 deletions(-) diff --git a/seasonal_clock/times.py b/seasonal_clock/times.py index 904d5ae..2608da6 100644 --- a/seasonal_clock/times.py +++ b/seasonal_clock/times.py @@ -92,31 +92,49 @@ def collect_day_parts( return day_parts +def get_rahukaalam_times( + observer: LocationInfo, + date: datetime, + surrounding_days: bool = False +) -> List[Tuple[datetime, datetime]]: + """Get today’s Răhukăla times""" + + if surrounding_days: + yesterday = date - timedelta(days=1) + tomorrow = date + timedelta(days=1) + dates = [yesterday, date, tomorrow] + else: + dates = [date] + + times: List[Tuple[datetime, datetime]] = [] + + for day in dates: + try: + daytime_rahukaala = sun.rahukaalam(observer, date=day, daytime=True) + except ValueError: + pass + else: + times.append(daytime_rahukaala) + + try: + night_rahukaala = sun.rahukaalam(observer, date=day, daytime=False) + except ValueError: + pass + else: + times.append(night_rahukaala) + + times.sort(key=lambda items: items[0]) + + return times + + def get_rahukaalam( observer: LocationInfo, date: datetime ) -> Tuple[Optional[bool], datetime]: """Get the time of the next Rāhukāla or, if we are in the middle of one, the time of its end""" - yesterday = date - timedelta(days=1) - tomorrow = date + timedelta(days=1) - times: List[Tuple[datetime, datetime]] = [] + times = get_rahukaalam_times(observer, date, surrounding_days=True) - for day in (yesterday, date, tomorrow): - try: - daytime_rahukaalam = sun.rahukaalam(observer, date=day, daytime=False) - except ValueError: - pass - else: - times.append(daytime_rahukaalam) - - try: - night_rahukaalam = sun.rahukaalam(observer, date=day, daytime=True) - except ValueError: - pass - else: - times.append(night_rahukaalam) - - times.sort(key=lambda items: items[0]) active: bool = False next_time: Optional[datetime] = None