fix: WhatsApp 时间解析正则与行匹配正则宽严一致性修复

- 逗号替换为空格而非删除,避免日期时间粘连导致解析失败
- AM/PM 前改为 \s* 与行匹配正则一致,支持无空格格式
- 移除冗余的 is12HourFormat 预检测变量

Made-with: Cursor
This commit is contained in:
digua
2026-04-11 17:49:39 +08:00
parent ffab2d3006
commit ae975452cf
@@ -180,10 +180,13 @@ function detectMessageType(content: string): MessageType {
function parseWhatsAppTime(timeStr: string, isV2Format: boolean = false): number { function parseWhatsAppTime(timeStr: string, isV2Format: boolean = false): number {
if (isV2Format) { if (isV2Format) {
// 方括号格式:规范化特殊空格(Thin Space U+2009, NNBSP U+202F)和逗号 // 方括号格式:规范化特殊空格(Thin Space U+2009, NNBSP U+202F)和逗号
const normalizedStr = timeStr.replace(/[\u2009\u202F]/g, ' ').replace(',', '') // 逗号替换为空格(非删除),确保日期和时间之间总有分隔
// 检测 12 小时制(AM/PM const normalizedStr = timeStr
const is12HourFormat = /\s\d{1,2}:\d{2}:\d{2}\s+(?:AM|PM)$/i.test(normalizedStr) .replace(/[\u2009\u202F]/g, ' ')
const match = normalizedStr.match(/^(\d{1,4})\/(\d{1,2})\/(\d{2,4}) (\d{1,2}):(\d{2}):(\d{2})(?:\s+(AM|PM))?$/i) .replace(',', ' ')
.replace(/\s+/g, ' ')
.trim()
const match = normalizedStr.match(/^(\d{1,4})\/(\d{1,2})\/(\d{2,4}) (\d{1,2}):(\d{2}):(\d{2})(?:\s*(AM|PM))?$/i)
if (match) { if (match) {
const [, part1, part2, part3, hour, minute, second, ampm] = match const [, part1, part2, part3, hour, minute, second, ampm] = match
let year: number, month: number, day: number, hourNum: number let year: number, month: number, day: number, hourNum: number
@@ -206,7 +209,7 @@ function parseWhatsAppTime(timeStr: string, isV2Format: boolean = false): number
} }
hourNum = parseInt(hour, 10) hourNum = parseInt(hour, 10)
if (is12HourFormat && ampm) { if (ampm) {
if (ampm.toUpperCase() === 'PM' && hourNum !== 12) { if (ampm.toUpperCase() === 'PM' && hourNum !== 12) {
hourNum += 12 hourNum += 12
} else if (ampm.toUpperCase() === 'AM' && hourNum === 12) { } else if (ampm.toUpperCase() === 'AM' && hourNum === 12) {