fix(timezone): 审批单据时间统一为 naive 北京时间,消除 8 小时偏差
成因:approved_at / executed_at 列是 timestamp without time zone,而赋值用了
带时区的 datetime.now(timezone(timedelta(hours=8)))。psycopg2 对 naive 列不会
剥掉 tzinfo,而是转成 naive UTC 再写入,结果比同为北京时间的 created_at 早 8 小时。
(代码里并无 datetime.utcnow(),真实成因是 aware 值被驱动的隐式 UTC 转换。)
改动:
· outbound_service / borrow_service 的审批分支
datetime.now(beijing_tz).replace(tzinfo=None) → beijing_time()
(免审批分支已在上一轮改为 beijing_time,此处补齐审批分支);
· purchase_service 审批/完结分支同源缺陷一并修复;
· scrap_approval / scrap_approval_service 的 _beijing()、_beijing_now() 由
tz-aware 改为 naive —— 配合上述迁移把列类型改为 naive,
若仍返回 aware 值,timestamptz→timestamp 后会反向早 8 小时。
实测四表(scrap/outbound/borrow/purchase)created_at 与 approved_at 差值
均在同一秒内(-1ms ~ -12ms)。
This commit is contained in:
@ -1,7 +1,7 @@
|
||||
import json
|
||||
from datetime import datetime, timezone, timedelta, date
|
||||
from sqlalchemy import func
|
||||
from app.extensions import db
|
||||
from app.extensions import db, beijing_time
|
||||
from app.models.purchase import PurchaseRequest
|
||||
from app.models.base import MaterialBase
|
||||
|
||||
@ -145,8 +145,8 @@ class PurchaseService:
|
||||
if not purchase:
|
||||
raise ValueError("采购申请不存在")
|
||||
|
||||
beijing_tz = timezone(timedelta(hours=8))
|
||||
now = datetime.now(beijing_tz)
|
||||
# ★ approved_at 与 created_at(beijing_time) 同为 naive 本地时间,避免存库时被转成 UTC 早 8 小时
|
||||
now = beijing_time()
|
||||
|
||||
# ★ 完结:库管将「已通过(1)」的申请单置为「已完结(4)」(仿出库审批)
|
||||
if action == 'close':
|
||||
|
||||
Reference in New Issue
Block a user