This commit is contained in:
HuangHai
2026-01-31 07:40:07 +08:00
parent e3e5d76299
commit 54f1b2fb91
5 changed files with 70 additions and 16 deletions

View File

@@ -67,26 +67,41 @@ def parse_wechat_time(time_str):
dt = datetime.combine(yesterday, datetime.min.time().replace(hour=h, minute=m))
return dt.strftime("%Y-%m-%d %H:%M")
# 3. 星期X HH:mm
weekdays = {"星期一": 0, "星期二": 1, "星期三": 2, "星期四": 3, "星期五": 4, "星期六": 5, "星期日": 6}
for w_str, w_idx in weekdays.items():
# 3. 星期X / 周X HH:mm
weekdays_map = {
"星期一": 0, "星期二": 1, "星期三": 2, "星期四": 3, "星期五": 4, "星期六": 5, "星期日": 6,
"周一": 0, "周二": 1, "周三": 2, "周四": 3, "周五": 4, "周六": 5, "周日": 6
}
for w_str, w_idx in weekdays_map.items():
if w_str in clean_str:
# 提取时间部分 (支持 "周三 10:03" 或 "周三10:03")
t_part = clean_str.replace(w_str, "").strip()
if re.match(r'^\d{1,2}:\d{2}$', t_part):
h, m = map(int, t_part.split(':'))
current_weekday = now.weekday()
# 计算日期回退天数 (mod 7 确保是过去的一周内)
delta_days = (current_weekday - w_idx) % 7
# 如果 delta_days 是 0 且当前时间比消息时间早 (不可能发生,除非穿越),说明是今天
# 但通常"星期X"不显示今天,今天显示 HH:mm
# 如果 delta_days == 0,可能是上周的今天?微信通常显示 "上周X"?
# 简单起见,认为是今天或过去7天内的那天
if delta_days == 0 and datetime.now().time() < datetime.min.time().replace(hour=h, minute=m):
delta_days = 7 # 上周
target_date = today - timedelta(days=delta_days)
time_match = re.search(r'(\d{1,2}):(\d{2})', t_part)
h, m = 0, 0
if time_match:
h, m = map(int, time_match.groups())
current_weekday = now.weekday()
# 计算日期回退天数 (mod 7 确保是过去的一周内)
delta_days = (current_weekday - w_idx) % 7
# 如果 delta_days 是 0,且当前时间比解析出的时间早,说明是上周的今天
# 微信通常只有在真的“过去”才会显示星期几
if delta_days == 0 and time_match:
if now.hour < h or (now.hour == h and now.minute < m):
delta_days = 7
elif delta_days == 0 and not time_match:
# 只有“周三”没有时间,通常指最近的一个周三(如果今天是周三,可能指上周三)
# 但为了简单,如果今天是周三且没时间,我们暂定为今天
pass
target_date = today - timedelta(days=delta_days)
if time_match:
dt = datetime.combine(target_date, datetime.min.time().replace(hour=h, minute=m))
return dt.strftime("%Y-%m-%d %H:%M")
else:
return target_date.strftime("%Y-%m-%d 00:00")
# 4. YYYY年MM月DD日 HH:mm
# 简单匹配年月日