#!/usr/bin/env python3 """Extract timestamp values from AT Protocol TID strings.""" import datetime # Base32-sortable alphabet used by TIDs BASE32_SORTABLE = "234567abcdefghijklmnopqrstuvwxyz" def tid_to_timestamp(tid: str) -> int: """Convert a TID string to microseconds since UNIX epoch.""" if len(tid) != 13: raise ValueError(f"TID must be 13 characters, got {len(tid)}") # Decode base32-sortable to integer value = 0 for char in tid: idx = BASE32_SORTABLE.find(char) if idx == -1: raise ValueError(f"Invalid character in TID: {char!r}") value = value * 32 + idx # Extract timestamp (top 54 bits, but bit 63 is always 0, so really 53 bits) # The bottom 10 bits are the clock identifier timestamp_us = value >> 10 return timestamp_us def tid_to_datetime(tid: str) -> datetime.datetime: """Convert a TID string to a datetime object (UTC).""" timestamp_us = tid_to_timestamp(tid) return datetime.datetime.fromtimestamp(timestamp_us / 1_000_000, tz=datetime.timezone.utc) if __name__ == "__main__": import sys if len(sys.argv) < 2: print(f"Usage: {sys.argv[0]} [tid ...]") print(f"Example: {sys.argv[0]} 3jzfcijpj2z2a") sys.exit(1) for tid in sys.argv[1:]: try: ts_us = tid_to_timestamp(tid) dt = tid_to_datetime(tid) print(f"{tid} -> {ts_us} µs -> {dt.isoformat()}") except ValueError as e: print(f"{tid} -> Error: {e}", file=sys.stderr)