45 lines
1.0 KiB
Python
45 lines
1.0 KiB
Python
from __future__ import annotations
|
|
|
|
import re
|
|
from datetime import datetime, timedelta
|
|
from pathlib import Path
|
|
|
|
|
|
ORDERED_TIMESTAMP_STEM_RE = re.compile(r"^(?P<timestamp>\d{8}_\d{6})-[1-9]\d*$")
|
|
|
|
|
|
def naive_wall_time(dt: datetime) -> datetime:
|
|
return dt.replace(tzinfo=None)
|
|
|
|
|
|
def format_filename_stem(dt: datetime) -> str:
|
|
return naive_wall_time(dt).strftime("%Y%m%d_%H%M%S")
|
|
|
|
|
|
def round_datetime_to_second(dt: datetime) -> datetime:
|
|
rounded = dt.replace(microsecond=0)
|
|
if dt.microsecond >= 500_000:
|
|
rounded += timedelta(seconds=1)
|
|
return rounded
|
|
|
|
|
|
def format_rounded_filename_stem(dt: datetime) -> str:
|
|
rounded = round_datetime_to_second(dt)
|
|
return format_filename_stem(rounded)
|
|
|
|
|
|
def normalized_extension(path: Path) -> str:
|
|
return path.suffix.lower()
|
|
|
|
|
|
def group_stem_from_name(name: str) -> str:
|
|
stem = Path(name).stem
|
|
match = ORDERED_TIMESTAMP_STEM_RE.match(stem)
|
|
if match:
|
|
return match.group("timestamp")
|
|
return stem
|
|
|
|
|
|
def group_stem(path: Path) -> str:
|
|
return group_stem_from_name(path.name)
|