后端统一时区为 UTC
This commit is contained in:
@@ -0,0 +1,45 @@
|
||||
import importlib.util
|
||||
from datetime import date, datetime, timedelta, timezone
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
|
||||
|
||||
def _load_time_api_module():
|
||||
module_path = (
|
||||
Path(__file__).resolve().parents[2] / "app" / "services" / "time_api.py"
|
||||
)
|
||||
spec = importlib.util.spec_from_file_location("tests_time_api_under_test", module_path)
|
||||
module = importlib.util.module_from_spec(spec)
|
||||
assert spec and spec.loader
|
||||
spec.loader.exec_module(module)
|
||||
return module
|
||||
|
||||
|
||||
def test_parse_utc_time_rejects_naive_datetimes():
|
||||
module = _load_time_api_module()
|
||||
|
||||
with pytest.raises(ValueError, match="timezone information"):
|
||||
module.parse_utc_time("2025-01-01T08:00:00")
|
||||
|
||||
|
||||
def test_parse_utc_time_normalizes_offset_datetime_to_utc():
|
||||
module = _load_time_api_module()
|
||||
result = module.parse_utc_time("2025-01-01T08:00:00+08:00")
|
||||
|
||||
assert result == datetime(2025, 1, 1, 0, 0, tzinfo=timezone.utc)
|
||||
|
||||
|
||||
def test_extract_date_keeps_original_offset_calendar_day():
|
||||
module = _load_time_api_module()
|
||||
result = module.extract_date("2025-01-01T00:30:00+08:00")
|
||||
|
||||
assert result == date(2025, 1, 1)
|
||||
|
||||
|
||||
def test_utc_now_returns_timezone_aware_utc_datetime():
|
||||
module = _load_time_api_module()
|
||||
result = module.utc_now()
|
||||
|
||||
assert result.tzinfo == timezone.utc
|
||||
assert result.utcoffset() == timedelta(0)
|
||||
Reference in New Issue
Block a user