Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion src/voxkit/storage/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,22 @@ def generate_unique_id(prefix: str | None = None) -> str:
def readable_from_unique_id(date_str: str) -> str:
"""Convert a unique ID timestamp to a human-readable format.

Accepts both plain timestamps (YYYYMMDD_HHMMSS_ffffff) and prefixed IDs
produced by generate_unique_id (e.g. prefix_YYYYMMDD_HHMMSS_ffffff).

Args:
date_str: Timestamp string in format YYYYMMDD_HHMMSS_ffffff
date_str: Unique ID string, optionally prefixed as [prefix_]YYYYMMDD_HHMMSS_ffffff

Returns:
Human-readable date string (e.g., "January 01, 2024 at 12:00:00 PM")
"""
parts = date_str.split("_")
for i, part in enumerate(parts):
if len(part) == 8 and part.isdigit():
date_str = "_".join(parts[i:])
break
else:
raise ValueError(f"No valid timestamp found in unique ID: {date_str!r}")
dt = datetime.strptime(date_str, "%Y%m%d_%H%M%S_%f")
return dt.strftime("%B %d, %Y at %I:%M:%S %p")

Expand Down
20 changes: 20 additions & 0 deletions tests/storage/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,3 +185,23 @@ def test_readable_from_unique_id_format(self):
assert " at " in readable
# Should contain AM/PM
assert "AM" in readable or "PM" in readable

def test_readable_from_unique_id_with_prefix(self):
"""Test that readable_from_unique_id handles prefixed IDs from generate_unique_id."""
prefixed_id = generate_unique_id(prefix="test")
readable = readable_from_unique_id(prefixed_id)

assert " at " in readable
assert "AM" in readable or "PM" in readable

def test_readable_from_unique_id_roundtrip_with_prefix(self):
"""Test round-trip: a prefixed ID produces the same readable output as unprefixed."""
base_id = "20240115_143022_123456"
prefixed_id = f"myprefix_{base_id}"

assert readable_from_unique_id(base_id) == readable_from_unique_id(prefixed_id)

def test_readable_from_unique_id_invalid_raises(self):
"""Test that an ID with no valid timestamp raises ValueError."""
with pytest.raises(ValueError, match="No valid timestamp found"):
readable_from_unique_id("not_a_timestamp")
Loading