fix(XQ0037): 修复时间解析与时区处理导致的写入错误
新增北京时间统一处理工具函数,修复带时区时间写入openpyxl报错问题 同时更新插件版本至1.1.2
This commit is contained in:
@@ -119,9 +119,47 @@ TARGET_DATETIME_COLS = ("调动时间", "操作时间")
|
||||
# 第33列起(跨序列/是否跨门店/类型判断/是否需要发起改签/离职类型/前序发起日期)为预填公式列,
|
||||
# 第42列为人工备注列 —— 一概不清不动(公式已预填,由 WPS 重算出值)
|
||||
VALUE_COL_END = 32
|
||||
# 业务本地时区:台账/推送里的时间都是北京时间墙钟
|
||||
LOCAL_TZ = "Asia/Shanghai"
|
||||
|
||||
|
||||
# ==================== 日期工具 ====================
|
||||
def _parse_one_local(v):
|
||||
"""单个值解析成北京时间墙钟 naive(带时区的先转 Asia/Shanghai 再去 tzinfo)。"""
|
||||
if pd.isna(v):
|
||||
return pd.NaT
|
||||
try:
|
||||
ts = pd.Timestamp(str(v).strip())
|
||||
except (ValueError, TypeError):
|
||||
return pd.NaT
|
||||
if ts.tz is not None:
|
||||
ts = ts.tz_convert(LOCAL_TZ).tz_localize(None)
|
||||
return ts
|
||||
|
||||
|
||||
def to_local_naive(series: pd.Series) -> pd.Series:
|
||||
"""把时间列统一成「北京时间墙钟、无时区」的 datetime。
|
||||
|
||||
台账导出的时间是北京时间墙钟(naive),原样保留不做换算;
|
||||
若上游给的是带时区的字符串(+08:00 / Z),先转 Asia/Shanghai 再去 tzinfo。
|
||||
|
||||
必须显式归一化的原因:openpyxl 写 tz-aware datetime 会抛 TypeError,
|
||||
而 WPS/Excel COM 写 naive datetime 会按本地时区转 UTC 落盘(整列 -8 小时)。
|
||||
"""
|
||||
try:
|
||||
s = pd.to_datetime(series, errors="coerce")
|
||||
except (ValueError, TypeError):
|
||||
return series.map(_parse_one_local)
|
||||
if isinstance(s.dtype, pd.DatetimeTZDtype):
|
||||
return s.dt.tz_convert(LOCAL_TZ).dt.tz_localize(None)
|
||||
# 同列混有 naive 与带时区时,pandas 会把带时区的行判成 NaT,逐值兜底
|
||||
bad = s.isna() & series.notna()
|
||||
if bad.any():
|
||||
s = s.copy()
|
||||
s[bad] = series[bad].map(_parse_one_local)
|
||||
return s
|
||||
|
||||
|
||||
def _shift_months(today: date, months: int) -> date:
|
||||
"""today 往前推 months 个自然月(同日,月末自动收敛,如 3-31 -> 2-28)。"""
|
||||
y, m = today.year, today.month - months
|
||||
@@ -164,7 +202,7 @@ def filter_recent_rows(raw_path: str, output_path: str,
|
||||
f"台账缺少「{OPERAT_TIME_COL}」列,实际列:{list(df.columns)}。"
|
||||
"请确认导出模板是否正确。")
|
||||
|
||||
operat = pd.to_datetime(df[OPERAT_TIME_COL].str.strip(), errors="coerce")
|
||||
operat = to_local_naive(df[OPERAT_TIME_COL].astype(str).str.strip())
|
||||
invalid = int(operat.isna().sum())
|
||||
if invalid:
|
||||
logger.warning("「%s」列有 %d 行无法解析为时间(将按不命中处理)",
|
||||
@@ -261,7 +299,7 @@ def write_push_values(target_path: str, df: pd.DataFrame) -> int:
|
||||
df_out["序号"] = range(1, len(df_out) + 1)
|
||||
for col in TARGET_DATETIME_COLS:
|
||||
if col in df_out.columns:
|
||||
df_out[col] = pd.to_datetime(df_out[col], errors="coerce")
|
||||
df_out[col] = to_local_naive(df_out[col])
|
||||
expected = [str(c) for c in df_out.columns]
|
||||
|
||||
target_path = os.path.abspath(target_path)
|
||||
@@ -316,6 +354,9 @@ def write_push_values(target_path: str, df: pd.DataFrame) -> int:
|
||||
v = None
|
||||
elif isinstance(v, pd.Timestamp):
|
||||
v = v.to_pydatetime()
|
||||
if isinstance(v, datetime) and v.tzinfo is not None:
|
||||
# 兜底:任何情况下都不带时区落盘(tz-aware 会让 openpyxl 抛错)
|
||||
v = v.replace(tzinfo=None)
|
||||
cell = ws.cell(row=r, column=c)
|
||||
if isinstance(v, datetime) and cell.number_format == "General":
|
||||
cell.number_format = fmt_by_col.get(c, "yyyy/mm/dd hh:mm:ss")
|
||||
|
||||
Reference in New Issue
Block a user