统一前后端时间时区请求
This commit is contained in:
+19
-22
@@ -31,7 +31,7 @@ from fastapi.middleware.cors import CORSMiddleware
|
||||
from starlette.responses import FileResponse, JSONResponse
|
||||
from contextlib import asynccontextmanager
|
||||
|
||||
from pydantic import BaseModel
|
||||
from pydantic import BaseModel, field_validator
|
||||
|
||||
from multiprocessing import Value
|
||||
|
||||
@@ -3654,40 +3654,35 @@ async def fastapi_download_history_data_manually(
|
||||
class Run_Simulation_Manually_by_Date(BaseModel):
|
||||
"""
|
||||
name:数据库名称
|
||||
simulation_date:样式如 2025-05-04
|
||||
start_time:开始时间,样式如 08:00:00
|
||||
start_time:开始时间,样式如 2025-05-04T08:00:00+08:00
|
||||
duration:持续时间,单位为分钟
|
||||
"""
|
||||
|
||||
name: str
|
||||
simulation_date: str
|
||||
start_time: str
|
||||
duration: int
|
||||
|
||||
@field_validator("start_time")
|
||||
@classmethod
|
||||
def validate_start_time_timezone(cls, value: str) -> str:
|
||||
time_api.parse_aware_time(value, field_name="start_time")
|
||||
return value
|
||||
|
||||
|
||||
def run_simulation_manually_by_date(
|
||||
network_name: str, base_date: datetime, start_time: str, duration: int
|
||||
network_name: str, start_time: datetime, duration: int
|
||||
) -> None:
|
||||
# 解析开始时间
|
||||
start_hour, start_minute, start_second = map(int, start_time.split(":"))
|
||||
start_datetime = base_date.replace(
|
||||
hour=start_hour, minute=start_minute, second=start_second
|
||||
)
|
||||
|
||||
# 计算结束时间
|
||||
end_datetime = start_datetime + timedelta(minutes=duration)
|
||||
end_datetime = start_time + timedelta(minutes=duration)
|
||||
|
||||
# 生成时间点,每15分钟一个
|
||||
current_time = start_datetime
|
||||
current_time = start_time
|
||||
while current_time < end_datetime:
|
||||
# 格式化成ISO8601带时区格式
|
||||
iso_time = current_time.strftime("%Y-%m-%dT%H:%M:%S") + "+08:00"
|
||||
|
||||
## 执行函数调用
|
||||
simulation.run_simulation(
|
||||
name=network_name,
|
||||
simulation_type="realtime",
|
||||
modify_pattern_start_time=iso_time,
|
||||
modify_pattern_start_time=current_time.isoformat(timespec="seconds"),
|
||||
)
|
||||
|
||||
# 增加15分钟
|
||||
@@ -3698,7 +3693,7 @@ def run_simulation_manually_by_date(
|
||||
async def fastapi_run_simulation_manually_by_date(
|
||||
data: Run_Simulation_Manually_by_Date,
|
||||
) -> dict[str, str]:
|
||||
item = data.dict()
|
||||
item = data.model_dump()
|
||||
print(f"item: {item}")
|
||||
|
||||
filename = "c:/lock.simulation"
|
||||
@@ -3740,11 +3735,13 @@ async def fastapi_run_simulation_manually_by_date(
|
||||
globals.realtime_region_pipe_flow_and_demand_id,
|
||||
)
|
||||
|
||||
base_date = datetime.strptime(item["simulation_date"], "%Y-%m-%d")
|
||||
start_time = time_api.parse_utc_time(
|
||||
item["start_time"], field_name="start_time"
|
||||
)
|
||||
|
||||
thread = threading.Thread(
|
||||
target=lambda: run_simulation_manually_by_date(
|
||||
item["name"], base_date, item["start_time"], item["duration"]
|
||||
item["name"], start_time, item["duration"]
|
||||
)
|
||||
)
|
||||
|
||||
@@ -3753,11 +3750,11 @@ async def fastapi_run_simulation_manually_by_date(
|
||||
|
||||
return {"status": "success"}
|
||||
except Exception as e:
|
||||
return {"status": "error", "message": str(e)}
|
||||
raise HTTPException(status_code=500, detail=str(e)) from e
|
||||
|
||||
# thread.join()
|
||||
# DingZQ 08152025
|
||||
# matched_keys = redis_client.keys(f"*{item['simulation_date']}*")
|
||||
# matched_keys = redis_client.keys(...)
|
||||
# redis_client.delete(*matched_keys)
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user