implemented functions to get human readable dates

This commit is contained in:
Hellow
2024-02-14 23:37:43 +01:00
parent 420deb16cf
commit 6be934e374
5 changed files with 71 additions and 5 deletions

View File

@@ -12,4 +12,12 @@ PROGRAM_DATA_DIR.mkdir(parents=True, exist_ok=True)
ICS_FILE = Path(PROGRAM_DATA_DIR / "meetup.ics")
__all__ = ["prompt", "PROGRAM_DATA_DIR", "PROGRAM_NAME", "errors", "config", "ICS_FILE"]
__all__ = [
"prompt",
"PROGRAM_DATA_DIR",
"PROGRAM_NAME",
"errors",
"config",
"ICS_FILE",
"date",
]

View File

@@ -11,6 +11,8 @@ _config: dict = {
"mastodon",
"twitter",
],
"weekday_translations": {},
"months_translations": {},
"ics_url": "",
"mastodon": {},
"twitter": {},

View File

@@ -0,0 +1,40 @@
from datetime import datetime
from ics import Calendar, Event
from . import config
def to_date_string(date_time: datetime) -> str:
date_format = config.get_field("date_format", "%Y-%m-%d %H:%M:%S")
weekday_translations = config.get_field("weekday_translations", {})
months_translations = config.get_field("months_translations", {})
parsed = date_time.strftime(date_format)
for key, value in weekday_translations.items():
parsed = parsed.replace(key, value)
for key, value in months_translations.items():
parsed = parsed.replace(key, value)
return parsed.strip()
def to_time_string(date_time: datetime) -> str:
time_format = config.get_field("time_format", "%H:%M")
time_format_full_hour = config.get_field("time_format_full_hour", time_format)
parsed = date_time.strftime(time_format)
if int(date_time.minute) == 0:
parsed = date_time.strftime(time_format_full_hour)
return parsed.strip()
def event_formatting_values(event: Event, locale="en") -> dict:
return {
"time": to_time_string(event.begin),
"date": to_date_string(event.begin),
"date_humanized": event.begin.humanize(locale=locale),
}