/
/
/
1"""Helpers for date and time."""
2
3from __future__ import annotations
4
5import datetime
6
7LOCAL_TIMEZONE = datetime.datetime.now(datetime.UTC).astimezone().tzinfo
8
9
10def utc() -> datetime.datetime:
11 """Get current UTC datetime."""
12 return datetime.datetime.now(datetime.UTC)
13
14
15def utc_timestamp() -> float:
16 """Return UTC timestamp in seconds as float."""
17 return utc().timestamp()
18
19
20def now() -> datetime.datetime:
21 """Get current datetime in local timezone."""
22 return datetime.datetime.now(LOCAL_TIMEZONE)
23
24
25def now_timestamp() -> float:
26 """Return current datetime as timestamp in local timezone."""
27 return now().timestamp()
28
29
30def future_timestamp(**kwargs: float) -> float:
31 """Return current timestamp + timedelta."""
32 return (now() + datetime.timedelta(**kwargs)).timestamp()
33
34
35def from_utc_timestamp(timestamp: float) -> datetime.datetime:
36 """Return datetime from UTC timestamp."""
37 return datetime.datetime.fromtimestamp(timestamp, datetime.UTC)
38
39
40def iso_from_utc_timestamp(timestamp: float) -> str:
41 """Return ISO 8601 datetime string from UTC timestamp."""
42 return from_utc_timestamp(timestamp).isoformat()
43
44
45def from_iso_string(iso_datetime: str) -> datetime.datetime:
46 """Return datetime from ISO datetime string."""
47 return datetime.datetime.fromisoformat(iso_datetime)
48