Compare commits

..

2 commits

View file

@ -31,10 +31,6 @@ warnings.simplefilter(action="ignore", category=FutureWarning)
# starlink visible
here = dateutil.tz.tzlocal()
now = datetime.now()
today = now.date()
now_str = now.strftime("%Y-%m-%d_%H:%M")
now_utc = datetime.now(timezone.utc)
next_us_presidential_election = date(2024, 11, 5)
next_uk_general_election = date(2024, 5, 2)
@ -81,7 +77,7 @@ def next_uk_fathers_day(input_date: date) -> date:
days_until_sunday = (6 - current_day_of_week) % 7
# Calculate the date of the next Sunday
next_sunday = today + timedelta(days=days_until_sunday)
next_sunday = input_date + timedelta(days=days_until_sunday)
# Calculate the date of Father's Day, which is the third Sunday of June
fathers_day = date(next_sunday.year, 6, 1) + timedelta(
@ -89,7 +85,7 @@ def next_uk_fathers_day(input_date: date) -> date:
)
# Check if Father's Day has already passed this year
if today > fathers_day:
if input_date > fathers_day:
# If it has passed, calculate for the next year
fathers_day = date(fathers_day.year + 1, 6, 1) + timedelta(
weeks=2, days=next_sunday.weekday()
@ -98,15 +94,15 @@ def next_uk_fathers_day(input_date: date) -> date:
return fathers_day
def get_next_timezone_transition(tz_name: str) -> date:
def get_next_timezone_transition(from_dt: datetime, tz_name: str) -> date:
"""Datetime of the next time the clocks change."""
tz = pytz.timezone(tz_name)
dt = next(t for t in tz._utc_transition_times if t > now)
dt = next(t for t in tz._utc_transition_times if t > from_dt)
return typing.cast(date, dt.date())
def get_next_bank_holiday() -> dict[str, date | str]:
def get_next_bank_holiday(input_date: date) -> dict[str, date | str]:
"""Date and name of the next UK bank holiday."""
url = "https://www.gov.uk/bank-holidays.json"
filename = os.path.join(data_dir, "bank-holidays.json")
@ -119,7 +115,7 @@ def get_next_bank_holiday() -> dict[str, date | str]:
next_holiday = None
for event in events:
event_date = datetime.strptime(event["date"], "%Y-%m-%d").date()
if event_date < today:
if event_date < input_date:
continue
next_holiday = {"date": event_date, "title": event["title"]}
break
@ -129,8 +125,9 @@ def get_next_bank_holiday() -> dict[str, date | str]:
return next_holiday
def get_gbpusd() -> Decimal:
def get_gbpusd(now: datetime) -> Decimal:
"""Get the current value for GBPUSD, with caching."""
now_str = now.strftime("%Y-%m-%d_%H:%M")
fx_dir = os.path.join(data_dir, "fx")
existing_data = os.listdir(fx_dir)
existing = [f for f in existing_data if f.endswith("_GBPUSD.json")]
@ -161,7 +158,7 @@ def next_economist(input_date: date) -> date:
# Define the publication day (Thursday) and the day of the week of the input date
publication_day = 3 # Thursday (0 - Monday, 1 - Tuesday, ..., 6 - Sunday)
current_day_of_week = input_date.weekday()
current_week_number = today.isocalendar().week
current_week_number = input_date.isocalendar().week
# Define the list of weeks when The Economist is not published
non_publication_weeks = [26, 56]
@ -260,29 +257,30 @@ def stock_markets() -> list[str]:
return reply
def get_us_holiday() -> dict[str, date | str]:
def get_us_holiday(input_date: date) -> dict[str, date | str]:
"""Date and name of next US holiday."""
hols = holidays.UnitedStates(years=[today.year, today.year + 1])
next_hol = next(x for x in sorted(hols.items()) if x[0] >= today)
hols = holidays.UnitedStates(years=[input_date.year, input_date.year + 1])
next_hol = next(x for x in sorted(hols.items()) if x[0] >= input_date)
return {"date": next_hol[0], "title": next_hol[1]}
def get_data() -> dict[str, str | object]:
def get_data(now: datetime) -> dict[str, str | object]:
"""Get data to display on agenda dashboard."""
rocket_dir = os.path.join(data_dir, "thespacedevs")
today = now.date()
reply = {
"now": now,
"gbpusd": get_gbpusd(),
"gbpusd": get_gbpusd(now),
"next_economist": next_economist(today),
"bank_holiday": get_next_bank_holiday(),
"us_holiday": get_us_holiday(),
"bank_holiday": get_next_bank_holiday(today),
"us_holiday": get_us_holiday(today),
# "next_uk_general_election": next_uk_general_election,
"next_us_presidential_election": next_us_presidential_election,
"stock_markets": stock_markets(),
"uk_clock_change": get_next_timezone_transition("Europe/London"),
"us_clock_change": get_next_timezone_transition("America/New_York"),
"uk_clock_change": get_next_timezone_transition(now, "Europe/London"),
"us_clock_change": get_next_timezone_transition(now, "America/New_York"),
"mothers_day": next_uk_mothers_day(today),
"fathers_day": next_uk_fathers_day(today),
"uk_financial_year_end": uk_financial_year_end(today),