mirror of
https://github.com/comfyanonymous/ComfyUI.git
synced 2026-05-08 08:12:34 +08:00
fix: add UTC timezone suffix to datetime serializers
Co-authored-by: Amp <amp@ampcode.com> Amp-Thread-ID: https://ampcode.com/threads/T-019cf73e-7f44-7003-8886-58eaaf1edbf4
This commit is contained in:
parent
593be209a4
commit
7da9f22166
@ -1,4 +1,4 @@
|
||||
from datetime import datetime
|
||||
from datetime import datetime, timezone
|
||||
from typing import Any
|
||||
|
||||
from pydantic import BaseModel, ConfigDict, Field, field_serializer
|
||||
@ -20,7 +20,11 @@ class AssetSummary(BaseModel):
|
||||
|
||||
@field_serializer("created_at", "updated_at", "last_access_time")
|
||||
def _serialize_datetime(self, v: datetime | None, _info):
|
||||
return v.isoformat() if v else None
|
||||
if v is None:
|
||||
return None
|
||||
if v.tzinfo is None:
|
||||
v = v.replace(tzinfo=timezone.utc)
|
||||
return v.isoformat()
|
||||
|
||||
|
||||
class AssetsList(BaseModel):
|
||||
@ -41,7 +45,11 @@ class AssetUpdated(BaseModel):
|
||||
|
||||
@field_serializer("updated_at")
|
||||
def _serialize_updated_at(self, v: datetime | None, _info):
|
||||
return v.isoformat() if v else None
|
||||
if v is None:
|
||||
return None
|
||||
if v.tzinfo is None:
|
||||
v = v.replace(tzinfo=timezone.utc)
|
||||
return v.isoformat()
|
||||
|
||||
|
||||
class AssetDetail(BaseModel):
|
||||
@ -60,7 +68,11 @@ class AssetDetail(BaseModel):
|
||||
|
||||
@field_serializer("created_at", "last_access_time")
|
||||
def _serialize_datetime(self, v: datetime | None, _info):
|
||||
return v.isoformat() if v else None
|
||||
if v is None:
|
||||
return None
|
||||
if v.tzinfo is None:
|
||||
v = v.replace(tzinfo=timezone.utc)
|
||||
return v.isoformat()
|
||||
|
||||
|
||||
class AssetCreated(AssetDetail):
|
||||
|
||||
@ -34,7 +34,7 @@ def escape_sql_like_string(s: str, escape: str = "!") -> tuple[str, str]:
|
||||
|
||||
|
||||
def get_utc_now() -> datetime:
|
||||
"""Naive UTC timestamp (no tzinfo). We always treat DB datetimes as UTC."""
|
||||
"""Naive UTC timestamp (no tzinfo) for DB columns declared with timezone=False."""
|
||||
return datetime.now(timezone.utc).replace(tzinfo=None)
|
||||
|
||||
|
||||
|
||||
@ -1,5 +1,5 @@
|
||||
from typing import Any
|
||||
from datetime import datetime
|
||||
from datetime import datetime, timezone
|
||||
from sqlalchemy.orm import DeclarativeBase
|
||||
|
||||
class Base(DeclarativeBase):
|
||||
@ -13,6 +13,8 @@ def to_dict(obj: Any, include_none: bool = False) -> dict[str, Any]:
|
||||
if val is None and not include_none:
|
||||
continue
|
||||
if isinstance(val, datetime):
|
||||
if val.tzinfo is None:
|
||||
val = val.replace(tzinfo=timezone.utc)
|
||||
out[field] = val.isoformat()
|
||||
else:
|
||||
out[field] = val
|
||||
|
||||
Loading…
Reference in New Issue
Block a user