[Refactor] Move Răhukăla calculations to a separate function

This commit is contained in:
Gergely Polonkai 2022-03-29 08:18:11 +02:00
parent 95d7d89470
commit d4d2bea4b0
No known key found for this signature in database
GPG Key ID: 2D2885533B869ED4

View File

@ -92,31 +92,49 @@ def collect_day_parts(
return day_parts return day_parts
def get_rahukaalam_times(
observer: LocationInfo,
date: datetime,
surrounding_days: bool = False
) -> List[Tuple[datetime, datetime]]:
"""Get todays 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( def get_rahukaalam(
observer: LocationInfo, date: datetime observer: LocationInfo, date: datetime
) -> Tuple[Optional[bool], 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""" """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) times = get_rahukaalam_times(observer, date, surrounding_days=True)
tomorrow = date + timedelta(days=1)
times: List[Tuple[datetime, datetime]] = []
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 active: bool = False
next_time: Optional[datetime] = None next_time: Optional[datetime] = None