52 lines
1.5 KiB
Python
52 lines
1.5 KiB
Python
"""Broken out util functions to avoid importing sql during testing."""
|
|
|
|
import calendar
|
|
from datetime import UTC, datetime
|
|
|
|
from .dakiny_settings import settings
|
|
|
|
|
|
def get_datetime(month_day: str) -> datetime:
|
|
"""
|
|
Takes in a string month and day and returns a datetime object of that Month/Day this year.
|
|
|
|
Args:
|
|
month_day (str): Month/Day in that format.
|
|
|
|
Returns:
|
|
datetime: Datetime representation of Month/Day.
|
|
"""
|
|
today = datetime.now(tz=UTC).date()
|
|
year = today.year
|
|
month = int(month_day.split("/")[0])
|
|
day = int(month_day.split("/")[1])
|
|
|
|
# ensure that year rolls over if current month is December and the next month is less than 12.
|
|
if today.month == calendar.DECEMBER and month < calendar.OCTOBER:
|
|
year += 1
|
|
|
|
if today.month == calendar.JANUARY and month > calendar.MARCH:
|
|
year -= 1
|
|
|
|
return datetime(year, month, day, tzinfo=UTC)
|
|
|
|
|
|
def get_quarter_cycle() -> int | None:
|
|
"""
|
|
Returns the index for the current cycle/quarter. Defined as between term start and end.
|
|
|
|
Can return None if entire year isn't covered by cycles.
|
|
|
|
Returns:
|
|
int: cycle index for current cycle.
|
|
"""
|
|
today = datetime.now(tz=UTC)
|
|
for cycle, election in enumerate(settings.election_cycles):
|
|
if (
|
|
today.date() >= get_datetime(election.term_start).date()
|
|
and today.date() <= get_datetime(election.term_end).date()
|
|
):
|
|
return cycle
|
|
|
|
return None
|