fix timezone handling and logging fallback robustness

This commit is contained in:
windyboy
2026-02-09 20:16:39 +08:00
parent 976a1fa1d0
commit aa0dc8ec47
13 changed files with 492 additions and 86 deletions
+37 -1
View File
@@ -2,7 +2,8 @@
import os
import tempfile
from datetime import datetime
import time
from datetime import datetime, timezone
from pathlib import Path
from unittest.mock import patch, MagicMock
import subprocess
@@ -125,6 +126,7 @@ class TestScanLibrary:
assert vf.path == video_file
assert vf.size_bytes > 0
assert isinstance(vf.modified_timestamp, datetime)
assert vf.modified_timestamp.tzinfo == timezone.utc
assert vf.category == "movie"
def test_scan_categorizes_files(self, tmp_path):
@@ -867,3 +869,37 @@ class TestInventoryReports:
assert csv_row['category'] == json_file_data['category']
assert csv_row['resolution'] == json_file_data['resolution']
assert csv_row['codec'] == json_file_data['codec']
@pytest.mark.skipif(not hasattr(time, "tzset"), reason="tzset not available on this platform")
def test_save_inventory_csv_converts_naive_local_time_to_utc(self, tmp_path, monkeypatch):
"""Test naive timestamps are interpreted as local time and converted to UTC."""
original_tz = os.environ.get("TZ")
try:
monkeypatch.setenv("TZ", "Etc/GMT-2")
time.tzset()
naive_local = datetime(2024, 1, 15, 10, 30, 0)
expected_utc = naive_local.astimezone(timezone.utc).strftime("%Y-%m-%dT%H:%M:%S")
video_files = [
VideoFile(
path=Path("/library/movie/test.mp4"),
filename="test.mp4",
size_bytes=1024000,
modified_timestamp=naive_local,
category="movie"
)
]
output_file = tmp_path / "inventory.csv"
from vlm.scanner import save_inventory_csv
save_inventory_csv(video_files, output_file, Path("/library"))
content = output_file.read_text(encoding="utf-8")
assert expected_utc in content
finally:
if original_tz is None:
monkeypatch.delenv("TZ", raising=False)
else:
monkeypatch.setenv("TZ", original_tz)
time.tzset()