timecore 0.1.0__tar.gz → 0.2.0__tar.gz
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-0.2.0/PKG-INFO +142 -0
- timecore-0.2.0/README.md +120 -0
- {timecore-0.1.0 → timecore-0.2.0}/pyproject.toml +4 -4
- {timecore-0.1.0 → timecore-0.2.0}/pyproject.toml.orig +4 -4
- timecore-0.2.0/src/timecore/_internal/__init__.py +7 -0
- timecore-0.2.0/src/timecore/_internal/timezone.py +90 -0
- timecore-0.2.0/src/timecore/_internal/validate.py +111 -0
- {timecore-0.1.0 → timecore-0.2.0}/src/timecore/availability/availability.py +11 -37
- timecore-0.2.0/src/timecore/calendar/_common.py +35 -0
- {timecore-0.1.0 → timecore-0.2.0}/src/timecore/calendar/business.py +1 -2
- {timecore-0.1.0 → timecore-0.2.0}/src/timecore/calendar/calendar.py +2 -1
- {timecore-0.1.0 → timecore-0.2.0}/src/timecore/calendar/holidays.py +2 -5
- {timecore-0.1.0 → timecore-0.2.0}/src/timecore/clock/clock.py +5 -1
- {timecore-0.1.0 → timecore-0.2.0}/src/timecore/core/_common.py +28 -74
- {timecore-0.1.0 → timecore-0.2.0}/src/timecore/core/date.py +29 -16
- {timecore-0.1.0 → timecore-0.2.0}/src/timecore/core/duration.py +20 -18
- {timecore-0.1.0 → timecore-0.2.0}/src/timecore/core/instant.py +12 -17
- {timecore-0.1.0 → timecore-0.2.0}/src/timecore/core/local_datetime.py +36 -28
- {timecore-0.1.0 → timecore-0.2.0}/src/timecore/core/period.py +2 -1
- {timecore-0.1.0 → timecore-0.2.0}/src/timecore/core/time.py +17 -19
- {timecore-0.1.0 → timecore-0.2.0}/src/timecore/core/timezone.py +2 -1
- {timecore-0.1.0 → timecore-0.2.0}/src/timecore/core/zoned_datetime.py +45 -30
- {timecore-0.1.0 → timecore-0.2.0}/src/timecore/formatting/format.py +2 -15
- {timecore-0.1.0 → timecore-0.2.0}/src/timecore/interval/interval.py +8 -16
- {timecore-0.1.0 → timecore-0.2.0}/src/timecore/parsing/parse.py +10 -63
- {timecore-0.1.0 → timecore-0.2.0}/src/timecore/recurrence/_shared.py +5 -39
- {timecore-0.1.0 → timecore-0.2.0}/src/timecore/recurrence/cron.py +4 -9
- {timecore-0.1.0 → timecore-0.2.0}/src/timecore/recurrence/recurrence.py +47 -43
- {timecore-0.1.0 → timecore-0.2.0}/src/timecore/schedule/schedule.py +34 -62
- timecore-0.1.0/PKG-INFO +0 -111
- timecore-0.1.0/README.md +0 -89
- timecore-0.1.0/src/timecore/_internal/__init__.py +0 -1
- timecore-0.1.0/src/timecore/calendar/_common.py +0 -71
- {timecore-0.1.0 → timecore-0.2.0}/src/timecore/__init__.py +0 -0
- {timecore-0.1.0 → timecore-0.2.0}/src/timecore/availability/__init__.py +0 -0
- {timecore-0.1.0 → timecore-0.2.0}/src/timecore/calendar/__init__.py +0 -0
- {timecore-0.1.0 → timecore-0.2.0}/src/timecore/clock/__init__.py +0 -0
- {timecore-0.1.0 → timecore-0.2.0}/src/timecore/clock/timer.py +0 -0
- {timecore-0.1.0 → timecore-0.2.0}/src/timecore/core/__init__.py +0 -0
- {timecore-0.1.0 → timecore-0.2.0}/src/timecore/core/weekday.py +0 -0
- {timecore-0.1.0 → timecore-0.2.0}/src/timecore/errors.py +0 -0
- {timecore-0.1.0 → timecore-0.2.0}/src/timecore/formatting/__init__.py +0 -0
- {timecore-0.1.0 → timecore-0.2.0}/src/timecore/interval/__init__.py +0 -0
- {timecore-0.1.0 → timecore-0.2.0}/src/timecore/interval/interval_set.py +0 -0
- {timecore-0.1.0 → timecore-0.2.0}/src/timecore/parsing/__init__.py +0 -0
- {timecore-0.1.0 → timecore-0.2.0}/src/timecore/py.typed +0 -0
- {timecore-0.1.0 → timecore-0.2.0}/src/timecore/recurrence/__init__.py +0 -0
- {timecore-0.1.0 → timecore-0.2.0}/src/timecore/schedule/__init__.py +0 -0
timecore-0.2.0/PKG-INFO
ADDED
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: timecore
|
|
3
|
+
Version: 0.2.0
|
|
4
|
+
Summary: A modern, unified, fully typed, cross-platform Python library for date, time, duration, intervals, calendars, recurrence, and scheduling.
|
|
5
|
+
Author: Oceanus
|
|
6
|
+
Author-email: Oceanus <295322314+oceanus403@users.noreply.github.com>
|
|
7
|
+
License-Expression: MIT
|
|
8
|
+
Requires-Dist: whenever>=0.11.0
|
|
9
|
+
Requires-Dist: tzdata>=2025.1
|
|
10
|
+
Requires-Dist: tzlocal>=5.0
|
|
11
|
+
Requires-Dist: python-dateutil>=2.9.0
|
|
12
|
+
Requires-Dist: holidays>=0.109
|
|
13
|
+
Requires-Dist: portion>=2.6.3
|
|
14
|
+
Requires-Dist: babel>=2.19.0
|
|
15
|
+
Requires-Dist: croniter>=7.0.0
|
|
16
|
+
Requires-Dist: dateparser>=1.5.1 ; extra == 'natural'
|
|
17
|
+
Requires-Python: >=3.12
|
|
18
|
+
Project-URL: Repository, https://github.com/oceanus403/timecore
|
|
19
|
+
Project-URL: Documentation, https://oceanus403.github.io/timecore/
|
|
20
|
+
Provides-Extra: natural
|
|
21
|
+
Description-Content-Type: text/markdown
|
|
22
|
+
|
|
23
|
+
<div align="center">
|
|
24
|
+
|
|
25
|
+
<h1>TimeCore</h1>
|
|
26
|
+
|
|
27
|
+
<p><strong>A modern, unified, fully typed Python library for date and time</strong></p>
|
|
28
|
+
|
|
29
|
+
<p>
|
|
30
|
+
Date, time, duration, intervals, calendars, recurrence, and scheduling<br>
|
|
31
|
+
under one consistent, strongly typed API.
|
|
32
|
+
</p>
|
|
33
|
+
|
|
34
|
+
<p>
|
|
35
|
+
<a href="https://pypi.org/project/timecore/"><img alt="PyPI version" src="https://img.shields.io/pypi/v/timecore"></a>
|
|
36
|
+
<a href="pyproject.toml"><img alt="Python 3.12+" src="https://img.shields.io/badge/python-3.12%2B-blue"></a>
|
|
37
|
+
<a href="LICENSE"><img alt="License: MIT" src="https://img.shields.io/badge/license-MIT-blue"></a>
|
|
38
|
+
</p>
|
|
39
|
+
|
|
40
|
+
<p>
|
|
41
|
+
<a href="https://oceanus403.github.io/timecore/"><b>Documentation</b></a>
|
|
42
|
+
·
|
|
43
|
+
<a href="#capabilities"><b>Capabilities</b></a>
|
|
44
|
+
·
|
|
45
|
+
<a href="#installation"><b>Installation</b></a>
|
|
46
|
+
·
|
|
47
|
+
<a href="#usage"><b>Usage</b></a>
|
|
48
|
+
·
|
|
49
|
+
<a href="https://oceanus403.github.io/timecore/docs/contributing"><b>Contributing</b></a>
|
|
50
|
+
</p>
|
|
51
|
+
|
|
52
|
+
</div>
|
|
53
|
+
|
|
54
|
+
<br>
|
|
55
|
+
|
|
56
|
+
## Capabilities
|
|
57
|
+
|
|
58
|
+
| Family | Covers |
|
|
59
|
+
|:--|:--|
|
|
60
|
+
| [Core types](docs/content/docs/core-types/index.mdx) | `Date`, `Time`, `LocalDateTime`, `DateTime`, `Instant`, `TimeZone`, `Weekday`; immutable value objects, DST-aware arithmetic, native interop |
|
|
61
|
+
| [Durations & periods](docs/content/docs/durations-periods/index.mdx) | `Duration` for exact lengths of time, `Period` for calendar-based amounts |
|
|
62
|
+
| [Intervals](docs/content/docs/intervals/index.mdx) | `Interval` and `IntervalSet`; half-open by default, set operations |
|
|
63
|
+
| [Calendars & business time](docs/content/docs/calendars-business-time/index.mdx) | `Calendar`, `HolidayCalendar`, `BusinessCalendar`; holidays, business days and hours |
|
|
64
|
+
| [Recurrence & cron](docs/content/docs/recurrence-cron/index.mdx) | `Recurrence` rules with iCalendar interchange, `Cron` expressions |
|
|
65
|
+
| [Schedules & availability](docs/content/docs/schedules-availability/index.mdx) | `Schedule` combining rules with times, timezones and calendars; `Availability` free/busy time |
|
|
66
|
+
| [Parsing](docs/content/docs/parsing/index.mdx) | Strict ISO 8601 parsing, custom formats, optional natural-language parsing |
|
|
67
|
+
| [Formatting & localization](docs/content/docs/formatting-localization/index.mdx) | Locale-aware formatting of temporal values and durations |
|
|
68
|
+
| [Clocks & timers](docs/content/docs/clocks-timers/index.mdx) | Injectable `Clock` for deterministic tests, `Timer` for elapsed time |
|
|
69
|
+
|
|
70
|
+
## Installation
|
|
71
|
+
|
|
72
|
+
TimeCore supports Python 3.12 and newer.
|
|
73
|
+
|
|
74
|
+
Add it to a uv project:
|
|
75
|
+
|
|
76
|
+
```sh
|
|
77
|
+
uv add timecore
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
To install it in an active Python environment:
|
|
81
|
+
|
|
82
|
+
```sh
|
|
83
|
+
pip install timecore
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
To build from source:
|
|
87
|
+
|
|
88
|
+
```sh
|
|
89
|
+
git clone https://github.com/oceanus403/timecore
|
|
90
|
+
cd timecore
|
|
91
|
+
uv sync --locked
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
## Usage
|
|
95
|
+
|
|
96
|
+
```python
|
|
97
|
+
from timecore import (
|
|
98
|
+
BusinessCalendar,
|
|
99
|
+
Date,
|
|
100
|
+
DateTime,
|
|
101
|
+
Duration,
|
|
102
|
+
Interval,
|
|
103
|
+
Period,
|
|
104
|
+
Recurrence,
|
|
105
|
+
Schedule,
|
|
106
|
+
Time,
|
|
107
|
+
Weekday,
|
|
108
|
+
parse_date,
|
|
109
|
+
)
|
|
110
|
+
|
|
111
|
+
# Dates and calendar arithmetic.
|
|
112
|
+
today = Date(2026, 11, 25)
|
|
113
|
+
next_month = today + Period(months=1) # 2026-12-25
|
|
114
|
+
ten_days = today + Duration(days=10) # 2026-12-05
|
|
115
|
+
|
|
116
|
+
# A zoned datetime; DST is handled, never guessed.
|
|
117
|
+
meeting = DateTime(2026, 11, 25, 9, 0, timezone="America/New_York")
|
|
118
|
+
instant = meeting.to_instant() # 2026-11-25T14:00:00Z
|
|
119
|
+
|
|
120
|
+
# A half-open interval and a weekly schedule.
|
|
121
|
+
week = Interval(today, today + Duration(days=7))
|
|
122
|
+
schedule = Schedule(
|
|
123
|
+
Recurrence.weekly([Weekday.MONDAY, Weekday.FRIDAY]),
|
|
124
|
+
time=Time(9, 0),
|
|
125
|
+
timezone="America/New_York",
|
|
126
|
+
calendar=BusinessCalendar(country="US"),
|
|
127
|
+
)
|
|
128
|
+
occurrences = schedule.occurrences(today, count=3)
|
|
129
|
+
|
|
130
|
+
# Strict parsing and locale-aware formatting.
|
|
131
|
+
deadline = parse_date("2026-12-25")
|
|
132
|
+
print(deadline.format()) # Dec 25, 2026
|
|
133
|
+
print(deadline.format(locale="fr_FR")) # 25 déc. 2026
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
## Project status
|
|
137
|
+
|
|
138
|
+
TimeCore 0.1.0 is released. The API may change before 1.0.
|
|
139
|
+
|
|
140
|
+
## License
|
|
141
|
+
|
|
142
|
+
MIT. See [LICENSE](LICENSE).
|
timecore-0.2.0/README.md
ADDED
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
<div align="center">
|
|
2
|
+
|
|
3
|
+
<h1>TimeCore</h1>
|
|
4
|
+
|
|
5
|
+
<p><strong>A modern, unified, fully typed Python library for date and time</strong></p>
|
|
6
|
+
|
|
7
|
+
<p>
|
|
8
|
+
Date, time, duration, intervals, calendars, recurrence, and scheduling<br>
|
|
9
|
+
under one consistent, strongly typed API.
|
|
10
|
+
</p>
|
|
11
|
+
|
|
12
|
+
<p>
|
|
13
|
+
<a href="https://pypi.org/project/timecore/"><img alt="PyPI version" src="https://img.shields.io/pypi/v/timecore"></a>
|
|
14
|
+
<a href="pyproject.toml"><img alt="Python 3.12+" src="https://img.shields.io/badge/python-3.12%2B-blue"></a>
|
|
15
|
+
<a href="LICENSE"><img alt="License: MIT" src="https://img.shields.io/badge/license-MIT-blue"></a>
|
|
16
|
+
</p>
|
|
17
|
+
|
|
18
|
+
<p>
|
|
19
|
+
<a href="https://oceanus403.github.io/timecore/"><b>Documentation</b></a>
|
|
20
|
+
·
|
|
21
|
+
<a href="#capabilities"><b>Capabilities</b></a>
|
|
22
|
+
·
|
|
23
|
+
<a href="#installation"><b>Installation</b></a>
|
|
24
|
+
·
|
|
25
|
+
<a href="#usage"><b>Usage</b></a>
|
|
26
|
+
·
|
|
27
|
+
<a href="https://oceanus403.github.io/timecore/docs/contributing"><b>Contributing</b></a>
|
|
28
|
+
</p>
|
|
29
|
+
|
|
30
|
+
</div>
|
|
31
|
+
|
|
32
|
+
<br>
|
|
33
|
+
|
|
34
|
+
## Capabilities
|
|
35
|
+
|
|
36
|
+
| Family | Covers |
|
|
37
|
+
|:--|:--|
|
|
38
|
+
| [Core types](docs/content/docs/core-types/index.mdx) | `Date`, `Time`, `LocalDateTime`, `DateTime`, `Instant`, `TimeZone`, `Weekday`; immutable value objects, DST-aware arithmetic, native interop |
|
|
39
|
+
| [Durations & periods](docs/content/docs/durations-periods/index.mdx) | `Duration` for exact lengths of time, `Period` for calendar-based amounts |
|
|
40
|
+
| [Intervals](docs/content/docs/intervals/index.mdx) | `Interval` and `IntervalSet`; half-open by default, set operations |
|
|
41
|
+
| [Calendars & business time](docs/content/docs/calendars-business-time/index.mdx) | `Calendar`, `HolidayCalendar`, `BusinessCalendar`; holidays, business days and hours |
|
|
42
|
+
| [Recurrence & cron](docs/content/docs/recurrence-cron/index.mdx) | `Recurrence` rules with iCalendar interchange, `Cron` expressions |
|
|
43
|
+
| [Schedules & availability](docs/content/docs/schedules-availability/index.mdx) | `Schedule` combining rules with times, timezones and calendars; `Availability` free/busy time |
|
|
44
|
+
| [Parsing](docs/content/docs/parsing/index.mdx) | Strict ISO 8601 parsing, custom formats, optional natural-language parsing |
|
|
45
|
+
| [Formatting & localization](docs/content/docs/formatting-localization/index.mdx) | Locale-aware formatting of temporal values and durations |
|
|
46
|
+
| [Clocks & timers](docs/content/docs/clocks-timers/index.mdx) | Injectable `Clock` for deterministic tests, `Timer` for elapsed time |
|
|
47
|
+
|
|
48
|
+
## Installation
|
|
49
|
+
|
|
50
|
+
TimeCore supports Python 3.12 and newer.
|
|
51
|
+
|
|
52
|
+
Add it to a uv project:
|
|
53
|
+
|
|
54
|
+
```sh
|
|
55
|
+
uv add timecore
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
To install it in an active Python environment:
|
|
59
|
+
|
|
60
|
+
```sh
|
|
61
|
+
pip install timecore
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
To build from source:
|
|
65
|
+
|
|
66
|
+
```sh
|
|
67
|
+
git clone https://github.com/oceanus403/timecore
|
|
68
|
+
cd timecore
|
|
69
|
+
uv sync --locked
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
## Usage
|
|
73
|
+
|
|
74
|
+
```python
|
|
75
|
+
from timecore import (
|
|
76
|
+
BusinessCalendar,
|
|
77
|
+
Date,
|
|
78
|
+
DateTime,
|
|
79
|
+
Duration,
|
|
80
|
+
Interval,
|
|
81
|
+
Period,
|
|
82
|
+
Recurrence,
|
|
83
|
+
Schedule,
|
|
84
|
+
Time,
|
|
85
|
+
Weekday,
|
|
86
|
+
parse_date,
|
|
87
|
+
)
|
|
88
|
+
|
|
89
|
+
# Dates and calendar arithmetic.
|
|
90
|
+
today = Date(2026, 11, 25)
|
|
91
|
+
next_month = today + Period(months=1) # 2026-12-25
|
|
92
|
+
ten_days = today + Duration(days=10) # 2026-12-05
|
|
93
|
+
|
|
94
|
+
# A zoned datetime; DST is handled, never guessed.
|
|
95
|
+
meeting = DateTime(2026, 11, 25, 9, 0, timezone="America/New_York")
|
|
96
|
+
instant = meeting.to_instant() # 2026-11-25T14:00:00Z
|
|
97
|
+
|
|
98
|
+
# A half-open interval and a weekly schedule.
|
|
99
|
+
week = Interval(today, today + Duration(days=7))
|
|
100
|
+
schedule = Schedule(
|
|
101
|
+
Recurrence.weekly([Weekday.MONDAY, Weekday.FRIDAY]),
|
|
102
|
+
time=Time(9, 0),
|
|
103
|
+
timezone="America/New_York",
|
|
104
|
+
calendar=BusinessCalendar(country="US"),
|
|
105
|
+
)
|
|
106
|
+
occurrences = schedule.occurrences(today, count=3)
|
|
107
|
+
|
|
108
|
+
# Strict parsing and locale-aware formatting.
|
|
109
|
+
deadline = parse_date("2026-12-25")
|
|
110
|
+
print(deadline.format()) # Dec 25, 2026
|
|
111
|
+
print(deadline.format(locale="fr_FR")) # 25 déc. 2026
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
## Project status
|
|
115
|
+
|
|
116
|
+
TimeCore 0.1.0 is released. The API may change before 1.0.
|
|
117
|
+
|
|
118
|
+
## License
|
|
119
|
+
|
|
120
|
+
MIT. See [LICENSE](LICENSE).
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
[project]
|
|
2
2
|
name = "timecore"
|
|
3
|
-
version = "0.
|
|
3
|
+
version = "0.2.0"
|
|
4
4
|
description = "A modern, unified, fully typed, cross-platform Python library for date, time, duration, intervals, calendars, recurrence, and scheduling."
|
|
5
5
|
readme = "README.md"
|
|
6
6
|
license = "MIT"
|
|
7
|
-
requires-python = ">=3.
|
|
7
|
+
requires-python = ">=3.12"
|
|
8
8
|
dependencies = [
|
|
9
9
|
"whenever>=0.11.0",
|
|
10
10
|
"tzdata>=2025.1",
|
|
@@ -38,10 +38,10 @@ module-root = "src"
|
|
|
38
38
|
testpaths = ["tests"]
|
|
39
39
|
|
|
40
40
|
[tool.ruff]
|
|
41
|
-
target-version = "
|
|
41
|
+
target-version = "py312"
|
|
42
42
|
|
|
43
43
|
[tool.pyright]
|
|
44
|
-
pythonVersion = "3.
|
|
44
|
+
pythonVersion = "3.12"
|
|
45
45
|
typeCheckingMode = "strict"
|
|
46
46
|
include = ["src/timecore"]
|
|
47
47
|
|
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
[project]
|
|
2
2
|
name = "timecore"
|
|
3
|
-
version = "0.
|
|
3
|
+
version = "0.2.0"
|
|
4
4
|
description = "A modern, unified, fully typed, cross-platform Python library for date, time, duration, intervals, calendars, recurrence, and scheduling."
|
|
5
5
|
readme = "README.md"
|
|
6
6
|
license = "MIT"
|
|
7
7
|
authors = [
|
|
8
8
|
{ name = "Oceanus", email = "295322314+oceanus403@users.noreply.github.com" }
|
|
9
9
|
]
|
|
10
|
-
requires-python = ">=3.
|
|
10
|
+
requires-python = ">=3.12"
|
|
11
11
|
dependencies = [
|
|
12
12
|
"whenever>=0.11.0",
|
|
13
13
|
"tzdata>=2025.1",
|
|
@@ -40,10 +40,10 @@ module-root = "src"
|
|
|
40
40
|
testpaths = ["tests"]
|
|
41
41
|
|
|
42
42
|
[tool.ruff]
|
|
43
|
-
target-version = "
|
|
43
|
+
target-version = "py312"
|
|
44
44
|
|
|
45
45
|
[tool.pyright]
|
|
46
|
-
pythonVersion = "3.
|
|
46
|
+
pythonVersion = "3.12"
|
|
47
47
|
typeCheckingMode = "strict"
|
|
48
48
|
# Pyright covers the library source only. Tests exercise runtime behavior
|
|
49
49
|
# and are intentionally excluded from strict checking.
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
"""Shared private helpers. Not part of the public API.
|
|
2
|
+
|
|
3
|
+
``validate`` holds the runtime argument checks every workstream
|
|
4
|
+
shares; ``timezone`` holds time-zone argument resolution. Workstream
|
|
5
|
+
``_common`` / ``_shared`` modules keep only the helpers specific to
|
|
6
|
+
their own types.
|
|
7
|
+
"""
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
"""Time-zone argument resolution shared by every workstream.
|
|
2
|
+
|
|
3
|
+
Two flavours: :func:`_resolve_timezone` treats ``None`` as the local
|
|
4
|
+
zone, while :func:`_resolve_timezone_or_none` keeps ``None``. Both
|
|
5
|
+
accept a :class:`~timecore.core.TimeZone` or a zone key string.
|
|
6
|
+
:func:`_at_timezone` reads a wall time in a zone with the DST policy
|
|
7
|
+
shared by schedules and availability (ambiguous -> earlier,
|
|
8
|
+
nonexistent -> later).
|
|
9
|
+
|
|
10
|
+
The ``TimeZone`` class is imported lazily inside the functions so
|
|
11
|
+
this module creates no import cycle with ``timecore.core``.
|
|
12
|
+
"""
|
|
13
|
+
|
|
14
|
+
from __future__ import annotations
|
|
15
|
+
|
|
16
|
+
from typing import TYPE_CHECKING
|
|
17
|
+
|
|
18
|
+
from ..errors import AmbiguousTimeError, NonexistentTimeError
|
|
19
|
+
|
|
20
|
+
if TYPE_CHECKING:
|
|
21
|
+
from ..core.local_datetime import LocalDateTime
|
|
22
|
+
from ..core.timezone import TimeZone
|
|
23
|
+
from ..core.zoned_datetime import DateTime
|
|
24
|
+
|
|
25
|
+
__all__ = [
|
|
26
|
+
"_at_timezone",
|
|
27
|
+
"_resolve_timezone",
|
|
28
|
+
"_resolve_timezone_or_none",
|
|
29
|
+
"_resolve_timezone_required",
|
|
30
|
+
]
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
def _resolve_timezone(timezone: object) -> TimeZone:
|
|
34
|
+
"""Resolve a ``timezone`` argument: ``None`` means the local zone.
|
|
35
|
+
|
|
36
|
+
Takes ``object`` so pyright does not flag the type checks as
|
|
37
|
+
unnecessary when callers already declare the union type.
|
|
38
|
+
"""
|
|
39
|
+
from ..core.timezone import TimeZone
|
|
40
|
+
|
|
41
|
+
if timezone is None:
|
|
42
|
+
return TimeZone.local()
|
|
43
|
+
if isinstance(timezone, str):
|
|
44
|
+
return TimeZone(timezone)
|
|
45
|
+
if isinstance(timezone, TimeZone):
|
|
46
|
+
return timezone
|
|
47
|
+
raise TypeError(
|
|
48
|
+
f"timezone must be a TimeZone, a string, or None, got {type(timezone).__name__}"
|
|
49
|
+
)
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
def _resolve_timezone_or_none(value: object, what: str) -> TimeZone | None:
|
|
53
|
+
"""Resolve a ``TimeZone | str | None`` argument, keeping ``None``.
|
|
54
|
+
|
|
55
|
+
Takes ``object`` so pyright does not flag the checks as
|
|
56
|
+
unnecessary when callers already declare the union type.
|
|
57
|
+
"""
|
|
58
|
+
from ..core.timezone import TimeZone
|
|
59
|
+
|
|
60
|
+
if value is None:
|
|
61
|
+
return None
|
|
62
|
+
if isinstance(value, str):
|
|
63
|
+
return TimeZone(value)
|
|
64
|
+
if isinstance(value, TimeZone):
|
|
65
|
+
return value
|
|
66
|
+
raise TypeError(
|
|
67
|
+
f"{what} must be a TimeZone, a string, or None, got {type(value).__name__}"
|
|
68
|
+
)
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
def _resolve_timezone_required(timezone: TimeZone | str | None) -> TimeZone:
|
|
72
|
+
"""Resolve a ``timezone`` argument that must be given explicitly."""
|
|
73
|
+
if timezone is None:
|
|
74
|
+
raise TypeError("a timezone is required here")
|
|
75
|
+
return _resolve_timezone(timezone)
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
def _at_timezone(local: LocalDateTime, timezone: TimeZone) -> DateTime:
|
|
79
|
+
"""Interpret a wall time in ``timezone``.
|
|
80
|
+
|
|
81
|
+
Ambiguous wall times resolve to the earlier occurrence and
|
|
82
|
+
nonexistent wall times shift forward to the first valid time
|
|
83
|
+
after the gap.
|
|
84
|
+
"""
|
|
85
|
+
try:
|
|
86
|
+
return local.at_timezone(timezone)
|
|
87
|
+
except AmbiguousTimeError:
|
|
88
|
+
return local.at_timezone(timezone, disambiguation="earlier")
|
|
89
|
+
except NonexistentTimeError:
|
|
90
|
+
return local.at_timezone(timezone, disambiguation="later")
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
"""Runtime argument validation shared by every workstream.
|
|
2
|
+
|
|
3
|
+
This is the single home for the small ``_require_*`` checks that
|
|
4
|
+
every workstream used to copy: a value is returned unchanged when it
|
|
5
|
+
has the expected type, else a ``TypeError`` (or ``ValueError`` for a
|
|
6
|
+
bad value) names the argument and the offending type. Workstream
|
|
7
|
+
``_common`` / ``_shared`` modules keep only the checks specific to
|
|
8
|
+
their own types (``_require_date``, ``_require_temporal``).
|
|
9
|
+
|
|
10
|
+
The checks take ``object`` so pyright does not flag them as
|
|
11
|
+
unnecessary when callers already declare a typed parameter; they
|
|
12
|
+
still protect against wrongly typed callers at runtime.
|
|
13
|
+
|
|
14
|
+
This module imports nothing from ``timecore`` itself (except
|
|
15
|
+
``TYPE_CHECKING`` annotations), so any module can import it without
|
|
16
|
+
creating an import cycle. The ``Duration`` / ``Time`` checks import
|
|
17
|
+
their class lazily inside the function for the same reason.
|
|
18
|
+
"""
|
|
19
|
+
|
|
20
|
+
from __future__ import annotations
|
|
21
|
+
|
|
22
|
+
from typing import TYPE_CHECKING
|
|
23
|
+
|
|
24
|
+
if TYPE_CHECKING:
|
|
25
|
+
from ..core.duration import Duration
|
|
26
|
+
from ..core.time import Time
|
|
27
|
+
|
|
28
|
+
__all__ = [
|
|
29
|
+
"_optional_str",
|
|
30
|
+
"_require_duration",
|
|
31
|
+
"_require_instance",
|
|
32
|
+
"_require_int",
|
|
33
|
+
"_require_number",
|
|
34
|
+
"_require_positive",
|
|
35
|
+
"_require_positive_duration",
|
|
36
|
+
"_require_str",
|
|
37
|
+
"_require_time_or_none",
|
|
38
|
+
]
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
def _require_str(value: object, what: str) -> str:
|
|
42
|
+
"""Return ``value`` if it is a string, else raise ``TypeError``."""
|
|
43
|
+
if not isinstance(value, str):
|
|
44
|
+
raise TypeError(f"{what} must be a string, got {type(value).__name__}")
|
|
45
|
+
return value
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
def _optional_str(value: object, what: str) -> str | None:
|
|
49
|
+
"""Return ``value`` if it is None or a string, else raise ``TypeError``."""
|
|
50
|
+
if value is None or isinstance(value, str):
|
|
51
|
+
return value
|
|
52
|
+
raise TypeError(f"{what} must be a string, got {type(value).__name__}")
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
def _require_int(value: object, what: str) -> int:
|
|
56
|
+
"""Return ``value`` if it is an int (not a bool), else raise ``TypeError``."""
|
|
57
|
+
if isinstance(value, bool) or not isinstance(value, int):
|
|
58
|
+
raise TypeError(f"{what} must be an int, got {type(value).__name__}")
|
|
59
|
+
return value
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
def _require_number(value: object, what: str) -> int | float:
|
|
63
|
+
"""Return ``value`` if it is an int or float (not a bool), else raise ``TypeError``."""
|
|
64
|
+
if isinstance(value, bool) or not isinstance(value, (int, float)):
|
|
65
|
+
raise TypeError(f"{what} must be an int or float, got {type(value).__name__}")
|
|
66
|
+
return value
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
def _require_instance[T](value: object, cls: type[T], what: str) -> T:
|
|
70
|
+
"""Return ``value`` if it is an instance of ``cls``, else raise ``TypeError``."""
|
|
71
|
+
if not isinstance(value, cls):
|
|
72
|
+
raise TypeError(f"{what} must be a {cls.__name__}, got {type(value).__name__}")
|
|
73
|
+
return value
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
def _require_positive(value: object, what: str) -> int:
|
|
77
|
+
"""Return ``value`` if it is an int >= 1, else raise ``TypeError`` / ``ValueError``."""
|
|
78
|
+
if isinstance(value, bool) or not isinstance(value, int):
|
|
79
|
+
raise TypeError(f"{what} must be an int, got {type(value).__name__}")
|
|
80
|
+
if value < 1:
|
|
81
|
+
raise ValueError(f"{what} must be >= 1, got {value}")
|
|
82
|
+
return value
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
def _require_duration(value: object, what: str) -> Duration:
|
|
86
|
+
"""Return ``value`` if it is a :class:`~timecore.core.Duration`, else raise ``TypeError``."""
|
|
87
|
+
from ..core.duration import Duration
|
|
88
|
+
|
|
89
|
+
if not isinstance(value, Duration):
|
|
90
|
+
raise TypeError(f"{what} must be a Duration, got {type(value).__name__}")
|
|
91
|
+
return value
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
def _require_positive_duration(value: object, what: str) -> Duration:
|
|
95
|
+
"""Return ``value`` if it is a positive :class:`~timecore.core.Duration`, else raise."""
|
|
96
|
+
from ..core.duration import Duration
|
|
97
|
+
|
|
98
|
+
if not isinstance(value, Duration):
|
|
99
|
+
raise TypeError(f"{what} must be a Duration, got {type(value).__name__}")
|
|
100
|
+
if value <= Duration.ZERO:
|
|
101
|
+
raise ValueError(f"{what} must be positive, got {value}")
|
|
102
|
+
return value
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
def _require_time_or_none(value: object, what: str) -> Time | None:
|
|
106
|
+
"""Return ``value`` if it is a :class:`~timecore.core.Time` or None, else raise ``TypeError``."""
|
|
107
|
+
from ..core.time import Time
|
|
108
|
+
|
|
109
|
+
if value is None or isinstance(value, Time):
|
|
110
|
+
return value
|
|
111
|
+
raise TypeError(f"{what} must be a Time or None, got {type(value).__name__}")
|
|
@@ -29,9 +29,10 @@ from __future__ import annotations
|
|
|
29
29
|
from collections.abc import Iterable
|
|
30
30
|
from typing import cast
|
|
31
31
|
|
|
32
|
+
from .._internal.timezone import _at_timezone
|
|
33
|
+
from .._internal.validate import _require_positive_duration, _require_time_or_none
|
|
32
34
|
from ..calendar import BusinessCalendar
|
|
33
|
-
from ..core import DateTime, Duration,
|
|
34
|
-
from ..errors import AmbiguousTimeError, NonexistentTimeError
|
|
35
|
+
from ..core import DateTime, Duration, Period, Time
|
|
35
36
|
from ..interval import Interval, IntervalSet
|
|
36
37
|
|
|
37
38
|
__all__ = ["Availability"]
|
|
@@ -46,21 +47,6 @@ _DEFAULT_WORK_END = Time(17)
|
|
|
46
47
|
_DEFAULT_HORIZON = Duration(days=365)
|
|
47
48
|
|
|
48
49
|
|
|
49
|
-
def _at_timezone(local: LocalDateTime, timezone: TimeZone) -> DateTime:
|
|
50
|
-
"""Interpret a wall time in ``timezone``.
|
|
51
|
-
|
|
52
|
-
Ambiguous wall times resolve to the earlier occurrence and
|
|
53
|
-
nonexistent wall times shift forward to the first valid time
|
|
54
|
-
after the gap.
|
|
55
|
-
"""
|
|
56
|
-
try:
|
|
57
|
-
return local.at_timezone(timezone)
|
|
58
|
-
except AmbiguousTimeError:
|
|
59
|
-
return local.at_timezone(timezone, disambiguation="earlier")
|
|
60
|
-
except NonexistentTimeError:
|
|
61
|
-
return local.at_timezone(timezone, disambiguation="later")
|
|
62
|
-
|
|
63
|
-
|
|
64
50
|
def _require_datetime(value: object, what: str) -> DateTime:
|
|
65
51
|
"""Return ``value`` if it is a DateTime, else raise."""
|
|
66
52
|
if isinstance(value, DateTime):
|
|
@@ -68,22 +54,6 @@ def _require_datetime(value: object, what: str) -> DateTime:
|
|
|
68
54
|
raise TypeError(f"{what} must be a DateTime, got {type(value).__name__}")
|
|
69
55
|
|
|
70
56
|
|
|
71
|
-
def _require_duration(value: object, what: str) -> Duration:
|
|
72
|
-
"""Return ``value`` if it is a positive Duration, else raise."""
|
|
73
|
-
if not isinstance(value, Duration):
|
|
74
|
-
raise TypeError(f"{what} must be a Duration, got {type(value).__name__}")
|
|
75
|
-
if value <= Duration.ZERO:
|
|
76
|
-
raise ValueError(f"{what} must be positive, got {value}")
|
|
77
|
-
return value
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
def _require_time_or_none(value: object, what: str) -> Time | None:
|
|
81
|
-
"""Return ``value`` if it is a Time or None, else raise."""
|
|
82
|
-
if value is None or isinstance(value, Time):
|
|
83
|
-
return value
|
|
84
|
-
raise TypeError(f"{what} must be a Time or None, got {type(value).__name__}")
|
|
85
|
-
|
|
86
|
-
|
|
87
57
|
def _require_business_calendar_or_none(
|
|
88
58
|
value: object, what: str
|
|
89
59
|
) -> BusinessCalendar | None:
|
|
@@ -283,8 +253,12 @@ class Availability:
|
|
|
283
253
|
"""
|
|
284
254
|
start = _require_datetime(start, "slots() start")
|
|
285
255
|
end = _require_datetime(end, "slots() end")
|
|
286
|
-
duration =
|
|
287
|
-
step =
|
|
256
|
+
duration = _require_positive_duration(duration, "slots() duration")
|
|
257
|
+
step = (
|
|
258
|
+
duration
|
|
259
|
+
if step is None
|
|
260
|
+
else _require_positive_duration(step, "slots() step")
|
|
261
|
+
)
|
|
288
262
|
result: list[Interval] = []
|
|
289
263
|
for interval in self.free_intervals(start, end).intervals:
|
|
290
264
|
cursor = interval.start
|
|
@@ -315,7 +289,7 @@ class Availability:
|
|
|
315
289
|
ValueError: If ``until`` is before ``after``.
|
|
316
290
|
"""
|
|
317
291
|
after = _require_datetime(after, "next_available() after")
|
|
318
|
-
duration =
|
|
292
|
+
duration = _require_positive_duration(duration, "next_available() duration")
|
|
319
293
|
if until is not None:
|
|
320
294
|
until = _require_datetime(until, "next_available() until")
|
|
321
295
|
if until < after:
|
|
@@ -345,7 +319,7 @@ class Availability:
|
|
|
345
319
|
calendar is given, inside working hours on a business day).
|
|
346
320
|
"""
|
|
347
321
|
at = _require_datetime(at, "is_available() at")
|
|
348
|
-
duration =
|
|
322
|
+
duration = _require_positive_duration(duration, "is_available() duration")
|
|
349
323
|
end = at + duration
|
|
350
324
|
return self.free_intervals(at, end).total_duration() == duration
|
|
351
325
|
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
"""Private shared helpers for the calendar workstream.
|
|
2
|
+
|
|
3
|
+
These are small runtime validators and converters used by the calendar
|
|
4
|
+
modules. They are internal to :mod:`timecore.calendar` and are not part
|
|
5
|
+
of the public API.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from __future__ import annotations
|
|
9
|
+
|
|
10
|
+
from ..core import Date, Period, Time
|
|
11
|
+
|
|
12
|
+
__all__ = [
|
|
13
|
+
"_add_days",
|
|
14
|
+
"_require_date",
|
|
15
|
+
"_time_to_ns",
|
|
16
|
+
]
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def _require_date(value: object, what: str) -> Date:
|
|
20
|
+
"""Return ``value`` if it is a :class:`~timecore.core.Date`, else raise ``TypeError``."""
|
|
21
|
+
if not isinstance(value, Date):
|
|
22
|
+
raise TypeError(f"{what} must be a Date, got {type(value).__name__}")
|
|
23
|
+
return value
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
def _add_days(date: Date, days: int) -> Date:
|
|
27
|
+
"""Return ``date`` shifted by ``days`` calendar days."""
|
|
28
|
+
return date + Period(days=days)
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
def _time_to_ns(time: Time) -> int:
|
|
32
|
+
"""Return the number of nanoseconds since midnight for ``time``."""
|
|
33
|
+
return (
|
|
34
|
+
(time.hour * 60 + time.minute) * 60 + time.second
|
|
35
|
+
) * 1_000_000_000 + time.nanosecond
|
|
@@ -11,12 +11,11 @@ from __future__ import annotations
|
|
|
11
11
|
|
|
12
12
|
from collections.abc import Iterable
|
|
13
13
|
|
|
14
|
+
from .._internal.validate import _require_instance, _require_int
|
|
14
15
|
from ..core import Date, DateTime, Duration, LocalDateTime, Time, Weekday
|
|
15
16
|
from ._common import (
|
|
16
17
|
_add_days,
|
|
17
18
|
_require_date,
|
|
18
|
-
_require_instance,
|
|
19
|
-
_require_int,
|
|
20
19
|
_time_to_ns,
|
|
21
20
|
)
|
|
22
21
|
from .holidays import HolidayCalendar
|
|
@@ -4,8 +4,9 @@ from __future__ import annotations
|
|
|
4
4
|
|
|
5
5
|
import calendar as _stdlib_calendar
|
|
6
6
|
|
|
7
|
+
from .._internal.validate import _require_int
|
|
7
8
|
from ..core import Date
|
|
8
|
-
from ._common import _require_date
|
|
9
|
+
from ._common import _require_date
|
|
9
10
|
|
|
10
11
|
__all__ = ["Calendar"]
|
|
11
12
|
|