mirror of https://github.com/langgenius/dify.git
34 lines
1019 B
Python
34 lines
1019 B
Python
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)
|
|
|
|
|
|
def ensure_naive_utc(dt: datetime.datetime) -> datetime.datetime:
|
|
"""Return the datetime as naive UTC (tzinfo=None).
|
|
|
|
If the input is timezone-aware, convert to UTC and drop the tzinfo.
|
|
Assumes naive datetimes are already expressed in UTC.
|
|
"""
|
|
if dt.tzinfo is None:
|
|
return dt
|
|
return dt.astimezone(datetime.UTC).replace(tzinfo=None)
|