-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtimeparse.py
More file actions
98 lines (77 loc) · 3.07 KB
/
Copy pathtimeparse.py
File metadata and controls
98 lines (77 loc) · 3.07 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
from datetime import timedelta
# Note: "m" appears in both minutes and months in the original — minutes wins (first match).
_TIME_UNITS: list[tuple[frozenset[str], callable]] = [
(frozenset(["s", "sec", "secs", "second", "seconds"]), lambda n: timedelta(seconds=n)),
(frozenset(["m", "min", "mins", "minute", "minutes"]), lambda n: timedelta(minutes=n)),
(frozenset(["h", "hr", "hrs", "hour", "hours"]), lambda n: timedelta(hours=n)),
(frozenset(["d", "day", "days"]), lambda n: timedelta(days=n)),
(frozenset(["w", "week", "weeks"]), lambda n: timedelta(weeks=n)),
(frozenset(["month", "months"]), lambda n: timedelta(days=n * 30)),
(frozenset(["y", "yr", "yrs", "year", "years"]), lambda n: timedelta(days=n * 365)),
]
def _split_into_parts(raw: str) -> list[str]:
"""Break string into alternating digit/letter tokens, ignoring commas and whitespace."""
parts: list[str] = []
buf: list[str] = []
for ch in raw:
if ch.isdigit() or ch.isalpha():
if buf and (buf[-1].isalpha() != ch.isalpha()):
parts.append("".join(buf))
buf.clear()
buf.append(ch)
elif ch.isspace() or ch == ",":
if buf:
parts.append("".join(buf))
buf.clear()
else:
raise ValueError(f"Invalid character for duration: {ch!r}")
if buf:
parts.append("".join(buf))
return parts
def parse_duration(raw: str) -> timedelta:
"""
Parse a human-readable duration string into a timedelta.
Examples: "2h 30m", "2 hours 30 minutes", "9000", "2w", "1d 6h"
A bare integer is treated as seconds.
"""
raw = raw.strip()
# Bare integer → seconds
try:
return timedelta(seconds=int(raw))
except ValueError:
pass
parts = _split_into_parts(raw)
if len(parts) % 2 != 0:
raise ValueError(f"Unexpected token sequence in duration: {raw!r}")
result = timedelta()
for i in range(0, len(parts), 2):
count_str, unit_str = parts[i], parts[i + 1]
try:
count = int(count_str)
except ValueError:
raise ValueError(f"Invalid number in duration: {count_str!r}")
unit_fn = next(
(fn for keys, fn in _TIME_UNITS if unit_str.lower() in keys),
None,
)
if unit_fn is None:
raise ValueError(f"Unknown time unit: {unit_str!r}")
result += unit_fn(count)
return result
def human_readable_duration(td: timedelta) -> str:
total = int(td.total_seconds())
if total <= 0:
return "0 seconds"
days = total // 86400
hours = (total % 86400) // 3600
minutes = (total % 3600) // 60
seconds = total % 60
def fmt(n: int, unit: str) -> str:
return f"{n} {unit}{'s' if n != 1 else ''}" if n > 0 else ""
parts = list(filter(None, [
fmt(days, "day"),
fmt(hours, "hour"),
fmt(minutes, "minute"),
fmt(seconds, "second"),
]))
return ", ".join(parts)