agenda/agenda/__init__.py

42 lines
1.1 KiB
Python

"""Agenda functions."""
from datetime import date, datetime, time
import pycountry
import pytz
uk_tz = pytz.timezone("Europe/London")
def uk_time(d: date, t: time) -> datetime:
"""Combine time and date for UK timezone."""
return uk_tz.localize(datetime.combine(d, t))
def format_list_with_ampersand(items: list[str]) -> str:
"""Join a list of strings with commas and an ampersand."""
if len(items) > 1:
return ", ".join(items[:-1]) + " & " + items[-1]
elif items:
return items[0]
return ""
def get_country(alpha_2: str) -> pycountry.db.Country | None:
"""Lookup country by alpha-2 country code."""
if alpha_2.count(",") > 10: # ESA
return pycountry.db.Country(flag="🇪🇺", name="ESA")
if not alpha_2:
return None
if alpha_2 == "xk":
return pycountry.db.Country(
flag="\U0001F1FD\U0001F1F0", name="Kosovo", alpha_2="xk"
)
country: pycountry.db.Country
if len(alpha_2) == 2:
country = pycountry.countries.get(alpha_2=alpha_2.upper())
elif len(alpha_2) == 3:
country = pycountry.countries.get(alpha_3=alpha_2.upper())
return country