fix(timezone):修复触发告警记录的时间混乱问题
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
from datetime import datetime, timezone, timedelta
|
||||
from typing import List, Optional
|
||||
|
||||
from fastapi import APIRouter, Depends, HTTPException, Body
|
||||
@@ -26,6 +27,19 @@ class CameraUpdateRequest(BaseModel):
|
||||
enabled: Optional[bool] = None
|
||||
|
||||
|
||||
def convert_to_china_time(dt: Optional[datetime]) -> Optional[str]:
|
||||
"""将 UTC 时间转换为中国时间 (UTC+8)"""
|
||||
if dt is None:
|
||||
return None
|
||||
try:
|
||||
china_tz = timezone(timedelta(hours=8))
|
||||
if dt.tzinfo is None:
|
||||
dt = dt.replace(tzinfo=timezone.utc)
|
||||
return dt.astimezone(china_tz).isoformat()
|
||||
except Exception:
|
||||
return dt.isoformat() if dt else None
|
||||
|
||||
|
||||
@router.get("", response_model=List[dict])
|
||||
def list_cameras(
|
||||
enabled_only: bool = True,
|
||||
@@ -40,7 +54,7 @@ def list_cameras(
|
||||
"enabled": cam.enabled,
|
||||
"fps_limit": cam.fps_limit,
|
||||
"process_every_n_frames": cam.process_every_n_frames,
|
||||
"created_at": cam.created_at.isoformat() if cam.created_at else None,
|
||||
"created_at": convert_to_china_time(cam.created_at),
|
||||
}
|
||||
for cam in cameras
|
||||
]
|
||||
@@ -58,7 +72,7 @@ def get_camera(camera_id: int, db: Session = Depends(get_db)):
|
||||
"enabled": camera.enabled,
|
||||
"fps_limit": camera.fps_limit,
|
||||
"process_every_n_frames": camera.process_every_n_frames,
|
||||
"created_at": camera.created_at.isoformat() if camera.created_at else None,
|
||||
"created_at": convert_to_china_time(camera.created_at),
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user