Files
MajoorWaldi-ComfyUI-Majoor-…/mjr_am_shared/time.py
T
Your Name 934f10b58a Refactor imports and improve type hints across multiple modules
- Organized and grouped import statements for better readability in health.py, metadata.py, releases.py, scan.py, search.py, version.py, viewer.py, and registry.py.
- Updated type hints to use the new union syntax (e.g., str | None) for better clarity in scan.py, settings.py, tool_detect.py, and result.py.
- Enhanced the handling of optional types in various functions to ensure more robust type checking.
- Removed unnecessary blank lines and improved formatting for consistency.
- Adjusted the logic in several test files to ensure compatibility with the updated import structure and type hints. (See <attachments> above for file contents. You may not need to search or read the file again.)
2026-02-20 19:13:15 +01:00

53 lines
1.2 KiB
Python

"""
Time utilities for timestamps and performance measurement.
"""
from __future__ import annotations
import logging
import time
from collections.abc import Iterator
from contextlib import contextmanager
def now() -> float:
"""Get current timestamp in seconds (float)."""
return time.time()
def ms() -> int:
"""Get current timestamp in milliseconds (int)."""
return int(time.time() * 1000)
def format_timestamp(ts: float | None = None) -> str:
"""
Format timestamp as ISO 8601 string.
Args:
ts: Timestamp in seconds (default: now())
Returns:
ISO 8601 formatted string (e.g., "2025-12-29T19:30:45")
"""
if ts is None:
ts = now()
return time.strftime("%Y-%m-%dT%H:%M:%S", time.localtime(ts))
@contextmanager
def timer(label: str, logger: logging.Logger | None = None) -> Iterator[None]:
"""
Context manager for timing operations.
Usage:
with timer("metadata extraction", logger):
extract_metadata(path)
"""
start = now()
try:
yield
finally:
elapsed = now() - start
msg = f"{label} took {elapsed:.3f}s"
if logger:
logger.debug(msg)
else:
print(msg)