-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtime_parser.py
More file actions
55 lines (47 loc) · 1.4 KB
/
Copy pathtime_parser.py
File metadata and controls
55 lines (47 loc) · 1.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import re
from typing import Optional
def parse_chinese_time(text: str) -> Optional[int]:
"""
从中文文本中解析时间,返回总秒数。
支持格式:
- "1小时48秒" -> 3648
- "2小时30分钟" -> 9000
- "15分30秒" -> 930
- "还需时间1小时48秒" -> 3648
- "3天2小时" -> 262800
"""
total = 0
found = False
day_match = re.search(r'(\d+)\s*天', text)
hour_match = re.search(r'(\d+)\s*小时', text)
min_match = re.search(r'(\d+)\s*分(?:钟)?', text)
sec_match = re.search(r'(\d+)\s*秒', text)
if day_match:
total += int(day_match.group(1)) * 86400
found = True
if hour_match:
total += int(hour_match.group(1)) * 3600
found = True
if min_match:
total += int(min_match.group(1)) * 60
found = True
if sec_match:
total += int(sec_match.group(1))
found = True
return total if found else None
def format_seconds(seconds: int) -> str:
"""将秒数格式化为人类可读字符串"""
if seconds <= 0:
return '0秒'
parts = []
if seconds >= 3600:
h = seconds // 3600
parts.append(f'{h}小时')
seconds %= 3600
if seconds >= 60:
m = seconds // 60
parts.append(f'{m}分钟')
seconds %= 60
if seconds > 0:
parts.append(f'{seconds}秒')
return ''.join(parts)