TESTING / utils /time_utils.py
HansTran's picture
Upload 21 files
0d872e9 verified
Raw
History Blame Contribute Delete
2.57 kB
from __future__ import annotations
from datetime import datetime, timedelta, timezone
from uuid import uuid4
from zoneinfo import ZoneInfo
HOUR_OPTIONS = [f"{hour:02d}" for hour in range(24)]
MINUTE_OPTIONS = [f"{minute:02d}" for minute in range(60)]
def compute_end_time(start_hour: str, start_minute: str) -> tuple[str, str]:
start_time = datetime(2000, 1, 1, int(start_hour), int(start_minute), tzinfo=timezone.utc)
end_time = start_time + timedelta(hours=2)
return end_time.strftime("%H"), end_time.strftime("%M")
def format_time(hour: str, minute: str) -> str:
return f"{hour} : {minute}"
def get_next_occurrence_minutes(end_hour: str, end_minute: str) -> int:
now = datetime.now(ZoneInfo("Asia/Ho_Chi_Minh"))
end_time = now.replace(hour=int(end_hour), minute=int(end_minute), second=0, microsecond=0)
if end_time < now:
end_time += timedelta(days=1)
return int((end_time - now).total_seconds() // 60)
def sort_cards_by_upcoming_time(cards: list[dict]) -> list[dict[str, str]]:
return sorted(
cards,
key=lambda card: (
get_next_occurrence_minutes(card.get("endHour", "23"), card.get("endMinute", "59")),
card.get("title", "").lower(),
card.get("createdAt", ""),
),
)
def build_card_payload() -> dict[str, str]:
end_hour, end_minute = compute_end_time("08", "00")
return {
"id": "",
"title": "",
"startHour": "08",
"startMinute": "00",
"endHour": end_hour,
"endMinute": end_minute,
"createdAt": "",
"clientKey": str(uuid4()),
"tabId": "",
}
def normalize_cards(cards: list[dict]) -> list[dict[str, str]]:
normalized = []
for card in cards:
start_hour = str(card.get("startHour", "08")).zfill(2)
start_minute = str(card.get("startMinute", "00")).zfill(2)
end_hour, end_minute = compute_end_time(start_hour, start_minute)
normalized.append(
{
"id": str(card.get("id", "")),
"title": str(card.get("title", "")),
"startHour": start_hour,
"startMinute": start_minute,
"endHour": str(card.get("endHour", end_hour)).zfill(2),
"endMinute": str(card.get("endMinute", end_minute)).zfill(2),
"createdAt": str(card.get("createdAt", "")),
"clientKey": str(card.get("clientKey", uuid4())),
"tabId": str(card.get("tabId", "")),
}
)
return normalized