fix: 修正服务端 UTC LocalDateTime 在浏览器中的解析偏差
Jackson 配置 time-zone: UTC,LocalDateTime 序列化为不含时区后缀的 ISO 字符串(如 "2026-05-21T14:30:00")。浏览器将此类字符串视为本地 时间,导致非中国时区或非 UTC+8 环境出现 8 小时偏差。 修复:formatTime 对无时区后缀的 ISO 字符串补充 'Z',确保按 UTC 解析 后再转换为北京时间。 Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
这个提交包含在:
父节点
f3c58866df
当前提交
cfe8d19da9
@ -1,7 +1,14 @@
|
|||||||
const TZ = 'Asia/Shanghai'
|
const TZ = 'Asia/Shanghai'
|
||||||
|
|
||||||
|
function toUTC(value: string): string {
|
||||||
|
return /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}/.test(value) && !/[Z+\-]\d*$/.test(value)
|
||||||
|
? value + 'Z'
|
||||||
|
: value
|
||||||
|
}
|
||||||
|
|
||||||
export function formatTime(value: string | number | null | undefined): string {
|
export function formatTime(value: string | number | null | undefined): string {
|
||||||
if (value === null || value === undefined || value === '') return '-'
|
if (value === null || value === undefined || value === '') return '-'
|
||||||
const date = new Date(value as string | number)
|
const input = typeof value === 'string' ? toUTC(value) : value
|
||||||
|
const date = new Date(input)
|
||||||
return Number.isNaN(date.getTime()) ? '-' : date.toLocaleString('zh-CN', { timeZone: TZ })
|
return Number.isNaN(date.getTime()) ? '-' : date.toLocaleString('zh-CN', { timeZone: TZ })
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,7 +1,17 @@
|
|||||||
const TZ = 'Asia/Shanghai'
|
const TZ = 'Asia/Shanghai'
|
||||||
|
|
||||||
|
// Backend serializes LocalDateTime without timezone (Jackson time-zone: UTC).
|
||||||
|
// Bare ISO strings like "2026-05-21T14:30:00" are treated as local time by browsers,
|
||||||
|
// so we append 'Z' to force UTC parsing before converting to Beijing time.
|
||||||
|
function toUTC(value: string): string {
|
||||||
|
return /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}/.test(value) && !/[Z+\-]\d*$/.test(value)
|
||||||
|
? value + 'Z'
|
||||||
|
: value
|
||||||
|
}
|
||||||
|
|
||||||
export function formatTime(value: string | number | null | undefined): string {
|
export function formatTime(value: string | number | null | undefined): string {
|
||||||
if (value === null || value === undefined || value === '') return '-'
|
if (value === null || value === undefined || value === '') return '-'
|
||||||
const date = new Date(value as string | number)
|
const input = typeof value === 'string' ? toUTC(value) : value
|
||||||
|
const date = new Date(input)
|
||||||
return Number.isNaN(date.getTime()) ? '-' : date.toLocaleString('zh-CN', { timeZone: TZ })
|
return Number.isNaN(date.getTime()) ? '-' : date.toLocaleString('zh-CN', { timeZone: TZ })
|
||||||
}
|
}
|
||||||
|
|||||||
正在加载...
在新工单中引用
屏蔽一个用户