feat(api): add a utility function to retrieve naive datetime in UTC timezone

pull/20699/head
QuantumGhost 11 months ago
parent 102d2f4d58
commit 56ff5d0e8b

@ -0,0 +1,22 @@
import abc
import datetime
from typing import Protocol
class _NowFunction(Protocol):
@abc.abstractmethod
def __call__(self, tz: datetime.timezone | None) -> datetime.datetime:
pass
# _now_func is a callable with the _NowFunction signature.
# Its sole purpose is to abstract time retrieval, enabling
# developers to mock this behavior in tests and time-dependent scenarios.
_now_func: _NowFunction = datetime.datetime.now
def naive_utc_now() -> datetime.datetime:
"""Return a naive datetime object (without timezone information)
representing current UTC time.
"""
return _now_func(datetime.UTC).replace(tzinfo=None)

@ -0,0 +1,20 @@
import datetime
from libs.datetime_utils import naive_utc_now
def test_naive_utc_now(monkeypatch):
tz_aware_utc_now = datetime.datetime.now(tz=datetime.UTC)
def _now_func(tz: datetime.timezone | None) -> datetime.datetime:
return tz_aware_utc_now.astimezone(tz)
monkeypatch.setattr("libs.datetime_utils._now_func", _now_func)
naive_datetime = naive_utc_now()
assert naive_datetime.tzinfo is None
assert naive_datetime.date() == tz_aware_utc_now.date()
naive_time = naive_datetime.time()
utc_time = tz_aware_utc_now.time()
assert naive_time == utc_time
Loading…
Cancel
Save