Render an ISO-8601 timestamp as a short relative string.
(iso_str: str)
| 253 | |
| 254 | |
| 255 | def relative_time(iso_str: str) -> str: |
| 256 | """Render an ISO-8601 timestamp as a short relative string.""" |
| 257 | try: |
| 258 | t = datetime.strptime(iso_str, "%Y-%m-%dT%H:%M:%SZ").replace(tzinfo=timezone.utc) |
| 259 | except (ValueError, TypeError): |
| 260 | return iso_str or "" |
| 261 | now = datetime.now(timezone.utc) |
| 262 | seconds = int((now - t).total_seconds()) |
| 263 | if seconds < 60: |
| 264 | return "just now" |
| 265 | if seconds < 3600: |
| 266 | return f"{seconds // 60}m ago" |
| 267 | if seconds < 86400: |
| 268 | return f"{seconds // 3600}h ago" |
| 269 | if seconds < 86400 * 7: |
| 270 | return f"{seconds // 86400}d ago" |
| 271 | return t.strftime("%Y-%m-%d") |