-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathutils.py
More file actions
47 lines (35 loc) · 1.06 KB
/
Copy pathutils.py
File metadata and controls
47 lines (35 loc) · 1.06 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
import os
import time
from datetime import datetime
def set_tz(tz: str) -> None:
os.environ["TZ"] = tz
try:
time.tzset()
except Exception:
pass
def sanitize_one_line(s: str) -> str:
return " ".join(
s.replace("\r", " ").replace("\n", " ").replace("\t", " ").split()
).strip()
def clock_text() -> str:
t = time.strftime("%I:%M")
return t[1:] if t.startswith("0") else t
def _parse_hhmm(s: str) -> tuple[int, int]:
s = (s or "").strip()
try:
hh, mm = s.split(":")
return int(hh), int(mm)
except (ValueError, AttributeError) as exc:
raise ValueError(f"Invalid HH:MM time string: {s!r}") from exc
def is_quiet_hours(off_start: str, on_start: str, now: datetime | None = None) -> bool:
now = now or datetime.now()
h1, m1 = _parse_hhmm(off_start)
h2, m2 = _parse_hhmm(on_start)
off = h1 * 60 + m1
on = h2 * 60 + m2
t = now.hour * 60 + now.minute
if off == on:
return True
if off < on:
return off <= t < on
return (t >= off) or (t < on)