33 lines
811 B
Python
33 lines
811 B
Python
#!/usr/bin/env python3
|
|
# Eryn Wells <eryn@erynwells.me>
|
|
|
|
import datetime
|
|
from typing import Optional
|
|
|
|
|
|
def next_sunday_noon(from_date: Optional[datetime.date] = None) -> datetime.datetime:
|
|
'''
|
|
Return a `datetime` for noon on the upcoming Sunday. Use today if no
|
|
`from_date` is given.
|
|
'''
|
|
|
|
today = from_date or datetime.date.today()
|
|
|
|
current_weekday = today.weekday()
|
|
if current_weekday == 6:
|
|
days_to_next_sunday = 6
|
|
else:
|
|
days_to_next_sunday = 6 - current_weekday
|
|
|
|
delta = datetime.timedelta(days=days_to_next_sunday)
|
|
|
|
noon = datetime.time(hour=12)
|
|
|
|
timezone = datetime.datetime.now().astimezone().tzinfo
|
|
next_sunday_noon = datetime.datetime.combine(
|
|
today + delta,
|
|
noon,
|
|
tzinfo=timezone
|
|
)
|
|
|
|
return next_sunday_noon
|