修改时间时区问题

This commit is contained in:
dxc
2026-02-05 14:36:36 +08:00
parent 0bc47d306d
commit 4f90e02dcf
4 changed files with 40 additions and 16 deletions

View File

@ -3,7 +3,7 @@ from app.extensions import db
from app.models.inbound.buy import StockBuy
from app.models.base import MaterialBase
from app.models.outbound import TransOutbound
from datetime import datetime
from datetime import datetime, timedelta, timezone # [修改] 引入 timezone
from sqlalchemy import or_, func, text, and_
import traceback
import json
@ -40,7 +40,7 @@ class BuyInboundService:
return []
# ============================================================
# 2. 新增入库逻辑 (修改:精确到时间)
# 2. 新增入库逻辑 (强制北京时间)
# ============================================================
@staticmethod
def handle_inbound(data):
@ -51,8 +51,11 @@ class BuyInboundService:
material = MaterialBase.query.get(base_id)
if not material: raise ValueError("物料不存在")
# [核心修改] 默认使用当前时间(含时分秒),不再截取 .date()
current_time = datetime.now()
# [核心修改] 获取当前北京时间 (UTC+8)
# 无论服务器在 UTC 还是其他时区,这里强制转换为 UTC+8 并去掉时区信息存入数据库
beijing_tz = timezone(timedelta(hours=8))
current_time = datetime.now(beijing_tz).replace(tzinfo=None)
in_date_val = current_time
if data.get('in_date'):
@ -62,12 +65,12 @@ class BuyInboundService:
if len(date_str) > 10:
in_date_val = datetime.strptime(date_str, '%Y-%m-%d %H:%M:%S')
else:
# 如果只传了日期,手动补上当前时间的时分秒,保证同日入库的排序正确
# 如果只传了日期,使用该日期 + 当前北京时间的时分秒
d_temp = datetime.strptime(date_str, '%Y-%m-%d')
in_date_val = datetime(d_temp.year, d_temp.month, d_temp.day,
current_time.hour, current_time.minute, current_time.second)
except:
# 解析失败则使用当前时间作为兜底
# 解析失败则使用当前北京时间
in_date_val = current_time
in_qty = float(data.get('in_quantity') or 0)

View File

@ -2,7 +2,7 @@
from app.extensions import db
from app.models.base import MaterialBase
from app.models.outbound import TransOutbound
from datetime import datetime
from datetime import datetime, timedelta, timezone # [修改]
from sqlalchemy import or_, func, text, and_
import traceback
import json
@ -33,7 +33,7 @@ class ProductInboundService:
return []
# ============================================================
# 2. 新增入库逻辑 (修改:精确到时间)
# 2. 新增入库逻辑 (强制北京时间)
# ============================================================
@staticmethod
def handle_inbound(data):
@ -45,8 +45,10 @@ class ProductInboundService:
material = MaterialBase.query.get(base_id)
if not material: raise ValueError("物料不存在")
# [核心修改] 处理 production_date包含时分秒
current_time = datetime.now()
# [核心修改] 强制北京时间
beijing_tz = timezone(timedelta(hours=8))
current_time = datetime.now(beijing_tz).replace(tzinfo=None)
in_date_val = current_time
if data.get('in_date'):

View File

@ -2,7 +2,7 @@
from app.extensions import db
from app.models.base import MaterialBase
from app.models.outbound import TransOutbound
from datetime import datetime
from datetime import datetime, timedelta, timezone # [修改]
from sqlalchemy import or_, func, text, and_
import traceback
import json
@ -52,8 +52,10 @@ class SemiInboundService:
if not material:
raise ValueError(f"ID为 {base_id} 的基础物料不存在")
# [核心修改] 处理入库日期(production_date),包含时分秒
current_time = datetime.now()
# [核心修改] 强制北京时间
beijing_tz = timezone(timedelta(hours=8))
current_time = datetime.now(beijing_tz).replace(tzinfo=None)
in_date_val = current_time
if data.get('in_date'):