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/instant.py
ADDED
|
@@ -0,0 +1,265 @@
|
|
|
1
|
+
"""Instant: a single moment on the global timeline, with nanosecond precision."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from datetime import datetime as _StdDateTime
|
|
6
|
+
from typing import TYPE_CHECKING, overload
|
|
7
|
+
|
|
8
|
+
import whenever
|
|
9
|
+
|
|
10
|
+
from ..errors import ParseError
|
|
11
|
+
from ._common import (
|
|
12
|
+
Clock,
|
|
13
|
+
_check_us_precision,
|
|
14
|
+
_exact_parts,
|
|
15
|
+
_format_value,
|
|
16
|
+
_instant_to_zoned,
|
|
17
|
+
_map_parse_errors,
|
|
18
|
+
_map_whenever_errors,
|
|
19
|
+
_now_instant,
|
|
20
|
+
_require_int,
|
|
21
|
+
_require_number,
|
|
22
|
+
_require_str,
|
|
23
|
+
_resolve_timezone,
|
|
24
|
+
_strptime,
|
|
25
|
+
)
|
|
26
|
+
from .duration import Duration
|
|
27
|
+
|
|
28
|
+
if TYPE_CHECKING:
|
|
29
|
+
from .timezone import TimeZone
|
|
30
|
+
from .zoned_datetime import DateTime
|
|
31
|
+
|
|
32
|
+
__all__ = ["Instant"]
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
class Instant:
|
|
36
|
+
"""A single moment on the global timeline, with nanosecond precision.
|
|
37
|
+
|
|
38
|
+
Instants have no calendar or time-zone interpretation of their own;
|
|
39
|
+
convert to a :class:`~timecore.core.DateTime` to see one in a zone.
|
|
40
|
+
|
|
41
|
+
Construct via :meth:`now`, :meth:`parse`,
|
|
42
|
+
:meth:`from_unix_timestamp`, or :meth:`from_unix_timestamp_ns` --
|
|
43
|
+
there is no public ``Instant(...)`` constructor.
|
|
44
|
+
|
|
45
|
+
Example:
|
|
46
|
+
>>> Instant.from_unix_timestamp(0)
|
|
47
|
+
Instant("1970-01-01T00:00:00Z")
|
|
48
|
+
"""
|
|
49
|
+
|
|
50
|
+
__slots__ = ("_inner",)
|
|
51
|
+
|
|
52
|
+
# The wrapped whenever value; declared here so pyright knows its type
|
|
53
|
+
# (``__init__`` raises and never assigns it).
|
|
54
|
+
_inner: whenever.Instant
|
|
55
|
+
|
|
56
|
+
def __init__(self, *args: object, **kwargs: object) -> None:
|
|
57
|
+
raise TypeError(
|
|
58
|
+
"Instant cannot be constructed directly; use Instant.now(), "
|
|
59
|
+
"Instant.parse(), Instant.from_unix_timestamp(), or "
|
|
60
|
+
"Instant.from_unix_timestamp_ns()"
|
|
61
|
+
)
|
|
62
|
+
|
|
63
|
+
@classmethod
|
|
64
|
+
def from_whenever(cls, inner: whenever.Instant) -> Instant:
|
|
65
|
+
"""Wrap a whenever ``Instant``.
|
|
66
|
+
|
|
67
|
+
Internal constructor; not part of the public API.
|
|
68
|
+
"""
|
|
69
|
+
obj = cls.__new__(cls)
|
|
70
|
+
obj._inner = inner
|
|
71
|
+
return obj
|
|
72
|
+
|
|
73
|
+
@classmethod
|
|
74
|
+
def now(cls, *, clock: Clock | None = None) -> Instant:
|
|
75
|
+
"""Return the current instant.
|
|
76
|
+
|
|
77
|
+
Pass ``clock`` (an object with a ``now() -> Instant`` method) to
|
|
78
|
+
determine "now" deterministically, e.g. in tests.
|
|
79
|
+
"""
|
|
80
|
+
return _now_instant(clock)
|
|
81
|
+
|
|
82
|
+
@classmethod
|
|
83
|
+
def parse(cls, text: str, *, format: str | None = None) -> Instant:
|
|
84
|
+
"""Parse an ISO 8601 instant such as ``"2026-01-15T10:30:00Z"``.
|
|
85
|
+
|
|
86
|
+
The input must carry a ``Z`` or numeric offset. With ``format``,
|
|
87
|
+
parse with a ``strftime`` pattern instead (English month names
|
|
88
|
+
only, locale-independent); the parsed value must be aware.
|
|
89
|
+
|
|
90
|
+
Raises:
|
|
91
|
+
ParseError: If ``text`` is not a valid instant.
|
|
92
|
+
"""
|
|
93
|
+
text = _require_str(text, "Instant.parse()")
|
|
94
|
+
if format is not None:
|
|
95
|
+
format = _require_str(format, "Instant.parse() format")
|
|
96
|
+
parsed = _strptime(text, format)
|
|
97
|
+
if parsed.tzinfo is None:
|
|
98
|
+
raise ParseError(
|
|
99
|
+
f"could not parse {text!r} as an Instant: "
|
|
100
|
+
"input carries no time zone offset"
|
|
101
|
+
)
|
|
102
|
+
with _map_parse_errors():
|
|
103
|
+
return cls.from_whenever(whenever.Instant(parsed))
|
|
104
|
+
with _map_parse_errors():
|
|
105
|
+
return cls.from_whenever(whenever.Instant.parse_iso(text))
|
|
106
|
+
|
|
107
|
+
@classmethod
|
|
108
|
+
def from_unix_timestamp(cls, value: float) -> Instant:
|
|
109
|
+
"""Create an :class:`Instant` from seconds since the Unix epoch."""
|
|
110
|
+
value = _require_number(value, "Instant.from_unix_timestamp()")
|
|
111
|
+
with _map_whenever_errors():
|
|
112
|
+
return cls.from_whenever(whenever.Instant.from_timestamp(value))
|
|
113
|
+
|
|
114
|
+
@classmethod
|
|
115
|
+
def from_unix_timestamp_ns(cls, value: int) -> Instant:
|
|
116
|
+
"""Create an :class:`Instant` from nanoseconds since the Unix epoch."""
|
|
117
|
+
value = _require_int(value, "Instant.from_unix_timestamp_ns()")
|
|
118
|
+
with _map_whenever_errors():
|
|
119
|
+
return cls.from_whenever(
|
|
120
|
+
whenever.Instant.from_timestamp(value, unit="nanosecond")
|
|
121
|
+
)
|
|
122
|
+
|
|
123
|
+
def to_unix_timestamp(self) -> float:
|
|
124
|
+
"""Return seconds since the Unix epoch, as a float."""
|
|
125
|
+
return self._inner.timestamp(unit="nanosecond") / 1_000_000_000
|
|
126
|
+
|
|
127
|
+
def to_unix_timestamp_ns(self) -> int:
|
|
128
|
+
"""Return nanoseconds since the Unix epoch."""
|
|
129
|
+
return self._inner.timestamp(unit="nanosecond")
|
|
130
|
+
|
|
131
|
+
@overload
|
|
132
|
+
def to_datetime(self, timezone: TimeZone | str) -> DateTime: ...
|
|
133
|
+
|
|
134
|
+
@overload
|
|
135
|
+
def to_datetime(self, timezone: None = None) -> _StdDateTime: ...
|
|
136
|
+
|
|
137
|
+
def to_datetime(
|
|
138
|
+
self, timezone: TimeZone | str | None = None
|
|
139
|
+
) -> DateTime | _StdDateTime:
|
|
140
|
+
"""Convert this instant.
|
|
141
|
+
|
|
142
|
+
With ``timezone`` (a :class:`~timecore.core.TimeZone` or key),
|
|
143
|
+
return a :class:`~timecore.core.DateTime` in that zone. Without
|
|
144
|
+
one, return a standard-library UTC ``datetime``.
|
|
145
|
+
|
|
146
|
+
Raises:
|
|
147
|
+
ValueError: If converting to a stdlib ``datetime`` would lose
|
|
148
|
+
sub-microsecond precision.
|
|
149
|
+
"""
|
|
150
|
+
if timezone is None:
|
|
151
|
+
_check_us_precision(self.to_unix_timestamp_ns(), "Instant")
|
|
152
|
+
return self._inner.to_stdlib()
|
|
153
|
+
tz = _resolve_timezone(timezone)
|
|
154
|
+
from .zoned_datetime import DateTime
|
|
155
|
+
|
|
156
|
+
with _map_whenever_errors():
|
|
157
|
+
return DateTime.from_whenever(_instant_to_zoned(self._inner, tz.key))
|
|
158
|
+
|
|
159
|
+
def isoformat(self) -> str:
|
|
160
|
+
"""Return the ISO 8601 form in UTC, e.g. ``"2026-01-15T10:30:00Z"``."""
|
|
161
|
+
return self._inner.format_iso()
|
|
162
|
+
|
|
163
|
+
def format(
|
|
164
|
+
self,
|
|
165
|
+
style: str = "medium",
|
|
166
|
+
locale: str | None = None,
|
|
167
|
+
pattern: str | None = None,
|
|
168
|
+
timezone: TimeZone | str | None = None,
|
|
169
|
+
) -> str:
|
|
170
|
+
"""Format this instant (see :func:`timecore.core._common._format_value`).
|
|
171
|
+
|
|
172
|
+
Formats in UTC unless ``timezone`` is given. ``style`` is one of
|
|
173
|
+
``"short"``, ``"medium"``, ``"long"``, ``"full"``; ``pattern`` is
|
|
174
|
+
a Babel pattern. The default call returns Babel's ``"medium"``
|
|
175
|
+
style output.
|
|
176
|
+
"""
|
|
177
|
+
return _format_value(
|
|
178
|
+
self,
|
|
179
|
+
style=style,
|
|
180
|
+
locale=locale,
|
|
181
|
+
pattern=pattern,
|
|
182
|
+
timezone=timezone,
|
|
183
|
+
)
|
|
184
|
+
|
|
185
|
+
def __add__(self, other: object) -> Instant:
|
|
186
|
+
if isinstance(other, Duration):
|
|
187
|
+
hours, minutes, seconds, ms, us, ns = _exact_parts(
|
|
188
|
+
other.total_nanoseconds()
|
|
189
|
+
)
|
|
190
|
+
with _map_whenever_errors():
|
|
191
|
+
return Instant.from_whenever(
|
|
192
|
+
self._inner.add(
|
|
193
|
+
hours=hours,
|
|
194
|
+
minutes=minutes,
|
|
195
|
+
seconds=seconds,
|
|
196
|
+
milliseconds=ms,
|
|
197
|
+
microseconds=us,
|
|
198
|
+
nanoseconds=ns,
|
|
199
|
+
)
|
|
200
|
+
)
|
|
201
|
+
return NotImplemented
|
|
202
|
+
|
|
203
|
+
def __radd__(self, other: object) -> Instant:
|
|
204
|
+
return self.__add__(other)
|
|
205
|
+
|
|
206
|
+
@overload
|
|
207
|
+
def __sub__(self, other: Duration) -> Instant: ...
|
|
208
|
+
|
|
209
|
+
@overload
|
|
210
|
+
def __sub__(self, other: Instant) -> Duration: ...
|
|
211
|
+
|
|
212
|
+
def __sub__(self, other: object) -> Instant | Duration:
|
|
213
|
+
if isinstance(other, Duration):
|
|
214
|
+
hours, minutes, seconds, ms, us, ns = _exact_parts(
|
|
215
|
+
other.total_nanoseconds()
|
|
216
|
+
)
|
|
217
|
+
with _map_whenever_errors():
|
|
218
|
+
return Instant.from_whenever(
|
|
219
|
+
self._inner.subtract(
|
|
220
|
+
hours=hours,
|
|
221
|
+
minutes=minutes,
|
|
222
|
+
seconds=seconds,
|
|
223
|
+
milliseconds=ms,
|
|
224
|
+
microseconds=us,
|
|
225
|
+
nanoseconds=ns,
|
|
226
|
+
)
|
|
227
|
+
)
|
|
228
|
+
if isinstance(other, Instant):
|
|
229
|
+
with _map_whenever_errors():
|
|
230
|
+
return Duration.from_whenever(self._inner.difference(other._inner))
|
|
231
|
+
return NotImplemented
|
|
232
|
+
|
|
233
|
+
def __eq__(self, other: object) -> bool:
|
|
234
|
+
if isinstance(other, Instant):
|
|
235
|
+
return self._inner == other._inner
|
|
236
|
+
return NotImplemented
|
|
237
|
+
|
|
238
|
+
def __lt__(self, other: object) -> bool:
|
|
239
|
+
if isinstance(other, Instant):
|
|
240
|
+
return self._inner < other._inner
|
|
241
|
+
return NotImplemented
|
|
242
|
+
|
|
243
|
+
def __le__(self, other: object) -> bool:
|
|
244
|
+
if isinstance(other, Instant):
|
|
245
|
+
return self._inner <= other._inner
|
|
246
|
+
return NotImplemented
|
|
247
|
+
|
|
248
|
+
def __gt__(self, other: object) -> bool:
|
|
249
|
+
if isinstance(other, Instant):
|
|
250
|
+
return self._inner > other._inner
|
|
251
|
+
return NotImplemented
|
|
252
|
+
|
|
253
|
+
def __ge__(self, other: object) -> bool:
|
|
254
|
+
if isinstance(other, Instant):
|
|
255
|
+
return self._inner >= other._inner
|
|
256
|
+
return NotImplemented
|
|
257
|
+
|
|
258
|
+
def __hash__(self) -> int:
|
|
259
|
+
return hash(self._inner)
|
|
260
|
+
|
|
261
|
+
def __repr__(self) -> str:
|
|
262
|
+
return f'Instant("{self.isoformat()}")'
|
|
263
|
+
|
|
264
|
+
def __str__(self) -> str:
|
|
265
|
+
return self.isoformat()
|
|
@@ -0,0 +1,347 @@
|
|
|
1
|
+
"""LocalDateTime: a date and time without a time zone."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from datetime import datetime as _StdDateTime
|
|
6
|
+
from typing import TYPE_CHECKING
|
|
7
|
+
|
|
8
|
+
import whenever
|
|
9
|
+
|
|
10
|
+
from ..errors import ParseError
|
|
11
|
+
from ._common import (
|
|
12
|
+
Clock,
|
|
13
|
+
Disambiguation,
|
|
14
|
+
_check_us_precision,
|
|
15
|
+
_exact_parts,
|
|
16
|
+
_format_value,
|
|
17
|
+
_map_parse_errors,
|
|
18
|
+
_map_whenever_errors,
|
|
19
|
+
_now_instant,
|
|
20
|
+
_plain_assume_tz,
|
|
21
|
+
_require_instance,
|
|
22
|
+
_require_str,
|
|
23
|
+
_resolve_timezone,
|
|
24
|
+
_strptime,
|
|
25
|
+
_validate_disambiguation,
|
|
26
|
+
)
|
|
27
|
+
from .date import Date
|
|
28
|
+
from .duration import Duration
|
|
29
|
+
from .period import Period
|
|
30
|
+
from .time import Time
|
|
31
|
+
from .weekday import Weekday
|
|
32
|
+
|
|
33
|
+
if TYPE_CHECKING:
|
|
34
|
+
from .timezone import TimeZone
|
|
35
|
+
from .zoned_datetime import DateTime
|
|
36
|
+
|
|
37
|
+
__all__ = ["LocalDateTime"]
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
class LocalDateTime:
|
|
41
|
+
"""A date and time without a time zone, with nanosecond precision.
|
|
42
|
+
|
|
43
|
+
This is a "wall clock" reading: it says nothing about which instant
|
|
44
|
+
it is until a time zone is assumed (see :meth:`at_timezone`).
|
|
45
|
+
|
|
46
|
+
Example:
|
|
47
|
+
>>> LocalDateTime(2026, 1, 15, 10, 30)
|
|
48
|
+
LocalDateTime(2026, 1, 15, 10, 30)
|
|
49
|
+
"""
|
|
50
|
+
|
|
51
|
+
__slots__ = ("_inner",)
|
|
52
|
+
|
|
53
|
+
# The wrapped whenever value; declared here so pyright knows its type.
|
|
54
|
+
_inner: whenever.PlainDateTime
|
|
55
|
+
|
|
56
|
+
def __init__(
|
|
57
|
+
self,
|
|
58
|
+
year: int,
|
|
59
|
+
month: int,
|
|
60
|
+
day: int,
|
|
61
|
+
hour: int = 0,
|
|
62
|
+
minute: int = 0,
|
|
63
|
+
second: int = 0,
|
|
64
|
+
microsecond: int = 0,
|
|
65
|
+
nanosecond: int = 0,
|
|
66
|
+
) -> None:
|
|
67
|
+
# See Time: microsecond and nanosecond add up to the sub-second part.
|
|
68
|
+
total_ns = microsecond * 1_000 + nanosecond
|
|
69
|
+
with _map_whenever_errors():
|
|
70
|
+
self._inner = whenever.PlainDateTime(
|
|
71
|
+
year, month, day, hour, minute, second, nanosecond=total_ns
|
|
72
|
+
)
|
|
73
|
+
|
|
74
|
+
@classmethod
|
|
75
|
+
def from_whenever(cls, inner: whenever.PlainDateTime) -> LocalDateTime:
|
|
76
|
+
"""Wrap a whenever ``PlainDateTime``.
|
|
77
|
+
|
|
78
|
+
Internal constructor; not part of the public API.
|
|
79
|
+
"""
|
|
80
|
+
obj = cls.__new__(cls)
|
|
81
|
+
obj._inner = inner
|
|
82
|
+
return obj
|
|
83
|
+
|
|
84
|
+
@classmethod
|
|
85
|
+
def now(
|
|
86
|
+
cls,
|
|
87
|
+
*,
|
|
88
|
+
timezone: TimeZone | str | None = None,
|
|
89
|
+
clock: Clock | None = None,
|
|
90
|
+
) -> LocalDateTime:
|
|
91
|
+
"""Return the current local date and time in ``timezone`` (default: local zone).
|
|
92
|
+
|
|
93
|
+
Pass ``clock`` (an object with a ``now() -> Instant`` method) to
|
|
94
|
+
determine "now" deterministically, e.g. in tests.
|
|
95
|
+
"""
|
|
96
|
+
instant = _now_instant(clock)
|
|
97
|
+
tz = _resolve_timezone(timezone)
|
|
98
|
+
return instant.to_datetime(tz).to_local_datetime()
|
|
99
|
+
|
|
100
|
+
@classmethod
|
|
101
|
+
def parse(cls, text: str, *, format: str | None = None) -> LocalDateTime:
|
|
102
|
+
"""Parse an ISO 8601 local datetime such as ``"2026-01-15T10:30:00"``.
|
|
103
|
+
|
|
104
|
+
With ``format``, parse with a ``strftime`` pattern instead
|
|
105
|
+
(English month names only, locale-independent).
|
|
106
|
+
|
|
107
|
+
Raises:
|
|
108
|
+
ParseError: If ``text`` is not a valid local datetime.
|
|
109
|
+
"""
|
|
110
|
+
text = _require_str(text, "LocalDateTime.parse()")
|
|
111
|
+
if format is not None:
|
|
112
|
+
format = _require_str(format, "LocalDateTime.parse() format")
|
|
113
|
+
parsed = _strptime(text, format)
|
|
114
|
+
if parsed.tzinfo is not None:
|
|
115
|
+
raise ParseError(
|
|
116
|
+
f"could not parse {text!r} as a LocalDateTime: "
|
|
117
|
+
"input carries a time zone offset"
|
|
118
|
+
)
|
|
119
|
+
return cls(
|
|
120
|
+
parsed.year,
|
|
121
|
+
parsed.month,
|
|
122
|
+
parsed.day,
|
|
123
|
+
parsed.hour,
|
|
124
|
+
parsed.minute,
|
|
125
|
+
parsed.second,
|
|
126
|
+
microsecond=parsed.microsecond,
|
|
127
|
+
)
|
|
128
|
+
with _map_parse_errors():
|
|
129
|
+
return cls.from_whenever(whenever.PlainDateTime.parse_iso(text))
|
|
130
|
+
|
|
131
|
+
@classmethod
|
|
132
|
+
def from_datetime(cls, value: _StdDateTime) -> LocalDateTime:
|
|
133
|
+
"""Create a :class:`LocalDateTime` from a naive stdlib ``datetime``.
|
|
134
|
+
|
|
135
|
+
Raises:
|
|
136
|
+
ValueError: If ``value`` is time-zone aware.
|
|
137
|
+
"""
|
|
138
|
+
value = _require_instance(value, _StdDateTime, "LocalDateTime.from_datetime()")
|
|
139
|
+
if value.tzinfo is not None:
|
|
140
|
+
raise ValueError(
|
|
141
|
+
"LocalDateTime.from_datetime() expects a naive datetime; "
|
|
142
|
+
"use DateTime.from_datetime() for aware datetimes"
|
|
143
|
+
)
|
|
144
|
+
with _map_whenever_errors():
|
|
145
|
+
return cls.from_whenever(whenever.PlainDateTime(value))
|
|
146
|
+
|
|
147
|
+
def to_datetime(self) -> _StdDateTime:
|
|
148
|
+
"""Convert to a naive standard-library ``datetime``.
|
|
149
|
+
|
|
150
|
+
Raises:
|
|
151
|
+
ValueError: If this value has a sub-microsecond remainder,
|
|
152
|
+
which a stdlib ``datetime`` cannot represent.
|
|
153
|
+
"""
|
|
154
|
+
_check_us_precision(self.nanosecond, "LocalDateTime")
|
|
155
|
+
return self._inner.to_stdlib()
|
|
156
|
+
|
|
157
|
+
def date(self) -> Date:
|
|
158
|
+
"""Return the date part."""
|
|
159
|
+
return Date.from_whenever(self._inner.date())
|
|
160
|
+
|
|
161
|
+
def time(self) -> Time:
|
|
162
|
+
"""Return the time-of-day part."""
|
|
163
|
+
return Time.from_whenever(self._inner.time())
|
|
164
|
+
|
|
165
|
+
def at_timezone(
|
|
166
|
+
self, timezone: TimeZone | str, disambiguation: Disambiguation = "raise"
|
|
167
|
+
) -> DateTime:
|
|
168
|
+
"""Interpret this wall time in ``timezone``, returning a ``DateTime``.
|
|
169
|
+
|
|
170
|
+
``disambiguation`` controls repeated (DST fall-back) and skipped
|
|
171
|
+
(DST spring-forward) local times: ``"raise"`` (default) raises
|
|
172
|
+
:class:`~timecore.errors.AmbiguousTimeError` /
|
|
173
|
+
:class:`~timecore.errors.NonexistentTimeError`, while
|
|
174
|
+
``"earlier"`` / ``"later"`` pick the earlier / later occurrence
|
|
175
|
+
(for skipped times, ``"earlier"`` / ``"later"`` shift to the
|
|
176
|
+
valid time before / after the gap, following whenever).
|
|
177
|
+
"""
|
|
178
|
+
from .timezone import TimeZone
|
|
179
|
+
from .zoned_datetime import DateTime
|
|
180
|
+
|
|
181
|
+
if isinstance(timezone, str):
|
|
182
|
+
tz = TimeZone(timezone)
|
|
183
|
+
else:
|
|
184
|
+
tz = _require_instance(timezone, TimeZone, "at_timezone() timezone")
|
|
185
|
+
disambiguation = _validate_disambiguation(disambiguation)
|
|
186
|
+
with _map_whenever_errors():
|
|
187
|
+
zoned = _plain_assume_tz(self._inner, tz.key, disambiguation)
|
|
188
|
+
return DateTime.from_whenever(zoned)
|
|
189
|
+
|
|
190
|
+
@property
|
|
191
|
+
def year(self) -> int:
|
|
192
|
+
"""The year, e.g. 2026."""
|
|
193
|
+
return self._inner.year
|
|
194
|
+
|
|
195
|
+
@property
|
|
196
|
+
def month(self) -> int:
|
|
197
|
+
"""The month, 1-12."""
|
|
198
|
+
return self._inner.month
|
|
199
|
+
|
|
200
|
+
@property
|
|
201
|
+
def day(self) -> int:
|
|
202
|
+
"""The day of the month, 1-31."""
|
|
203
|
+
return self._inner.day
|
|
204
|
+
|
|
205
|
+
@property
|
|
206
|
+
def hour(self) -> int:
|
|
207
|
+
"""The hour, 0-23."""
|
|
208
|
+
return self._inner.hour
|
|
209
|
+
|
|
210
|
+
@property
|
|
211
|
+
def minute(self) -> int:
|
|
212
|
+
"""The minute, 0-59."""
|
|
213
|
+
return self._inner.minute
|
|
214
|
+
|
|
215
|
+
@property
|
|
216
|
+
def second(self) -> int:
|
|
217
|
+
"""The second, 0-59."""
|
|
218
|
+
return self._inner.second
|
|
219
|
+
|
|
220
|
+
@property
|
|
221
|
+
def microsecond(self) -> int:
|
|
222
|
+
"""The microsecond component, 0-999999."""
|
|
223
|
+
return self.nanosecond // 1_000
|
|
224
|
+
|
|
225
|
+
@property
|
|
226
|
+
def nanosecond(self) -> int:
|
|
227
|
+
"""The sub-second part in nanoseconds, 0-999999999."""
|
|
228
|
+
return self._inner.nanosecond
|
|
229
|
+
|
|
230
|
+
@property
|
|
231
|
+
def weekday(self) -> Weekday:
|
|
232
|
+
"""The day of the week."""
|
|
233
|
+
return Weekday[self._inner.day_of_week().name]
|
|
234
|
+
|
|
235
|
+
def isoformat(self) -> str:
|
|
236
|
+
"""Return the ISO 8601 form, e.g. ``"2026-01-15T10:30:00"``."""
|
|
237
|
+
return self._inner.format_iso()
|
|
238
|
+
|
|
239
|
+
def format(
|
|
240
|
+
self,
|
|
241
|
+
style: str = "medium",
|
|
242
|
+
locale: str | None = None,
|
|
243
|
+
pattern: str | None = None,
|
|
244
|
+
) -> str:
|
|
245
|
+
"""Format this value (see :func:`timecore.core._common._format_value`).
|
|
246
|
+
|
|
247
|
+
``style`` is one of ``"short"``, ``"medium"``, ``"long"``,
|
|
248
|
+
``"full"``; ``pattern`` is a Babel pattern. The default call
|
|
249
|
+
returns Babel's ``"medium"`` style output.
|
|
250
|
+
"""
|
|
251
|
+
return _format_value(self, style=style, locale=locale, pattern=pattern)
|
|
252
|
+
|
|
253
|
+
def __add__(self, other: object) -> LocalDateTime:
|
|
254
|
+
if isinstance(other, Duration):
|
|
255
|
+
hours, minutes, seconds, ms, us, ns = _exact_parts(
|
|
256
|
+
other.total_nanoseconds()
|
|
257
|
+
)
|
|
258
|
+
with _map_whenever_errors():
|
|
259
|
+
return LocalDateTime.from_whenever(
|
|
260
|
+
self._inner.add(
|
|
261
|
+
hours=hours,
|
|
262
|
+
minutes=minutes,
|
|
263
|
+
seconds=seconds,
|
|
264
|
+
milliseconds=ms,
|
|
265
|
+
microseconds=us,
|
|
266
|
+
nanoseconds=ns,
|
|
267
|
+
naive_arithmetic_ok=True,
|
|
268
|
+
)
|
|
269
|
+
)
|
|
270
|
+
if isinstance(other, Period):
|
|
271
|
+
with _map_whenever_errors():
|
|
272
|
+
return LocalDateTime.from_whenever(
|
|
273
|
+
self._inner.add(
|
|
274
|
+
years=other.years,
|
|
275
|
+
months=other.months,
|
|
276
|
+
weeks=other.weeks,
|
|
277
|
+
days=other.days,
|
|
278
|
+
hours=other.hours,
|
|
279
|
+
minutes=other.minutes,
|
|
280
|
+
seconds=other.seconds,
|
|
281
|
+
milliseconds=other.milliseconds,
|
|
282
|
+
microseconds=other.microseconds,
|
|
283
|
+
nanoseconds=other.nanoseconds,
|
|
284
|
+
naive_arithmetic_ok=True,
|
|
285
|
+
)
|
|
286
|
+
)
|
|
287
|
+
return NotImplemented
|
|
288
|
+
|
|
289
|
+
def __radd__(self, other: object) -> LocalDateTime:
|
|
290
|
+
return self.__add__(other)
|
|
291
|
+
|
|
292
|
+
def __sub__(self, other: object) -> LocalDateTime | Duration:
|
|
293
|
+
if isinstance(other, (Duration, Period)):
|
|
294
|
+
return self.__add__(-other)
|
|
295
|
+
if isinstance(other, LocalDateTime):
|
|
296
|
+
with _map_whenever_errors():
|
|
297
|
+
total_ns = self._inner.since(
|
|
298
|
+
other._inner, total="nanoseconds", naive_arithmetic_ok=True
|
|
299
|
+
)
|
|
300
|
+
return Duration.from_whenever(whenever.TimeDelta(nanoseconds=int(total_ns)))
|
|
301
|
+
return NotImplemented
|
|
302
|
+
|
|
303
|
+
def __eq__(self, other: object) -> bool:
|
|
304
|
+
if isinstance(other, LocalDateTime):
|
|
305
|
+
return self._inner == other._inner
|
|
306
|
+
return NotImplemented
|
|
307
|
+
|
|
308
|
+
def __lt__(self, other: object) -> bool:
|
|
309
|
+
if isinstance(other, LocalDateTime):
|
|
310
|
+
return self._inner < other._inner
|
|
311
|
+
return NotImplemented
|
|
312
|
+
|
|
313
|
+
def __le__(self, other: object) -> bool:
|
|
314
|
+
if isinstance(other, LocalDateTime):
|
|
315
|
+
return self._inner <= other._inner
|
|
316
|
+
return NotImplemented
|
|
317
|
+
|
|
318
|
+
def __gt__(self, other: object) -> bool:
|
|
319
|
+
if isinstance(other, LocalDateTime):
|
|
320
|
+
return self._inner > other._inner
|
|
321
|
+
return NotImplemented
|
|
322
|
+
|
|
323
|
+
def __ge__(self, other: object) -> bool:
|
|
324
|
+
if isinstance(other, LocalDateTime):
|
|
325
|
+
return self._inner >= other._inner
|
|
326
|
+
return NotImplemented
|
|
327
|
+
|
|
328
|
+
def __hash__(self) -> int:
|
|
329
|
+
return hash(self._inner)
|
|
330
|
+
|
|
331
|
+
def __repr__(self) -> str:
|
|
332
|
+
base = (
|
|
333
|
+
f"LocalDateTime({self.year}, {self.month}, {self.day}, "
|
|
334
|
+
f"{self.hour}, {self.minute}, {self.second}"
|
|
335
|
+
)
|
|
336
|
+
if self.nanosecond:
|
|
337
|
+
if self.nanosecond % 1_000 == 0:
|
|
338
|
+
return base + f", microsecond={self.microsecond})"
|
|
339
|
+
return (
|
|
340
|
+
base
|
|
341
|
+
+ f", microsecond={self.microsecond}, "
|
|
342
|
+
+ f"nanosecond={self.nanosecond % 1_000})"
|
|
343
|
+
)
|
|
344
|
+
return base + ")"
|
|
345
|
+
|
|
346
|
+
def __str__(self) -> str:
|
|
347
|
+
return self.isoformat()
|