from datetime import UTC, datetime def humanize_delta(then: datetime, now: datetime | None = None) -> str: if now is None: now = datetime.now(UTC) if then.tzinfo is None: then = then.replace(tzinfo=UTC) if now.tzinfo is None: now = now.replace(tzinfo=UTC) seconds = int((now - then).total_seconds()) if seconds < 0: seconds = 0 if seconds < 45: return "just now" if seconds < 90: return "1 minute ago" minutes = seconds // 60 if minutes < 60: return f"{minutes} minutes ago" hours = minutes // 60 if hours < 24: return "1 hour ago" if hours == 1 else f"{hours} hours ago" days = hours // 24 if days < 7: return "1 day ago" if days == 1 else f"{days} days ago" return then.date().isoformat()