timecore 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- timecore/__init__.py +123 -0
- timecore/_internal/__init__.py +1 -0
- timecore/availability/__init__.py +7 -0
- timecore/availability/availability.py +360 -0
- timecore/calendar/__init__.py +13 -0
- timecore/calendar/_common.py +71 -0
- timecore/calendar/business.py +326 -0
- timecore/calendar/calendar.py +92 -0
- timecore/calendar/holidays.py +168 -0
- timecore/clock/__init__.py +14 -0
- timecore/clock/clock.py +138 -0
- timecore/clock/timer.py +142 -0
- timecore/core/__init__.py +26 -0
- timecore/core/_common.py +345 -0
- timecore/core/date.py +257 -0
- timecore/core/duration.py +264 -0
- timecore/core/instant.py +265 -0
- timecore/core/local_datetime.py +347 -0
- timecore/core/period.py +367 -0
- timecore/core/time.py +250 -0
- timecore/core/timezone.py +125 -0
- timecore/core/weekday.py +22 -0
- timecore/core/zoned_datetime.py +442 -0
- timecore/errors.py +48 -0
- timecore/formatting/__init__.py +10 -0
- timecore/formatting/format.py +249 -0
- timecore/interval/__init__.py +8 -0
- timecore/interval/interval.py +478 -0
- timecore/interval/interval_set.py +243 -0
- timecore/parsing/__init__.py +25 -0
- timecore/parsing/parse.py +477 -0
- timecore/py.typed +0 -0
- timecore/recurrence/__init__.py +8 -0
- timecore/recurrence/_shared.py +140 -0
- timecore/recurrence/cron.py +306 -0
- timecore/recurrence/recurrence.py +995 -0
- timecore/schedule/__init__.py +7 -0
- timecore/schedule/schedule.py +519 -0
- timecore-0.1.0.dist-info/METADATA +111 -0
- timecore-0.1.0.dist-info/RECORD +41 -0
- timecore-0.1.0.dist-info/WHEEL +4 -0
timecore/core/period.py
ADDED
|
@@ -0,0 +1,367 @@
|
|
|
1
|
+
"""Period: a calendar-based amount of time.
|
|
2
|
+
|
|
3
|
+
A :class:`Period` is a date/time amount whose length depends on where it
|
|
4
|
+
is applied: 1 month from January 31 is February 28 (or 29), and 1 day
|
|
5
|
+
across a DST transition is a calendar day, not necessarily 24 hours.
|
|
6
|
+
For exact, calendar-independent amounts, see
|
|
7
|
+
:class:`~timecore.core.Duration`.
|
|
8
|
+
|
|
9
|
+
Design note: a period stores its ten components as plain integers.
|
|
10
|
+
Whenever's ``ItemizedDelta`` cannot represent mixed-sign components
|
|
11
|
+
(e.g. the result of ``Period(years=1) - Period(months=1)``), but
|
|
12
|
+
whenever's ``add()`` keyword arguments accept them, so periods are
|
|
13
|
+
converted to ``add()`` kwargs only when they are applied to a date or
|
|
14
|
+
datetime.
|
|
15
|
+
"""
|
|
16
|
+
|
|
17
|
+
from __future__ import annotations
|
|
18
|
+
|
|
19
|
+
import whenever
|
|
20
|
+
|
|
21
|
+
from ..errors import ParseError
|
|
22
|
+
from ._common import _require_int, _require_str, _split_subsecond
|
|
23
|
+
|
|
24
|
+
__all__ = ["Period"]
|
|
25
|
+
|
|
26
|
+
_COMPONENTS = (
|
|
27
|
+
"years",
|
|
28
|
+
"months",
|
|
29
|
+
"weeks",
|
|
30
|
+
"days",
|
|
31
|
+
"hours",
|
|
32
|
+
"minutes",
|
|
33
|
+
"seconds",
|
|
34
|
+
"milliseconds",
|
|
35
|
+
"microseconds",
|
|
36
|
+
"nanoseconds",
|
|
37
|
+
)
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
class Period:
|
|
41
|
+
"""A calendar-based amount of time, e.g. 1 year, 2 months, 3 days.
|
|
42
|
+
|
|
43
|
+
Periods are compared and added component by component: ``Period(days=7)``
|
|
44
|
+
is not equal to ``Period(weeks=1)``, and neither can be converted to a
|
|
45
|
+
total number of seconds -- their length depends on the date they are
|
|
46
|
+
applied to.
|
|
47
|
+
|
|
48
|
+
Example:
|
|
49
|
+
>>> Period(years=1, months=2, days=3)
|
|
50
|
+
Period(years=1, months=2, days=3)
|
|
51
|
+
"""
|
|
52
|
+
|
|
53
|
+
__slots__ = (
|
|
54
|
+
"_days",
|
|
55
|
+
"_hours",
|
|
56
|
+
"_microseconds",
|
|
57
|
+
"_milliseconds",
|
|
58
|
+
"_minutes",
|
|
59
|
+
"_months",
|
|
60
|
+
"_nanoseconds",
|
|
61
|
+
"_seconds",
|
|
62
|
+
"_weeks",
|
|
63
|
+
"_years",
|
|
64
|
+
)
|
|
65
|
+
|
|
66
|
+
def __init__(
|
|
67
|
+
self,
|
|
68
|
+
years: int = 0,
|
|
69
|
+
months: int = 0,
|
|
70
|
+
weeks: int = 0,
|
|
71
|
+
days: int = 0,
|
|
72
|
+
hours: int = 0,
|
|
73
|
+
minutes: int = 0,
|
|
74
|
+
seconds: int = 0,
|
|
75
|
+
milliseconds: int = 0,
|
|
76
|
+
microseconds: int = 0,
|
|
77
|
+
nanoseconds: int = 0,
|
|
78
|
+
) -> None:
|
|
79
|
+
for name, value in [
|
|
80
|
+
("years", years),
|
|
81
|
+
("months", months),
|
|
82
|
+
("weeks", weeks),
|
|
83
|
+
("days", days),
|
|
84
|
+
("hours", hours),
|
|
85
|
+
("minutes", minutes),
|
|
86
|
+
("seconds", seconds),
|
|
87
|
+
("milliseconds", milliseconds),
|
|
88
|
+
("microseconds", microseconds),
|
|
89
|
+
("nanoseconds", nanoseconds),
|
|
90
|
+
]:
|
|
91
|
+
_require_int(value, f"Period() {name}")
|
|
92
|
+
# Fold the sub-second components into seconds + a remainder below
|
|
93
|
+
# one second, so e.g. milliseconds=1500 becomes seconds=1,
|
|
94
|
+
# milliseconds=500.
|
|
95
|
+
total_sub_ns = milliseconds * 1_000_000 + microseconds * 1_000 + nanoseconds
|
|
96
|
+
sign = -1 if total_sub_ns < 0 else 1
|
|
97
|
+
carry, remainder = divmod(abs(total_sub_ns), 1_000_000_000)
|
|
98
|
+
seconds += sign * carry
|
|
99
|
+
milliseconds, microseconds, nanoseconds = _split_subsecond(sign * remainder)
|
|
100
|
+
self._years = years
|
|
101
|
+
self._months = months
|
|
102
|
+
self._weeks = weeks
|
|
103
|
+
self._days = days
|
|
104
|
+
self._hours = hours
|
|
105
|
+
self._minutes = minutes
|
|
106
|
+
self._seconds = seconds
|
|
107
|
+
self._milliseconds = milliseconds
|
|
108
|
+
self._microseconds = microseconds
|
|
109
|
+
self._nanoseconds = nanoseconds
|
|
110
|
+
|
|
111
|
+
@classmethod
|
|
112
|
+
def _from_components(cls, values: dict[str, int]) -> Period:
|
|
113
|
+
"""Build a :class:`Period` from a component dict (internal constructor).
|
|
114
|
+
|
|
115
|
+
Goes through the normal constructor so sub-second components are
|
|
116
|
+
folded into seconds as usual.
|
|
117
|
+
"""
|
|
118
|
+
return cls(**{name: values.get(name, 0) for name in _COMPONENTS})
|
|
119
|
+
|
|
120
|
+
@classmethod
|
|
121
|
+
def parse(cls, text: str) -> Period:
|
|
122
|
+
"""Parse an ISO 8601 period such as ``"P1Y2M3D"`` or ``"PT2H30M"``.
|
|
123
|
+
|
|
124
|
+
Raises:
|
|
125
|
+
ParseError: If ``text`` is not a valid ISO 8601 duration.
|
|
126
|
+
"""
|
|
127
|
+
text = _require_str(text, "Period.parse()")
|
|
128
|
+
try:
|
|
129
|
+
delta = whenever.ItemizedDelta.parse_iso(text)
|
|
130
|
+
except ValueError as exc:
|
|
131
|
+
raise ParseError(f"could not parse {text!r} as an ISO 8601 period") from exc
|
|
132
|
+
milliseconds, microseconds, nanoseconds = _split_subsecond(
|
|
133
|
+
delta.get("nanoseconds") or 0
|
|
134
|
+
)
|
|
135
|
+
return cls(
|
|
136
|
+
years=delta.get("years") or 0,
|
|
137
|
+
months=delta.get("months") or 0,
|
|
138
|
+
weeks=delta.get("weeks") or 0,
|
|
139
|
+
days=delta.get("days") or 0,
|
|
140
|
+
hours=delta.get("hours") or 0,
|
|
141
|
+
minutes=delta.get("minutes") or 0,
|
|
142
|
+
seconds=delta.get("seconds") or 0,
|
|
143
|
+
milliseconds=milliseconds,
|
|
144
|
+
microseconds=microseconds,
|
|
145
|
+
nanoseconds=nanoseconds,
|
|
146
|
+
)
|
|
147
|
+
|
|
148
|
+
def _component(self, name: str) -> int:
|
|
149
|
+
return getattr(self, f"_{name}")
|
|
150
|
+
|
|
151
|
+
@property
|
|
152
|
+
def years(self) -> int:
|
|
153
|
+
"""The years component."""
|
|
154
|
+
return self._years
|
|
155
|
+
|
|
156
|
+
@property
|
|
157
|
+
def months(self) -> int:
|
|
158
|
+
"""The months component."""
|
|
159
|
+
return self._months
|
|
160
|
+
|
|
161
|
+
@property
|
|
162
|
+
def weeks(self) -> int:
|
|
163
|
+
"""The weeks component."""
|
|
164
|
+
return self._weeks
|
|
165
|
+
|
|
166
|
+
@property
|
|
167
|
+
def days(self) -> int:
|
|
168
|
+
"""The days component."""
|
|
169
|
+
return self._days
|
|
170
|
+
|
|
171
|
+
@property
|
|
172
|
+
def hours(self) -> int:
|
|
173
|
+
"""The hours component."""
|
|
174
|
+
return self._hours
|
|
175
|
+
|
|
176
|
+
@property
|
|
177
|
+
def minutes(self) -> int:
|
|
178
|
+
"""The minutes component."""
|
|
179
|
+
return self._minutes
|
|
180
|
+
|
|
181
|
+
@property
|
|
182
|
+
def seconds(self) -> int:
|
|
183
|
+
"""The seconds component."""
|
|
184
|
+
return self._seconds
|
|
185
|
+
|
|
186
|
+
@property
|
|
187
|
+
def milliseconds(self) -> int:
|
|
188
|
+
"""The milliseconds component."""
|
|
189
|
+
return self._milliseconds
|
|
190
|
+
|
|
191
|
+
@property
|
|
192
|
+
def microseconds(self) -> int:
|
|
193
|
+
"""The microseconds component."""
|
|
194
|
+
return self._microseconds
|
|
195
|
+
|
|
196
|
+
@property
|
|
197
|
+
def nanoseconds(self) -> int:
|
|
198
|
+
"""The nanoseconds component."""
|
|
199
|
+
return self._nanoseconds
|
|
200
|
+
|
|
201
|
+
def _as_tuple(self) -> tuple[int, ...]:
|
|
202
|
+
return tuple(self._component(name) for name in _COMPONENTS)
|
|
203
|
+
|
|
204
|
+
def __add__(self, other: object) -> Period:
|
|
205
|
+
if isinstance(other, Period):
|
|
206
|
+
return Period._from_components(
|
|
207
|
+
{
|
|
208
|
+
name: self._component(name) + other._component(name)
|
|
209
|
+
for name in _COMPONENTS
|
|
210
|
+
}
|
|
211
|
+
)
|
|
212
|
+
return NotImplemented
|
|
213
|
+
|
|
214
|
+
def __radd__(self, other: object) -> Period:
|
|
215
|
+
return self.__add__(other)
|
|
216
|
+
|
|
217
|
+
def __sub__(self, other: object) -> Period:
|
|
218
|
+
if isinstance(other, Period):
|
|
219
|
+
return Period._from_components(
|
|
220
|
+
{
|
|
221
|
+
name: self._component(name) - other._component(name)
|
|
222
|
+
for name in _COMPONENTS
|
|
223
|
+
}
|
|
224
|
+
)
|
|
225
|
+
return NotImplemented
|
|
226
|
+
|
|
227
|
+
def __rsub__(self, other: object) -> Period:
|
|
228
|
+
if isinstance(other, Period):
|
|
229
|
+
return Period._from_components(
|
|
230
|
+
{
|
|
231
|
+
name: other._component(name) - self._component(name)
|
|
232
|
+
for name in _COMPONENTS
|
|
233
|
+
}
|
|
234
|
+
)
|
|
235
|
+
return NotImplemented
|
|
236
|
+
|
|
237
|
+
def __neg__(self) -> Period:
|
|
238
|
+
return Period._from_components(
|
|
239
|
+
{name: -self._component(name) for name in _COMPONENTS}
|
|
240
|
+
)
|
|
241
|
+
|
|
242
|
+
def __pos__(self) -> Period:
|
|
243
|
+
return Period._from_components(
|
|
244
|
+
{name: +self._component(name) for name in _COMPONENTS}
|
|
245
|
+
)
|
|
246
|
+
|
|
247
|
+
def __mul__(self, other: object) -> Period:
|
|
248
|
+
if isinstance(other, int) and not isinstance(other, bool):
|
|
249
|
+
return Period._from_components(
|
|
250
|
+
{name: self._component(name) * other for name in _COMPONENTS}
|
|
251
|
+
)
|
|
252
|
+
return NotImplemented
|
|
253
|
+
|
|
254
|
+
def __rmul__(self, other: object) -> Period:
|
|
255
|
+
return self.__mul__(other)
|
|
256
|
+
|
|
257
|
+
def __eq__(self, other: object) -> bool:
|
|
258
|
+
if isinstance(other, Period):
|
|
259
|
+
return self._as_tuple() == other._as_tuple()
|
|
260
|
+
return NotImplemented
|
|
261
|
+
|
|
262
|
+
def __hash__(self) -> int:
|
|
263
|
+
return hash(self._as_tuple())
|
|
264
|
+
|
|
265
|
+
def __bool__(self) -> bool:
|
|
266
|
+
return any(self._component(name) for name in _COMPONENTS)
|
|
267
|
+
|
|
268
|
+
def __repr__(self) -> str:
|
|
269
|
+
parts = [
|
|
270
|
+
(name, self._component(name))
|
|
271
|
+
for name in _COMPONENTS
|
|
272
|
+
if self._component(name)
|
|
273
|
+
]
|
|
274
|
+
if not parts:
|
|
275
|
+
return "Period()"
|
|
276
|
+
inner = ", ".join(f"{name}={value}" for name, value in parts)
|
|
277
|
+
return f"Period({inner})"
|
|
278
|
+
|
|
279
|
+
def __str__(self) -> str:
|
|
280
|
+
"""Return the ISO 8601 form, e.g. ``"P1Y2M3D"``.
|
|
281
|
+
|
|
282
|
+
When all nonzero components share a sign (the usual case), the
|
|
283
|
+
result is strict ISO 8601 (a leading ``-`` for negative periods,
|
|
284
|
+
``"P0D"`` for the zero period). A mixed-sign period -- only
|
|
285
|
+
reachable via arithmetic such as ``Period(years=1) -
|
|
286
|
+
Period(months=1)`` -- has no ISO 8601 form; it is rendered with
|
|
287
|
+
a sign on each component instead (``"P1Y-1M"``).
|
|
288
|
+
"""
|
|
289
|
+
values = {name: self._component(name) for name in _COMPONENTS}
|
|
290
|
+
nonzero = [v for v in values.values() if v]
|
|
291
|
+
if not nonzero:
|
|
292
|
+
return "P0D"
|
|
293
|
+
if all(v > 0 for v in nonzero):
|
|
294
|
+
sign = ""
|
|
295
|
+
elif all(v < 0 for v in nonzero):
|
|
296
|
+
sign = "-"
|
|
297
|
+
values = {name: -v for name, v in values.items()}
|
|
298
|
+
else:
|
|
299
|
+
return self._mixed_sign_str(values)
|
|
300
|
+
date_part = "".join(
|
|
301
|
+
f"{values[name]}{letter}"
|
|
302
|
+
for name, letter in [
|
|
303
|
+
("years", "Y"),
|
|
304
|
+
("months", "M"),
|
|
305
|
+
("weeks", "W"),
|
|
306
|
+
("days", "D"),
|
|
307
|
+
]
|
|
308
|
+
if values[name]
|
|
309
|
+
)
|
|
310
|
+
time_part = "".join(
|
|
311
|
+
f"{values[name]}{letter}"
|
|
312
|
+
for name, letter in [
|
|
313
|
+
("hours", "H"),
|
|
314
|
+
("minutes", "M"),
|
|
315
|
+
("seconds", "S"),
|
|
316
|
+
]
|
|
317
|
+
if values[name]
|
|
318
|
+
)
|
|
319
|
+
# Sub-second part rides on the seconds component.
|
|
320
|
+
sub_ns = (
|
|
321
|
+
values["milliseconds"] * 1_000_000
|
|
322
|
+
+ values["microseconds"] * 1_000
|
|
323
|
+
+ values["nanoseconds"]
|
|
324
|
+
)
|
|
325
|
+
if sub_ns:
|
|
326
|
+
fraction = f"{sub_ns:09d}".rstrip("0")
|
|
327
|
+
if values["seconds"]:
|
|
328
|
+
time_part = time_part[: -len(f"{values['seconds']}S")]
|
|
329
|
+
time_part += f"{values['seconds']}.{fraction}S"
|
|
330
|
+
else:
|
|
331
|
+
time_part += f"0.{fraction}S"
|
|
332
|
+
if time_part:
|
|
333
|
+
return f"{sign}P{date_part}T{time_part}"
|
|
334
|
+
return f"{sign}P{date_part}"
|
|
335
|
+
|
|
336
|
+
@staticmethod
|
|
337
|
+
def _mixed_sign_str(values: dict[str, int]) -> str:
|
|
338
|
+
"""Render a mixed-sign period with a sign on each component."""
|
|
339
|
+
date_parts = [
|
|
340
|
+
f"{values[name]:+d}{letter}"
|
|
341
|
+
for name, letter in [
|
|
342
|
+
("years", "Y"),
|
|
343
|
+
("months", "M"),
|
|
344
|
+
("weeks", "W"),
|
|
345
|
+
("days", "D"),
|
|
346
|
+
]
|
|
347
|
+
if values[name]
|
|
348
|
+
]
|
|
349
|
+
time_parts = [
|
|
350
|
+
f"{values[name]:+d}{letter}"
|
|
351
|
+
for name, letter in [
|
|
352
|
+
("hours", "H"),
|
|
353
|
+
("minutes", "M"),
|
|
354
|
+
("seconds", "S"),
|
|
355
|
+
]
|
|
356
|
+
if values[name]
|
|
357
|
+
]
|
|
358
|
+
# The first component carries no explicit "+" (it is not negative).
|
|
359
|
+
if date_parts:
|
|
360
|
+
date_parts[0] = date_parts[0].removeprefix("+")
|
|
361
|
+
elif time_parts and time_parts[0].startswith("+"):
|
|
362
|
+
time_parts[0] = time_parts[0][1:]
|
|
363
|
+
date_part = "".join(date_parts)
|
|
364
|
+
time_part = "".join(time_parts)
|
|
365
|
+
if time_part:
|
|
366
|
+
return f"P{date_part}T{time_part}"
|
|
367
|
+
return f"P{date_part}"
|
timecore/core/time.py
ADDED
|
@@ -0,0 +1,250 @@
|
|
|
1
|
+
"""Time: a time of day without a date component."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from datetime import time as _StdTime
|
|
6
|
+
from typing import TYPE_CHECKING
|
|
7
|
+
|
|
8
|
+
import whenever
|
|
9
|
+
|
|
10
|
+
from ._common import (
|
|
11
|
+
Clock,
|
|
12
|
+
_check_us_precision,
|
|
13
|
+
_format_value,
|
|
14
|
+
_map_parse_errors,
|
|
15
|
+
_map_whenever_errors,
|
|
16
|
+
_now_instant,
|
|
17
|
+
_require_instance,
|
|
18
|
+
_require_str,
|
|
19
|
+
_resolve_timezone,
|
|
20
|
+
_strptime,
|
|
21
|
+
)
|
|
22
|
+
from .duration import Duration
|
|
23
|
+
|
|
24
|
+
if TYPE_CHECKING:
|
|
25
|
+
from .timezone import TimeZone
|
|
26
|
+
|
|
27
|
+
__all__ = ["Time"]
|
|
28
|
+
|
|
29
|
+
#: Nanoseconds in one day (for midnight wrap-around).
|
|
30
|
+
_NS_PER_DAY = 86_400_000_000_000
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
class Time:
|
|
34
|
+
"""A time of day without a date component, with nanosecond precision.
|
|
35
|
+
|
|
36
|
+
Example:
|
|
37
|
+
>>> Time(10, 30, 15)
|
|
38
|
+
Time(10, 30, 15)
|
|
39
|
+
"""
|
|
40
|
+
|
|
41
|
+
__slots__ = ("_inner",)
|
|
42
|
+
|
|
43
|
+
# The wrapped whenever value; declared here so pyright knows its type.
|
|
44
|
+
_inner: whenever.Time
|
|
45
|
+
|
|
46
|
+
def __init__(
|
|
47
|
+
self,
|
|
48
|
+
hour: int,
|
|
49
|
+
minute: int = 0,
|
|
50
|
+
second: int = 0,
|
|
51
|
+
microsecond: int = 0,
|
|
52
|
+
nanosecond: int = 0,
|
|
53
|
+
) -> None:
|
|
54
|
+
# ``microsecond`` and ``nanosecond`` are separate components that
|
|
55
|
+
# add up to the sub-second part: microsecond=123, nanosecond=456
|
|
56
|
+
# means 123456 nanoseconds past the second.
|
|
57
|
+
total_ns = microsecond * 1_000 + nanosecond
|
|
58
|
+
with _map_whenever_errors():
|
|
59
|
+
self._inner = whenever.Time(hour, minute, second, nanosecond=total_ns)
|
|
60
|
+
|
|
61
|
+
@classmethod
|
|
62
|
+
def from_whenever(cls, inner: whenever.Time) -> Time:
|
|
63
|
+
"""Wrap a whenever ``Time``.
|
|
64
|
+
|
|
65
|
+
Internal constructor; not part of the public API.
|
|
66
|
+
"""
|
|
67
|
+
obj = cls.__new__(cls)
|
|
68
|
+
obj._inner = inner
|
|
69
|
+
return obj
|
|
70
|
+
|
|
71
|
+
@classmethod
|
|
72
|
+
def _from_ns(cls, total_ns: int) -> Time:
|
|
73
|
+
"""Build a :class:`Time` from nanoseconds since midnight (internal helper)."""
|
|
74
|
+
hours, remaining = divmod(total_ns, 3_600_000_000_000)
|
|
75
|
+
minutes, remaining = divmod(remaining, 60_000_000_000)
|
|
76
|
+
seconds, remaining = divmod(remaining, 1_000_000_000)
|
|
77
|
+
microseconds, nanoseconds = divmod(remaining, 1_000)
|
|
78
|
+
return cls(
|
|
79
|
+
hours, minutes, seconds, microsecond=microseconds, nanosecond=nanoseconds
|
|
80
|
+
)
|
|
81
|
+
|
|
82
|
+
def _total_ns(self) -> int:
|
|
83
|
+
return (
|
|
84
|
+
(self.hour * 60 + self.minute) * 60 + self.second
|
|
85
|
+
) * 1_000_000_000 + self.nanosecond
|
|
86
|
+
|
|
87
|
+
@classmethod
|
|
88
|
+
def now(
|
|
89
|
+
cls,
|
|
90
|
+
*,
|
|
91
|
+
timezone: TimeZone | str | None = None,
|
|
92
|
+
clock: Clock | None = None,
|
|
93
|
+
) -> Time:
|
|
94
|
+
"""Return the current time of day in ``timezone`` (default: local zone).
|
|
95
|
+
|
|
96
|
+
Pass ``clock`` (an object with a ``now() -> Instant`` method) to
|
|
97
|
+
determine "now" deterministically, e.g. in tests.
|
|
98
|
+
"""
|
|
99
|
+
instant = _now_instant(clock)
|
|
100
|
+
tz = _resolve_timezone(timezone)
|
|
101
|
+
return instant.to_datetime(tz).to_local_datetime().time()
|
|
102
|
+
|
|
103
|
+
@classmethod
|
|
104
|
+
def parse(cls, text: str, *, format: str | None = None) -> Time:
|
|
105
|
+
"""Parse an ISO 8601 time such as ``"10:30:15"``.
|
|
106
|
+
|
|
107
|
+
With ``format``, parse with a ``strftime`` pattern instead
|
|
108
|
+
(English month names only, locale-independent).
|
|
109
|
+
|
|
110
|
+
Raises:
|
|
111
|
+
ParseError: If ``text`` is not a valid time in the given form.
|
|
112
|
+
"""
|
|
113
|
+
text = _require_str(text, "Time.parse()")
|
|
114
|
+
if format is not None:
|
|
115
|
+
format = _require_str(format, "Time.parse() format")
|
|
116
|
+
parsed = _strptime(text, format)
|
|
117
|
+
return cls(
|
|
118
|
+
parsed.hour,
|
|
119
|
+
parsed.minute,
|
|
120
|
+
parsed.second,
|
|
121
|
+
microsecond=parsed.microsecond,
|
|
122
|
+
)
|
|
123
|
+
with _map_parse_errors():
|
|
124
|
+
return cls.from_whenever(whenever.Time.parse_iso(text))
|
|
125
|
+
|
|
126
|
+
@classmethod
|
|
127
|
+
def from_time(cls, value: _StdTime) -> Time:
|
|
128
|
+
"""Create a :class:`Time` from a standard-library ``time``."""
|
|
129
|
+
value = _require_instance(value, _StdTime, "Time.from_time()")
|
|
130
|
+
with _map_whenever_errors():
|
|
131
|
+
return cls.from_whenever(whenever.Time(value))
|
|
132
|
+
|
|
133
|
+
def to_time(self) -> _StdTime:
|
|
134
|
+
"""Convert to a standard-library ``time``.
|
|
135
|
+
|
|
136
|
+
Raises:
|
|
137
|
+
ValueError: If this time has a sub-microsecond remainder,
|
|
138
|
+
which a stdlib ``time`` cannot represent.
|
|
139
|
+
"""
|
|
140
|
+
_check_us_precision(self.nanosecond, "Time")
|
|
141
|
+
return self._inner.to_stdlib()
|
|
142
|
+
|
|
143
|
+
@property
|
|
144
|
+
def hour(self) -> int:
|
|
145
|
+
"""The hour, 0-23."""
|
|
146
|
+
return self._inner.hour
|
|
147
|
+
|
|
148
|
+
@property
|
|
149
|
+
def minute(self) -> int:
|
|
150
|
+
"""The minute, 0-59."""
|
|
151
|
+
return self._inner.minute
|
|
152
|
+
|
|
153
|
+
@property
|
|
154
|
+
def second(self) -> int:
|
|
155
|
+
"""The second, 0-59."""
|
|
156
|
+
return self._inner.second
|
|
157
|
+
|
|
158
|
+
@property
|
|
159
|
+
def microsecond(self) -> int:
|
|
160
|
+
"""The microsecond component, 0-999999."""
|
|
161
|
+
return self.nanosecond // 1_000
|
|
162
|
+
|
|
163
|
+
@property
|
|
164
|
+
def nanosecond(self) -> int:
|
|
165
|
+
"""The sub-second part in nanoseconds, 0-999999999."""
|
|
166
|
+
return self._inner.nanosecond
|
|
167
|
+
|
|
168
|
+
def isoformat(self) -> str:
|
|
169
|
+
"""Return the ISO 8601 form, e.g. ``"10:30:15"``."""
|
|
170
|
+
return self._inner.format_iso()
|
|
171
|
+
|
|
172
|
+
def format(
|
|
173
|
+
self,
|
|
174
|
+
style: str = "medium",
|
|
175
|
+
locale: str | None = None,
|
|
176
|
+
pattern: str | None = None,
|
|
177
|
+
) -> str:
|
|
178
|
+
"""Format this time (see :func:`timecore.core._common._format_value`).
|
|
179
|
+
|
|
180
|
+
``style`` is one of ``"short"``, ``"medium"``, ``"long"``,
|
|
181
|
+
``"full"``; ``pattern`` is a Babel pattern. The default call
|
|
182
|
+
returns Babel's ``"medium"`` style output.
|
|
183
|
+
"""
|
|
184
|
+
return _format_value(self, style=style, locale=locale, pattern=pattern)
|
|
185
|
+
|
|
186
|
+
def __add__(self, other: object) -> Time:
|
|
187
|
+
if isinstance(other, Duration):
|
|
188
|
+
# Time arithmetic wraps around midnight.
|
|
189
|
+
total_ns = (self._total_ns() + other.total_nanoseconds()) % _NS_PER_DAY
|
|
190
|
+
return Time._from_ns(total_ns)
|
|
191
|
+
return NotImplemented
|
|
192
|
+
|
|
193
|
+
def __radd__(self, other: object) -> Time:
|
|
194
|
+
return self.__add__(other)
|
|
195
|
+
|
|
196
|
+
def __sub__(self, other: object) -> Time | Duration:
|
|
197
|
+
if isinstance(other, Duration):
|
|
198
|
+
total_ns = (self._total_ns() - other.total_nanoseconds()) % _NS_PER_DAY
|
|
199
|
+
return Time._from_ns(total_ns)
|
|
200
|
+
if isinstance(other, Time):
|
|
201
|
+
return Duration.from_whenever(
|
|
202
|
+
whenever.TimeDelta(nanoseconds=self._total_ns() - other._total_ns())
|
|
203
|
+
)
|
|
204
|
+
return NotImplemented
|
|
205
|
+
|
|
206
|
+
def __eq__(self, other: object) -> bool:
|
|
207
|
+
if isinstance(other, Time):
|
|
208
|
+
return self._inner == other._inner
|
|
209
|
+
return NotImplemented
|
|
210
|
+
|
|
211
|
+
def __lt__(self, other: object) -> bool:
|
|
212
|
+
if isinstance(other, Time):
|
|
213
|
+
return self._inner < other._inner
|
|
214
|
+
return NotImplemented
|
|
215
|
+
|
|
216
|
+
def __le__(self, other: object) -> bool:
|
|
217
|
+
if isinstance(other, Time):
|
|
218
|
+
return self._inner <= other._inner
|
|
219
|
+
return NotImplemented
|
|
220
|
+
|
|
221
|
+
def __gt__(self, other: object) -> bool:
|
|
222
|
+
if isinstance(other, Time):
|
|
223
|
+
return self._inner > other._inner
|
|
224
|
+
return NotImplemented
|
|
225
|
+
|
|
226
|
+
def __ge__(self, other: object) -> bool:
|
|
227
|
+
if isinstance(other, Time):
|
|
228
|
+
return self._inner >= other._inner
|
|
229
|
+
return NotImplemented
|
|
230
|
+
|
|
231
|
+
def __hash__(self) -> int:
|
|
232
|
+
return hash(self._inner)
|
|
233
|
+
|
|
234
|
+
def __repr__(self) -> str:
|
|
235
|
+
if self.nanosecond:
|
|
236
|
+
if self.nanosecond % 1_000 == 0:
|
|
237
|
+
return (
|
|
238
|
+
f"Time({self.hour}, {self.minute}, {self.second}, "
|
|
239
|
+
f"microsecond={self.microsecond})"
|
|
240
|
+
)
|
|
241
|
+
return (
|
|
242
|
+
f"Time({self.hour}, {self.minute}, {self.second}, "
|
|
243
|
+
f"microsecond={self.microsecond}, nanosecond={self.nanosecond % 1_000})"
|
|
244
|
+
)
|
|
245
|
+
if self.second:
|
|
246
|
+
return f"Time({self.hour}, {self.minute}, {self.second})"
|
|
247
|
+
return f"Time({self.hour}, {self.minute})"
|
|
248
|
+
|
|
249
|
+
def __str__(self) -> str:
|
|
250
|
+
return self.isoformat()
|