mirror of
https://ghfast.top/https://github.com/aeroxw/tick-stock-panel.git
synced 2026-09-12 16:44:15 +08:00
Merge pull request #272 from kevin9327/fix/report-created-at-beijing-time
fix(reports): AI 报告 created_at 改用北京墙钟, 修复 UTC 部署下「8 小时前」
This commit is contained in:
@@ -21,6 +21,8 @@ import threading
|
||||
import time
|
||||
from pathlib import Path
|
||||
|
||||
from app.market_time import cn_now
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
@@ -121,6 +123,9 @@ class JsonReportStore:
|
||||
|
||||
@staticmethod
|
||||
def _now_iso() -> str:
|
||||
"""当前本地时间 ISO 字符串(带秒精度,前端 toLocaleString 友好)。"""
|
||||
from datetime import datetime
|
||||
return datetime.now().isoformat(timespec="seconds")
|
||||
"""当前北京时间 ISO 字符串(带秒精度,前端 toLocaleString 友好)。
|
||||
|
||||
用北京墙钟而非宿主机时钟: 容器默认 UTC 时, 前端把这串 naive 时间按浏览器
|
||||
本地时区解析, 刚生成的报告会显示成「8 小时前」。
|
||||
"""
|
||||
return cn_now().replace(tzinfo=None).isoformat(timespec="seconds")
|
||||
|
||||
@@ -0,0 +1,42 @@
|
||||
"""AI 报告 created_at 时区测试 — 必须是北京墙钟, 不随服务器时区漂移。
|
||||
|
||||
三类 AI 报告(财务分析 / 个股分析 / 大盘复盘)共用 JsonReportStore 补 created_at,
|
||||
前端 fmtRelative 用 `new Date(created_at)` 按浏览器本地时区解析这串 naive 时间。
|
||||
服务端若用宿主机时钟(Docker 镜像默认 UTC), 刚生成的报告会被显示成「8 小时前」,
|
||||
「今天是否已生成过报告」的判定(created_at 前 10 位 == 浏览器今天)也会误判。
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import datetime, timedelta, timezone
|
||||
|
||||
import pytest
|
||||
|
||||
from app.services.json_report_store import JsonReportStore
|
||||
|
||||
CN_TZ = timezone(timedelta(hours=8))
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def store(tmp_path, monkeypatch):
|
||||
s = JsonReportStore("ai_reports.json", 20, id_prefix="rpt")
|
||||
monkeypatch.setattr(s, "_path", lambda: tmp_path / "ai_reports.json")
|
||||
return s
|
||||
|
||||
|
||||
def test_created_at_follows_beijing_clock(store):
|
||||
"""created_at 与北京墙钟一致(宿主机时区非 UTC+8 时旧实现偏移整时区差)。"""
|
||||
saved = store.save_report({"symbol": "600519.SH", "content": "正文"})
|
||||
|
||||
got = datetime.fromisoformat(saved["created_at"])
|
||||
expected = datetime.now(CN_TZ).replace(tzinfo=None)
|
||||
assert abs((got - expected).total_seconds()) < 5
|
||||
|
||||
|
||||
def test_created_at_keeps_naive_second_precision_format(store):
|
||||
"""格式不变: 秒精度、无时区后缀, 历史记录与前端解析保持兼容。"""
|
||||
saved = store.save_report({"symbol": "600519.SH", "content": "正文"})
|
||||
|
||||
created_at = saved["created_at"]
|
||||
assert len(created_at) == 19
|
||||
assert created_at[10] == "T"
|
||||
assert datetime.fromisoformat(created_at).tzinfo is None
|
||||
Reference in New Issue
Block a user