Merge pull request #576 from SuanmoSuanyangTechnology/fix/unify-timezone

[add] Change the "last_done" storage to UTC and remove the intermedia…
This commit is contained in:
Ke Sun
2026-03-16 16:22:19 +08:00
committed by GitHub
2 changed files with 10 additions and 13 deletions

View File

@@ -5,7 +5,7 @@ Implicit Emotions Storage Repository
事务由调用方控制,仓储层只使用 flush/refresh 事务由调用方控制,仓储层只使用 flush/refresh
""" """
import logging import logging
from datetime import date, datetime, timedelta, timezone from datetime import date, datetime, timezone
from typing import Generator, Optional from typing import Generator, Optional
@@ -177,22 +177,21 @@ class ImplicitEmotionsStorageRepository:
if raw is None: if raw is None:
continue continue
try: try:
CST = timezone(timedelta(hours=8))
last_done = datetime.fromisoformat(raw) last_done = datetime.fromisoformat(raw)
# last_done 写入时已是 CST naive直接使用无需转换 # last_done 写入时已是 UTC aware+00:00确保有 tzinfo
if last_done.tzinfo is not None: if last_done.tzinfo is None:
last_done = last_done.astimezone(CST).replace(tzinfo=None) last_done = last_done.replace(tzinfo=timezone.utc)
if updated_at is None: if updated_at is None:
yield end_user_id yield end_user_id
continue continue
# updated_at 数据库存的是 UTC naive转为 CST naive 再比较 # updated_at 数据库存的是 UTC naive补上 UTC tzinfo 再比较
if updated_at.tzinfo is None: if updated_at.tzinfo is None:
updated_at_cst = updated_at.replace(tzinfo=timezone.utc).astimezone(CST).replace(tzinfo=None) updated_at_utc = updated_at.replace(tzinfo=timezone.utc)
else: else:
updated_at_cst = updated_at.astimezone(CST).replace(tzinfo=None) updated_at_utc = updated_at.astimezone(timezone.utc)
if last_done > updated_at_cst: if last_done > updated_at_utc:
yield end_user_id yield end_user_id
except Exception as e: except Exception as e:
logger.warning(f"解析 last_done 时间戳失败: end_user_id={end_user_id}, raw={raw}, error={e}") logger.warning(f"解析 last_done 时间戳失败: end_user_id={end_user_id}, raw={raw}, error={e}")

View File

@@ -1158,13 +1158,11 @@ def write_message_task(self, end_user_id: str, message: list[dict], config_id: s
try: try:
_r = get_sync_redis_client() _r = get_sync_redis_client()
if _r is not None: if _r is not None:
from datetime import timedelta as _td
from datetime import timezone as _tz from datetime import timezone as _tz
_CST = _tz(_td(hours=8)) _now_utc = datetime.now(_tz.utc).isoformat()
_now_cst = datetime.now(_CST).replace(tzinfo=None).isoformat()
_r.set( _r.set(
f"write_message:last_done:{end_user_id}", f"write_message:last_done:{end_user_id}",
_now_cst, _now_utc,
ex=86400 * 30, ex=86400 * 30,
) )
except Exception as _e: except Exception as _e: