rubric-ai / frontend /utils /formatting.py
itslikethisnow's picture
three stages complete
a43aaf6
"""Formatting utilities for the frontend."""
from datetime import datetime, timedelta
def format_relative_timestamp(iso_timestamp: str) -> str:
"""
Format an ISO timestamp into a human-readable relative string.
Args:
iso_timestamp: ISO format timestamp string from Supabase
Returns:
Formatted string like:
- "Today, 9:00 AM"
- "Yesterday, 10:00 PM"
- "Nov. 12, 2:50 PM"
"""
try:
# Parse ISO timestamp
if iso_timestamp.endswith('Z'):
dt = datetime.fromisoformat(iso_timestamp.replace('Z', '+00:00'))
else:
dt = datetime.fromisoformat(iso_timestamp)
# Convert to local time if timezone-aware
if dt.tzinfo is not None:
dt = dt.astimezone()
dt = dt.replace(tzinfo=None)
now = datetime.now()
today = now.date()
yesterday = today - timedelta(days=1)
# Format time portion (cross-platform: use %I and strip leading zero)
time_str = dt.strftime("%I:%M %p").lstrip('0')
# Determine date portion
if dt.date() == today:
return f"Today, {time_str}"
elif dt.date() == yesterday:
return f"Yesterday, {time_str}"
else:
# Format as "Nov. 12, 2:50 PM"
month_abbr = dt.strftime("%b")
day = dt.day
return f"{month_abbr}. {day}, {time_str}"
except Exception as e:
print(f"Error formatting timestamp: {e}")
return iso_timestamp[:16] if len(iso_timestamp) > 16 else iso_timestamp
def format_standards_for_llm(standards: list) -> str:
"""
Format standards array for LLM consumption.
Args:
standards: List of dicts with 'code' and 'text' keys
Returns:
Formatted string: "code: text\ncode: text"
"""
if not standards:
return ""
lines = []
for standard in standards:
code = standard.get('code', '')
text = standard.get('text', '')
lines.append(f"{code}: {text}")
return "\n".join(lines)
def format_standards_list(standards: list) -> list:
"""
Validate and return standards list.
Args:
standards: List of dicts with 'code' and 'text' keys
Returns:
The standards list (pass-through for now)
"""
return standards