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
|
@@ -0,0 +1,243 @@
|
|
|
1
|
+
"""IntervalSet: a sorted collection of disjoint intervals.
|
|
2
|
+
|
|
3
|
+
An :class:`IntervalSet` is a set of points of one temporal type,
|
|
4
|
+
stored as a tuple of disjoint, sorted :class:`Interval` objects.
|
|
5
|
+
Every set operation (union, intersection, difference) accepts either
|
|
6
|
+
an :class:`Interval` or another :class:`IntervalSet` and always
|
|
7
|
+
returns an :class:`IntervalSet`, so results compose freely.
|
|
8
|
+
|
|
9
|
+
Set algebra is computed with the ``portion`` library internally; no
|
|
10
|
+
``portion`` type or exception ever reaches the public API.
|
|
11
|
+
"""
|
|
12
|
+
|
|
13
|
+
from __future__ import annotations
|
|
14
|
+
|
|
15
|
+
from collections.abc import Iterable, Iterator
|
|
16
|
+
from typing import Any
|
|
17
|
+
|
|
18
|
+
import portion # type: ignore[import-untyped] # portion ships no type stubs
|
|
19
|
+
|
|
20
|
+
from ..core import Date, Duration
|
|
21
|
+
from ..errors import InvalidIntervalError
|
|
22
|
+
from .interval import (
|
|
23
|
+
Endpoint,
|
|
24
|
+
Interval,
|
|
25
|
+
from_portion_atom,
|
|
26
|
+
require_endpoint,
|
|
27
|
+
require_interval,
|
|
28
|
+
)
|
|
29
|
+
|
|
30
|
+
__all__ = ["IntervalSet"]
|
|
31
|
+
|
|
32
|
+
# See interval.py: portion ships no type stubs, so all portion access
|
|
33
|
+
# goes through this untyped alias to keep Unknowns contained.
|
|
34
|
+
_portion: Any = portion
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
class IntervalSet:
|
|
38
|
+
"""A sorted set of disjoint intervals with one endpoint type.
|
|
39
|
+
|
|
40
|
+
Example:
|
|
41
|
+
>>> IntervalSet([Interval(Date(2026, 1, 1), Date(2026, 1, 3))])
|
|
42
|
+
IntervalSet([Interval(Date(2026, 1, 1), Date(2026, 1, 3))])
|
|
43
|
+
"""
|
|
44
|
+
|
|
45
|
+
__slots__ = ("_endpoint_type", "_portion")
|
|
46
|
+
|
|
47
|
+
_portion: Any
|
|
48
|
+
_endpoint_type: type[Endpoint] | None
|
|
49
|
+
|
|
50
|
+
def __init__(self, intervals: Interval | Iterable[Interval] = ()) -> None:
|
|
51
|
+
"""Create a set from ``intervals`` (an iterable of Interval).
|
|
52
|
+
|
|
53
|
+
Overlapping or adjacent intervals are merged and the members
|
|
54
|
+
are sorted, so the stored intervals are always disjoint. All
|
|
55
|
+
intervals must share one endpoint type; mixing types raises
|
|
56
|
+
:class:`~timecore.errors.InvalidIntervalError`. An empty
|
|
57
|
+
iterable (the default) makes an empty set with no endpoint
|
|
58
|
+
type yet.
|
|
59
|
+
"""
|
|
60
|
+
if isinstance(intervals, Interval):
|
|
61
|
+
intervals = (intervals,)
|
|
62
|
+
atoms: list[Any] = []
|
|
63
|
+
endpoint_type: type[Endpoint] | None = None
|
|
64
|
+
for interval in intervals:
|
|
65
|
+
interval = require_interval(interval, "IntervalSet()")
|
|
66
|
+
if endpoint_type is None:
|
|
67
|
+
endpoint_type = type(interval.start)
|
|
68
|
+
elif type(interval.start) is not endpoint_type:
|
|
69
|
+
raise InvalidIntervalError(
|
|
70
|
+
"IntervalSet() requires intervals with the same "
|
|
71
|
+
f"endpoint type; got {endpoint_type.__name__} and "
|
|
72
|
+
f"{type(interval.start).__name__}."
|
|
73
|
+
)
|
|
74
|
+
if not interval.is_empty:
|
|
75
|
+
# _to_portion is private to Interval; this is the
|
|
76
|
+
# subpackage-internal conversion for set construction.
|
|
77
|
+
to_portion = interval._to_portion # pyright: ignore[reportPrivateUsage]
|
|
78
|
+
atoms.append(to_portion())
|
|
79
|
+
self._portion = _portion.Interval(*atoms) if atoms else _portion.empty()
|
|
80
|
+
self._endpoint_type = endpoint_type
|
|
81
|
+
|
|
82
|
+
@classmethod
|
|
83
|
+
def _from_portion(
|
|
84
|
+
cls, value: Any, endpoint_type: type[Endpoint] | None
|
|
85
|
+
) -> IntervalSet:
|
|
86
|
+
"""Internal constructor from an already-normalized ``portion`` set."""
|
|
87
|
+
obj = cls.__new__(cls)
|
|
88
|
+
obj._portion = value
|
|
89
|
+
obj._endpoint_type = endpoint_type
|
|
90
|
+
return obj
|
|
91
|
+
|
|
92
|
+
@property
|
|
93
|
+
def intervals(self) -> tuple[Interval, ...]:
|
|
94
|
+
"""The member intervals, sorted and disjoint.
|
|
95
|
+
|
|
96
|
+
Empty intervals are never members. Pieces of a difference
|
|
97
|
+
that exclude their start point (see :class:`Interval`) keep
|
|
98
|
+
that shape here; for ``Date`` endpoints such pieces are
|
|
99
|
+
normalized to the first contained date.
|
|
100
|
+
"""
|
|
101
|
+
members: list[Interval] = []
|
|
102
|
+
for atom in self._portion:
|
|
103
|
+
interval = from_portion_atom(atom)
|
|
104
|
+
if interval is not None:
|
|
105
|
+
members.append(interval)
|
|
106
|
+
return tuple(members)
|
|
107
|
+
|
|
108
|
+
@property
|
|
109
|
+
def is_empty(self) -> bool:
|
|
110
|
+
"""Whether the set contains no interval (and no point)."""
|
|
111
|
+
return self._portion.empty
|
|
112
|
+
|
|
113
|
+
def __iter__(self) -> Iterator[Interval]:
|
|
114
|
+
return iter(self.intervals)
|
|
115
|
+
|
|
116
|
+
def __len__(self) -> int:
|
|
117
|
+
"""The number of disjoint intervals in the set."""
|
|
118
|
+
return len(self.intervals)
|
|
119
|
+
|
|
120
|
+
def contains(self, point: Endpoint) -> bool:
|
|
121
|
+
"""Whether ``point`` lies in any interval of the set.
|
|
122
|
+
|
|
123
|
+
``point`` must be of the set's endpoint type (an empty set
|
|
124
|
+
contains nothing, whatever the point). A different temporal
|
|
125
|
+
type raises :class:`~timecore.errors.InvalidIntervalError`.
|
|
126
|
+
"""
|
|
127
|
+
point = require_endpoint(point, "IntervalSet.contains()")
|
|
128
|
+
if self._endpoint_type is not None and type(point) is not self._endpoint_type:
|
|
129
|
+
raise InvalidIntervalError(
|
|
130
|
+
"IntervalSet.contains() point must be of the set's "
|
|
131
|
+
f"endpoint type ({self._endpoint_type.__name__}); "
|
|
132
|
+
f"got {type(point).__name__}."
|
|
133
|
+
)
|
|
134
|
+
return self._portion.contains(point)
|
|
135
|
+
|
|
136
|
+
def union(self, other: Interval | IntervalSet) -> IntervalSet:
|
|
137
|
+
"""The set of points in this set or ``other`` (or both)."""
|
|
138
|
+
other_set = _as_interval_set(other, "union")
|
|
139
|
+
self._check_compatible(other_set, "union")
|
|
140
|
+
return self._combine(other_set, "union")
|
|
141
|
+
|
|
142
|
+
def intersection(self, other: Interval | IntervalSet) -> IntervalSet:
|
|
143
|
+
"""The set of points in both this set and ``other``."""
|
|
144
|
+
other_set = _as_interval_set(other, "intersection")
|
|
145
|
+
self._check_compatible(other_set, "intersection")
|
|
146
|
+
return self._combine(other_set, "intersection")
|
|
147
|
+
|
|
148
|
+
def difference(self, other: Interval | IntervalSet) -> IntervalSet:
|
|
149
|
+
"""The set of points in this set but not in ``other``."""
|
|
150
|
+
other_set = _as_interval_set(other, "difference")
|
|
151
|
+
self._check_compatible(other_set, "difference")
|
|
152
|
+
return self._combine(other_set, "difference")
|
|
153
|
+
|
|
154
|
+
def total_days(self) -> int:
|
|
155
|
+
"""The total span of the set in whole days (``Date`` sets only).
|
|
156
|
+
|
|
157
|
+
The sum of ``(end - start)`` in days over the member
|
|
158
|
+
intervals. For half-open intervals this is also the number of
|
|
159
|
+
days contained; for a closed interval the contained day count
|
|
160
|
+
is one more than its span. Raises ``TypeError`` for sets whose
|
|
161
|
+
endpoint type is not ``Date``. An empty set totals 0.
|
|
162
|
+
"""
|
|
163
|
+
if self._endpoint_type is not None and self._endpoint_type is not Date:
|
|
164
|
+
raise TypeError(
|
|
165
|
+
"IntervalSet.total_days() is only defined for sets of "
|
|
166
|
+
f"Date intervals; this set holds {self._endpoint_type.__name__}."
|
|
167
|
+
)
|
|
168
|
+
total = 0
|
|
169
|
+
for interval in self.intervals:
|
|
170
|
+
start = interval.start
|
|
171
|
+
end = interval.end
|
|
172
|
+
if isinstance(start, Date) and isinstance(end, Date):
|
|
173
|
+
total += (end.to_date() - start.to_date()).days
|
|
174
|
+
return total
|
|
175
|
+
|
|
176
|
+
def total_duration(self) -> Duration:
|
|
177
|
+
"""The total span of the set (non-``Date`` sets only).
|
|
178
|
+
|
|
179
|
+
The sum of the member intervals' durations, computed with
|
|
180
|
+
each endpoint type's own arithmetic (so ``DateTime`` spans
|
|
181
|
+
across DST transitions are exact). Raises ``TypeError`` for
|
|
182
|
+
sets of ``Date`` intervals -- use :meth:`total_days` there.
|
|
183
|
+
An empty set totals :attr:`Duration.ZERO`.
|
|
184
|
+
"""
|
|
185
|
+
if self._endpoint_type is Date:
|
|
186
|
+
raise TypeError(
|
|
187
|
+
"IntervalSet.total_duration() is not defined for sets of "
|
|
188
|
+
"Date intervals; use total_days() instead."
|
|
189
|
+
)
|
|
190
|
+
total = Duration.ZERO
|
|
191
|
+
for interval in self.intervals:
|
|
192
|
+
duration = interval.duration
|
|
193
|
+
if isinstance(duration, Duration):
|
|
194
|
+
total = total + duration
|
|
195
|
+
return total
|
|
196
|
+
|
|
197
|
+
def _combine(self, other: IntervalSet, operation: str) -> IntervalSet:
|
|
198
|
+
"""Apply a ``portion`` set operation and wrap the result."""
|
|
199
|
+
if operation == "union":
|
|
200
|
+
result = self._portion | other._portion
|
|
201
|
+
elif operation == "intersection":
|
|
202
|
+
result = self._portion & other._portion
|
|
203
|
+
else:
|
|
204
|
+
result = self._portion - other._portion
|
|
205
|
+
endpoint_type = self._endpoint_type or other._endpoint_type
|
|
206
|
+
return IntervalSet._from_portion(result, endpoint_type)
|
|
207
|
+
|
|
208
|
+
def _check_compatible(self, other: IntervalSet, operation: str) -> None:
|
|
209
|
+
"""Require both sets to share an endpoint type (if either has one)."""
|
|
210
|
+
if (
|
|
211
|
+
self._endpoint_type is not None
|
|
212
|
+
and other._endpoint_type is not None
|
|
213
|
+
and self._endpoint_type is not other._endpoint_type
|
|
214
|
+
):
|
|
215
|
+
raise InvalidIntervalError(
|
|
216
|
+
f"IntervalSet.{operation}() requires the same endpoint "
|
|
217
|
+
f"type; got {self._endpoint_type.__name__} and "
|
|
218
|
+
f"{other._endpoint_type.__name__}."
|
|
219
|
+
)
|
|
220
|
+
|
|
221
|
+
def __eq__(self, other: object) -> bool:
|
|
222
|
+
if not isinstance(other, IntervalSet):
|
|
223
|
+
return NotImplemented
|
|
224
|
+
return self.intervals == other.intervals
|
|
225
|
+
|
|
226
|
+
def __hash__(self) -> int:
|
|
227
|
+
return hash(self.intervals)
|
|
228
|
+
|
|
229
|
+
def __repr__(self) -> str:
|
|
230
|
+
members = ", ".join(repr(interval) for interval in self.intervals)
|
|
231
|
+
return f"IntervalSet([{members}])"
|
|
232
|
+
|
|
233
|
+
|
|
234
|
+
def _as_interval_set(other: object, operation: str) -> IntervalSet:
|
|
235
|
+
"""Coerce an :class:`Interval` or :class:`IntervalSet` operand."""
|
|
236
|
+
if isinstance(other, Interval):
|
|
237
|
+
return IntervalSet((other,))
|
|
238
|
+
if isinstance(other, IntervalSet):
|
|
239
|
+
return other
|
|
240
|
+
raise TypeError(
|
|
241
|
+
f"IntervalSet.{operation}() expects an Interval or IntervalSet; "
|
|
242
|
+
f"got {type(other).__name__}."
|
|
243
|
+
)
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
"""Parsing temporal values from text."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from .parse import (
|
|
6
|
+
parse_date,
|
|
7
|
+
parse_datetime,
|
|
8
|
+
parse_duration,
|
|
9
|
+
parse_instant,
|
|
10
|
+
parse_local_datetime,
|
|
11
|
+
parse_natural_language,
|
|
12
|
+
parse_period,
|
|
13
|
+
parse_time,
|
|
14
|
+
)
|
|
15
|
+
|
|
16
|
+
__all__ = [
|
|
17
|
+
"parse_date",
|
|
18
|
+
"parse_datetime",
|
|
19
|
+
"parse_duration",
|
|
20
|
+
"parse_instant",
|
|
21
|
+
"parse_local_datetime",
|
|
22
|
+
"parse_natural_language",
|
|
23
|
+
"parse_period",
|
|
24
|
+
"parse_time",
|
|
25
|
+
]
|