xtr-clock 3.0.0__tar.gz → 4.0.2__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.
- {xtr_clock-3.0.0 → xtr_clock-4.0.2}/LICENSE +1 -0
- {xtr_clock-3.0.0 → xtr_clock-4.0.2}/PKG-INFO +19 -14
- {xtr_clock-3.0.0 → xtr_clock-4.0.2}/README.md +16 -12
- {xtr_clock-3.0.0 → xtr_clock-4.0.2}/pyproject.toml +5 -5
- {xtr_clock-3.0.0 → xtr_clock-4.0.2}/pyproject.toml.orig +7 -5
- {xtr_clock-3.0.0 → xtr_clock-4.0.2}/src/xtr_clock/.agents/skills/xtr-clock/SKILL.md +4 -1
- {xtr_clock-3.0.0 → xtr_clock-4.0.2}/src/xtr_clock/bundle/clock_config.py +12 -0
- {xtr_clock-3.0.0 → xtr_clock-4.0.2}/src/xtr_clock/calendar_shift.py +6 -0
- {xtr_clock-3.0.0 → xtr_clock-4.0.2}/src/xtr_clock/clock_interface.py +3 -1
- {xtr_clock-3.0.0 → xtr_clock-4.0.2}/src/xtr_clock/mock_clock.py +6 -2
- {xtr_clock-3.0.0 → xtr_clock-4.0.2}/src/xtr_clock/monotonic_clock.py +10 -5
- {xtr_clock-3.0.0 → xtr_clock-4.0.2}/src/xtr_clock/system_clock.py +8 -3
- {xtr_clock-3.0.0 → xtr_clock-4.0.2}/src/xtr_clock/__init__.py +0 -0
- {xtr_clock-3.0.0 → xtr_clock-4.0.2}/src/xtr_clock/bundle/__init__.py +0 -0
- {xtr_clock-3.0.0 → xtr_clock-4.0.2}/src/xtr_clock/bundle/clock_bundle.py +0 -0
- {xtr_clock-3.0.0 → xtr_clock-4.0.2}/src/xtr_clock/clock.py +0 -0
- {xtr_clock-3.0.0 → xtr_clock-4.0.2}/src/xtr_clock/clock_aware_mixin.py +0 -0
- {xtr_clock-3.0.0 → xtr_clock-4.0.2}/src/xtr_clock/date_point.py +0 -0
- {xtr_clock-3.0.0 → xtr_clock-4.0.2}/src/xtr_clock/exception/__init__.py +0 -0
- {xtr_clock-3.0.0 → xtr_clock-4.0.2}/src/xtr_clock/exception/clock_error.py +0 -0
- {xtr_clock-3.0.0 → xtr_clock-4.0.2}/src/xtr_clock/exception/invalid_modifier_error.py +0 -0
- {xtr_clock-3.0.0 → xtr_clock-4.0.2}/src/xtr_clock/exception/invalid_timezone_error.py +0 -0
- {xtr_clock-3.0.0 → xtr_clock-4.0.2}/src/xtr_clock/modifier.py +0 -0
- {xtr_clock-3.0.0 → xtr_clock-4.0.2}/src/xtr_clock/now.py +0 -0
- {xtr_clock-3.0.0 → xtr_clock-4.0.2}/src/xtr_clock/py.typed +0 -0
- {xtr_clock-3.0.0 → xtr_clock-4.0.2}/src/xtr_clock/pytest_plugin.py +0 -0
- {xtr_clock-3.0.0 → xtr_clock-4.0.2}/src/xtr_clock/recipe/__init__.py +0 -0
- {xtr_clock-3.0.0 → xtr_clock-4.0.2}/src/xtr_clock/recipe/manifest.toml +0 -0
- {xtr_clock-3.0.0 → xtr_clock-4.0.2}/src/xtr_clock/testing.py +0 -0
- {xtr_clock-3.0.0 → xtr_clock-4.0.2}/src/xtr_clock/timezone.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: xtr-clock
|
|
3
|
-
Version:
|
|
3
|
+
Version: 4.0.2
|
|
4
4
|
Summary: Decouples an application from the system clock: an injectable clock, a timezone-aware instant, and a frozen clock for tests.
|
|
5
5
|
Keywords: clock,time,datetime,timezone,testing,freeze-time,monotonic
|
|
6
6
|
Author: Xterr
|
|
@@ -15,8 +15,9 @@ Classifier: Programming Language :: Python :: 3.13
|
|
|
15
15
|
Classifier: Programming Language :: Python :: 3.14
|
|
16
16
|
Classifier: Topic :: Software Development :: Testing
|
|
17
17
|
Classifier: Typing :: Typed
|
|
18
|
+
Requires-Dist: anyio>=4.0
|
|
18
19
|
Requires-Dist: typing-extensions>=4.4 ; extra == 'di'
|
|
19
|
-
Requires-Dist: xtr-dependency-injection>=
|
|
20
|
+
Requires-Dist: xtr-dependency-injection>=4.0,<5 ; extra == 'di'
|
|
20
21
|
Requires-Dist: tzdata>=2024.1 ; extra == 'tzdata'
|
|
21
22
|
Requires-Python: >=3.11
|
|
22
23
|
Provides-Extra: di
|
|
@@ -30,8 +31,8 @@ Description-Content-Type: text/markdown
|
|
|
30
31
|
**A clock an application can be handed, instead of the one it is standing on.**
|
|
31
32
|
|
|
32
33
|
<img alt="python 3.11+" src="https://img.shields.io/badge/python-%E2%89%A5%203.11-3776AB?logo=python&logoColor=white">
|
|
33
|
-
<img alt="core dependencies:
|
|
34
|
-
<img alt="
|
|
34
|
+
<img alt="core dependencies: anyio" src="https://img.shields.io/badge/core%20deps-anyio-3FB950">
|
|
35
|
+
<img alt="statements 100%" src="https://img.shields.io/badge/statements-100%25-3FB950">
|
|
35
36
|
<img alt="typed" src="https://img.shields.io/badge/typed-ty%20%2B%20basedpyright-1f6feb">
|
|
36
37
|
<img alt="license MIT" src="https://img.shields.io/badge/license-MIT-blue">
|
|
37
38
|
|
|
@@ -49,7 +50,8 @@ Take a **clock** as a constructor argument and the choice becomes configuration:
|
|
|
49
50
|
clock in production, a frozen one in a test. What comes back is a **`DatePoint`** — a
|
|
50
51
|
`datetime`, so nothing downstream has to change.
|
|
51
52
|
|
|
52
|
-
- 🪶 **
|
|
53
|
+
- 🪶 **One dependency.** `anyio`, so an awaitable wait is not tied to one async backend.
|
|
54
|
+
`zoneinfo`, `contextvars` and `datetime` are already there.
|
|
53
55
|
- 🕐 **A `DatePoint` is a `datetime`.** It drops into an ORM column, a serializer, a comparison,
|
|
54
56
|
an existing signature. Adopting this library changes no type you already have.
|
|
55
57
|
- 🧭 **Always timezone-aware.** There is no such thing here as an instant whose offset is
|
|
@@ -57,7 +59,8 @@ clock in production, a frozen one in a test. What comes back is a **`DatePoint`*
|
|
|
57
59
|
- ⏱️ **Frozen time costs nothing.** `clock.sleep(86400)` returns immediately and the clock is a
|
|
58
60
|
day later.
|
|
59
61
|
- 🔀 **Blocking or awaiting.** `sleep()` and `sleep_async()` on the same clock, so one contract
|
|
60
|
-
serves a worker and a web handler
|
|
62
|
+
serves a worker and a web handler — and the awaitable one waits through anyio, so asyncio and
|
|
63
|
+
trio both get a clock they can use.
|
|
61
64
|
- 🧩 **Protocol-based.** Every collaborator is a constructor argument, so a DI container can own
|
|
62
65
|
the graph — and a clock from another library satisfies `SupportsNow` as it is.
|
|
63
66
|
|
|
@@ -76,7 +79,7 @@ uv add "xtr-clock[tzdata]" # + a timezone database, where the system has n
|
|
|
76
79
|
|
|
77
80
|
| Extra | Brings | For |
|
|
78
81
|
| --- | --- | --- |
|
|
79
|
-
| *(none)* |
|
|
82
|
+
| *(none)* | `anyio` | The whole library |
|
|
80
83
|
| `tzdata` | `tzdata` | Windows, and slim containers that ship no zone database |
|
|
81
84
|
|
|
82
85
|
Requires Python 3.11+.
|
|
@@ -437,6 +440,9 @@ class TokenIssuer:
|
|
|
437
440
|
| --- | --- |
|
|
438
441
|
| `timezone` | The zone `Clock.now()` reports in. `None` follows the machine's own zone |
|
|
439
442
|
|
|
443
|
+
A zone this system cannot resolve is refused by `ClockConfig` itself, so a typo fails the build
|
|
444
|
+
with an `InvalidTimezoneError` naming it rather than the first reading taken at runtime.
|
|
445
|
+
|
|
440
446
|
A test freezes time by replacing the `Clock` service — the one both `ClockInterface` and
|
|
441
447
|
`Clock.get()` answer with once the kernel boots:
|
|
442
448
|
|
|
@@ -525,11 +531,8 @@ read-only copy, so send issues and pull requests to the monorepo.
|
|
|
525
531
|
|
|
526
532
|
```sh
|
|
527
533
|
uv sync --all-extras
|
|
528
|
-
uv run ruff check
|
|
529
|
-
uv run
|
|
530
|
-
uv run ty check
|
|
531
|
-
uv run basedpyright
|
|
532
|
-
uv run coverage run -m pytest && uv run coverage report
|
|
534
|
+
uv run ruff check && uv run ruff format --check && uv run basedpyright && uv run ty check && uv run pytest
|
|
535
|
+
uv run coverage run -m pytest && uv run coverage report # statements 100%; the total adds branches
|
|
533
536
|
```
|
|
534
537
|
|
|
535
538
|
Two type checkers on purpose — they disagree often enough to be worth both. Both run strict on
|
|
@@ -538,8 +541,10 @@ the tests too, with nothing suppressed.
|
|
|
538
541
|
The suite mirrors the source tree: `tests/unit/` holds a `test_<module>.py` for each module,
|
|
539
542
|
testing it alone; `tests/integration/` holds what needs a fresh interpreter — that the fixture
|
|
540
543
|
is invisible until a suite opts in, and that importing the library pulls in nothing but the
|
|
541
|
-
standard library
|
|
542
|
-
|
|
544
|
+
standard library and `anyio`. The awaitable waits run on asyncio and, when the checkout has it,
|
|
545
|
+
on trio. Statement coverage is 100% — the figure the badge shows — measured with `coverage run`
|
|
546
|
+
rather than `pytest --cov`, which starts too late to see a module's import. The report's own
|
|
547
|
+
total reads 99%, since it counts branches too and one of them stays partial.
|
|
543
548
|
|
|
544
549
|
## License
|
|
545
550
|
|
|
@@ -5,8 +5,8 @@
|
|
|
5
5
|
**A clock an application can be handed, instead of the one it is standing on.**
|
|
6
6
|
|
|
7
7
|
<img alt="python 3.11+" src="https://img.shields.io/badge/python-%E2%89%A5%203.11-3776AB?logo=python&logoColor=white">
|
|
8
|
-
<img alt="core dependencies:
|
|
9
|
-
<img alt="
|
|
8
|
+
<img alt="core dependencies: anyio" src="https://img.shields.io/badge/core%20deps-anyio-3FB950">
|
|
9
|
+
<img alt="statements 100%" src="https://img.shields.io/badge/statements-100%25-3FB950">
|
|
10
10
|
<img alt="typed" src="https://img.shields.io/badge/typed-ty%20%2B%20basedpyright-1f6feb">
|
|
11
11
|
<img alt="license MIT" src="https://img.shields.io/badge/license-MIT-blue">
|
|
12
12
|
|
|
@@ -24,7 +24,8 @@ Take a **clock** as a constructor argument and the choice becomes configuration:
|
|
|
24
24
|
clock in production, a frozen one in a test. What comes back is a **`DatePoint`** — a
|
|
25
25
|
`datetime`, so nothing downstream has to change.
|
|
26
26
|
|
|
27
|
-
- 🪶 **
|
|
27
|
+
- 🪶 **One dependency.** `anyio`, so an awaitable wait is not tied to one async backend.
|
|
28
|
+
`zoneinfo`, `contextvars` and `datetime` are already there.
|
|
28
29
|
- 🕐 **A `DatePoint` is a `datetime`.** It drops into an ORM column, a serializer, a comparison,
|
|
29
30
|
an existing signature. Adopting this library changes no type you already have.
|
|
30
31
|
- 🧭 **Always timezone-aware.** There is no such thing here as an instant whose offset is
|
|
@@ -32,7 +33,8 @@ clock in production, a frozen one in a test. What comes back is a **`DatePoint`*
|
|
|
32
33
|
- ⏱️ **Frozen time costs nothing.** `clock.sleep(86400)` returns immediately and the clock is a
|
|
33
34
|
day later.
|
|
34
35
|
- 🔀 **Blocking or awaiting.** `sleep()` and `sleep_async()` on the same clock, so one contract
|
|
35
|
-
serves a worker and a web handler
|
|
36
|
+
serves a worker and a web handler — and the awaitable one waits through anyio, so asyncio and
|
|
37
|
+
trio both get a clock they can use.
|
|
36
38
|
- 🧩 **Protocol-based.** Every collaborator is a constructor argument, so a DI container can own
|
|
37
39
|
the graph — and a clock from another library satisfies `SupportsNow` as it is.
|
|
38
40
|
|
|
@@ -51,7 +53,7 @@ uv add "xtr-clock[tzdata]" # + a timezone database, where the system has n
|
|
|
51
53
|
|
|
52
54
|
| Extra | Brings | For |
|
|
53
55
|
| --- | --- | --- |
|
|
54
|
-
| *(none)* |
|
|
56
|
+
| *(none)* | `anyio` | The whole library |
|
|
55
57
|
| `tzdata` | `tzdata` | Windows, and slim containers that ship no zone database |
|
|
56
58
|
|
|
57
59
|
Requires Python 3.11+.
|
|
@@ -412,6 +414,9 @@ class TokenIssuer:
|
|
|
412
414
|
| --- | --- |
|
|
413
415
|
| `timezone` | The zone `Clock.now()` reports in. `None` follows the machine's own zone |
|
|
414
416
|
|
|
417
|
+
A zone this system cannot resolve is refused by `ClockConfig` itself, so a typo fails the build
|
|
418
|
+
with an `InvalidTimezoneError` naming it rather than the first reading taken at runtime.
|
|
419
|
+
|
|
415
420
|
A test freezes time by replacing the `Clock` service — the one both `ClockInterface` and
|
|
416
421
|
`Clock.get()` answer with once the kernel boots:
|
|
417
422
|
|
|
@@ -500,11 +505,8 @@ read-only copy, so send issues and pull requests to the monorepo.
|
|
|
500
505
|
|
|
501
506
|
```sh
|
|
502
507
|
uv sync --all-extras
|
|
503
|
-
uv run ruff check
|
|
504
|
-
uv run
|
|
505
|
-
uv run ty check
|
|
506
|
-
uv run basedpyright
|
|
507
|
-
uv run coverage run -m pytest && uv run coverage report
|
|
508
|
+
uv run ruff check && uv run ruff format --check && uv run basedpyright && uv run ty check && uv run pytest
|
|
509
|
+
uv run coverage run -m pytest && uv run coverage report # statements 100%; the total adds branches
|
|
508
510
|
```
|
|
509
511
|
|
|
510
512
|
Two type checkers on purpose — they disagree often enough to be worth both. Both run strict on
|
|
@@ -513,8 +515,10 @@ the tests too, with nothing suppressed.
|
|
|
513
515
|
The suite mirrors the source tree: `tests/unit/` holds a `test_<module>.py` for each module,
|
|
514
516
|
testing it alone; `tests/integration/` holds what needs a fresh interpreter — that the fixture
|
|
515
517
|
is invisible until a suite opts in, and that importing the library pulls in nothing but the
|
|
516
|
-
standard library
|
|
517
|
-
|
|
518
|
+
standard library and `anyio`. The awaitable waits run on asyncio and, when the checkout has it,
|
|
519
|
+
on trio. Statement coverage is 100% — the figure the badge shows — measured with `coverage run`
|
|
520
|
+
rather than `pytest --cov`, which starts too late to see a module's import. The report's own
|
|
521
|
+
total reads 99%, since it counts branches too and one of them stays partial.
|
|
518
522
|
|
|
519
523
|
## License
|
|
520
524
|
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
[project]
|
|
2
2
|
name = "xtr-clock"
|
|
3
|
-
version = "
|
|
3
|
+
version = "4.0.2"
|
|
4
4
|
description = "Decouples an application from the system clock: an injectable clock, a timezone-aware instant, and a frozen clock for tests."
|
|
5
5
|
readme = "README.md"
|
|
6
6
|
requires-python = ">=3.11"
|
|
@@ -25,7 +25,7 @@ classifiers = [
|
|
|
25
25
|
"Topic :: Software Development :: Testing",
|
|
26
26
|
"Typing :: Typed",
|
|
27
27
|
]
|
|
28
|
-
dependencies = []
|
|
28
|
+
dependencies = ["anyio>=4.0"]
|
|
29
29
|
|
|
30
30
|
[[project.authors]]
|
|
31
31
|
name = "Xterr"
|
|
@@ -35,7 +35,7 @@ email = "me@xterr.dev"
|
|
|
35
35
|
tzdata = ["tzdata>=2024.1"]
|
|
36
36
|
di = [
|
|
37
37
|
"typing-extensions>=4.4",
|
|
38
|
-
"xtr-dependency-injection>=
|
|
38
|
+
"xtr-dependency-injection>=4.0,<5",
|
|
39
39
|
]
|
|
40
40
|
|
|
41
41
|
[project.entry-points."xtr_dependency_injection.bundles"]
|
|
@@ -50,9 +50,9 @@ dev = [
|
|
|
50
50
|
"ruff>=0.8",
|
|
51
51
|
"pytest>=8",
|
|
52
52
|
"pytest-cov>=5",
|
|
53
|
-
"
|
|
53
|
+
"trio>=0.27",
|
|
54
54
|
"ty>=0.0.83",
|
|
55
|
-
"xtr-dependency-injection>=
|
|
55
|
+
"xtr-dependency-injection>=4.0,<5",
|
|
56
56
|
]
|
|
57
57
|
|
|
58
58
|
[build-system]
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
[project]
|
|
2
2
|
name = "xtr-clock"
|
|
3
|
-
version = "
|
|
3
|
+
version = "4.0.2"
|
|
4
4
|
description = "Decouples an application from the system clock: an injectable clock, a timezone-aware instant, and a frozen clock for tests."
|
|
5
5
|
readme = "README.md"
|
|
6
6
|
requires-python = ">=3.11"
|
|
@@ -20,7 +20,9 @@ classifiers = [
|
|
|
20
20
|
"Topic :: Software Development :: Testing",
|
|
21
21
|
"Typing :: Typed",
|
|
22
22
|
]
|
|
23
|
-
dependencies = [
|
|
23
|
+
dependencies = [
|
|
24
|
+
"anyio>=4.0",
|
|
25
|
+
]
|
|
24
26
|
|
|
25
27
|
[project.optional-dependencies]
|
|
26
28
|
tzdata = [
|
|
@@ -28,7 +30,7 @@ tzdata = [
|
|
|
28
30
|
]
|
|
29
31
|
di = [
|
|
30
32
|
"typing-extensions>=4.4",
|
|
31
|
-
"xtr-dependency-injection>=
|
|
33
|
+
"xtr-dependency-injection>=4.0,<5",
|
|
32
34
|
]
|
|
33
35
|
|
|
34
36
|
# No `pytest11` entry point on purpose. The `clock` fixture is opted into with
|
|
@@ -51,9 +53,9 @@ dev = [
|
|
|
51
53
|
"ruff>=0.8",
|
|
52
54
|
"pytest>=8",
|
|
53
55
|
"pytest-cov>=5",
|
|
54
|
-
"
|
|
56
|
+
"trio>=0.27",
|
|
55
57
|
"ty>=0.0.83",
|
|
56
|
-
"xtr-dependency-injection>=
|
|
58
|
+
"xtr-dependency-injection>=4.0,<5",
|
|
57
59
|
]
|
|
58
60
|
|
|
59
61
|
[build-system]
|
|
@@ -40,7 +40,7 @@ Every clock answers the same four calls:
|
|
|
40
40
|
```python
|
|
41
41
|
clock.now() # DatePoint, always aware
|
|
42
42
|
clock.sleep(2.5) # MockClock returns at once and moves 2.5s forward
|
|
43
|
-
await clock.sleep_async(2.5) # same, without blocking
|
|
43
|
+
await clock.sleep_async(2.5) # same, without blocking; waits through anyio, so any backend works
|
|
44
44
|
clock.with_timezone("Europe/Paris") # a copy; the original is unchanged
|
|
45
45
|
```
|
|
46
46
|
|
|
@@ -134,6 +134,9 @@ the steps below a recipe can do; the others it prints for you to make.
|
|
|
134
134
|
return ClockConfig(timezone="UTC")
|
|
135
135
|
```
|
|
136
136
|
|
|
137
|
+
`ClockConfig` resolves the zone as it is built, so an unknown one raises
|
|
138
|
+
`InvalidTimezoneError` at build time rather than on the first reading.
|
|
139
|
+
|
|
137
140
|
4. **Use** — inject `ClockInterface`. The bundle also installs the same clock as the clock in
|
|
138
141
|
force, so `now()` and the injected clock agree.
|
|
139
142
|
5. **Test** — replace the `Clock` service:
|
|
@@ -4,6 +4,8 @@ from __future__ import annotations
|
|
|
4
4
|
|
|
5
5
|
from dataclasses import dataclass
|
|
6
6
|
|
|
7
|
+
from xtr_clock.timezone import resolve_timezone
|
|
8
|
+
|
|
7
9
|
__all__ = ["ClockConfig"]
|
|
8
10
|
|
|
9
11
|
|
|
@@ -17,3 +19,13 @@ class ClockConfig:
|
|
|
17
19
|
"""
|
|
18
20
|
|
|
19
21
|
timezone: str | None = None
|
|
22
|
+
|
|
23
|
+
def __post_init__(self) -> None:
|
|
24
|
+
"""Resolve the zone now, so a typo fails the build and not the first reading.
|
|
25
|
+
|
|
26
|
+
Raises:
|
|
27
|
+
InvalidTimezoneError: When ``timezone`` names no zone this
|
|
28
|
+
system knows.
|
|
29
|
+
"""
|
|
30
|
+
if self.timezone is not None:
|
|
31
|
+
_ = resolve_timezone(self.timezone)
|
|
@@ -24,7 +24,13 @@ def shift_calendar(moment: _DateT, /, *, months: int = 0, days: int = 0) -> _Dat
|
|
|
24
24
|
24 hours later. A wall clock the zone skipped over (the hour lost as the
|
|
25
25
|
clocks go forward) is read as the instant it names, shown on the hour
|
|
26
26
|
that replaced it, so the result always really existed.
|
|
27
|
+
|
|
28
|
+
Asked for no shift at all — a scheduler stepping a calendar by zero —
|
|
29
|
+
it hands back what it was given, rather than resolving an instant it
|
|
30
|
+
never moved.
|
|
27
31
|
"""
|
|
32
|
+
if not months and not days:
|
|
33
|
+
return moment
|
|
28
34
|
if months:
|
|
29
35
|
position = moment.month - 1 + months
|
|
30
36
|
year = moment.year + position // _MONTHS_PER_YEAR
|
|
@@ -64,7 +64,9 @@ class ClockInterface(SupportsNow, Protocol):
|
|
|
64
64
|
async def sleep_async(self, seconds: float) -> None:
|
|
65
65
|
"""Wait ``seconds`` without blocking the event loop.
|
|
66
66
|
|
|
67
|
-
A value of zero or less returns immediately.
|
|
67
|
+
A value of zero or less returns immediately. Implementations wait
|
|
68
|
+
through anyio, so a clock is usable on whichever async backend the
|
|
69
|
+
application runs on.
|
|
68
70
|
"""
|
|
69
71
|
...
|
|
70
72
|
|
|
@@ -2,10 +2,11 @@
|
|
|
2
2
|
|
|
3
3
|
from __future__ import annotations
|
|
4
4
|
|
|
5
|
-
import asyncio
|
|
6
5
|
from datetime import UTC, datetime, timedelta
|
|
7
6
|
from typing import TYPE_CHECKING, Self, final
|
|
8
7
|
|
|
8
|
+
from anyio.lowlevel import checkpoint
|
|
9
|
+
|
|
9
10
|
from .date_point import DatePoint
|
|
10
11
|
from .timezone import resolve_timezone
|
|
11
12
|
|
|
@@ -99,11 +100,14 @@ class MockClock:
|
|
|
99
100
|
Nothing is waited for, but control is handed back once so the tasks
|
|
100
101
|
that were waiting on this clock get their turn — which is the whole
|
|
101
102
|
behaviour a test of concurrent code is trying to observe.
|
|
103
|
+
|
|
104
|
+
The yield goes through anyio, so this works on whichever async
|
|
105
|
+
backend the test runs on rather than only on asyncio.
|
|
102
106
|
"""
|
|
103
107
|
if seconds > 0:
|
|
104
108
|
self._advance(seconds)
|
|
105
109
|
|
|
106
|
-
await
|
|
110
|
+
await checkpoint()
|
|
107
111
|
|
|
108
112
|
def modify(self, modifier: str) -> None:
|
|
109
113
|
"""Move to wherever ``modifier`` points, relative to right here.
|
|
@@ -2,12 +2,13 @@
|
|
|
2
2
|
|
|
3
3
|
from __future__ import annotations
|
|
4
4
|
|
|
5
|
-
import asyncio
|
|
6
5
|
import time
|
|
7
6
|
from copy import copy
|
|
8
7
|
from datetime import UTC, datetime, timedelta
|
|
9
8
|
from typing import TYPE_CHECKING, Final, Self, final
|
|
10
9
|
|
|
10
|
+
import anyio
|
|
11
|
+
|
|
11
12
|
from .date_point import DatePoint
|
|
12
13
|
from .timezone import local_timezone, resolve_timezone
|
|
13
14
|
|
|
@@ -62,8 +63,8 @@ class MonotonicClock:
|
|
|
62
63
|
|
|
63
64
|
def now(self) -> DatePoint:
|
|
64
65
|
"""Return the current instant, truncated to the microsecond."""
|
|
65
|
-
|
|
66
|
-
moment = _EPOCH + timedelta(microseconds=
|
|
66
|
+
since_epoch_ns = self._anchor_ns + time.monotonic_ns()
|
|
67
|
+
moment = _EPOCH + timedelta(microseconds=since_epoch_ns // _NANOSECONDS_PER_MICROSECOND)
|
|
67
68
|
|
|
68
69
|
return DatePoint.from_datetime(moment.astimezone(self._timezone))
|
|
69
70
|
|
|
@@ -73,8 +74,12 @@ class MonotonicClock:
|
|
|
73
74
|
time.sleep(seconds)
|
|
74
75
|
|
|
75
76
|
async def sleep_async(self, seconds: float) -> None:
|
|
76
|
-
"""Wait ``seconds`` without blocking the event loop.
|
|
77
|
-
|
|
77
|
+
"""Wait ``seconds`` without blocking the event loop.
|
|
78
|
+
|
|
79
|
+
The wait goes through anyio, so the clock works on whichever async
|
|
80
|
+
backend the application runs on rather than only on asyncio.
|
|
81
|
+
"""
|
|
82
|
+
await anyio.sleep(max(seconds, 0.0))
|
|
78
83
|
|
|
79
84
|
def with_timezone(self, timezone: str | tzinfo) -> Self:
|
|
80
85
|
"""Return the same clock reporting in ``timezone``.
|
|
@@ -2,11 +2,12 @@
|
|
|
2
2
|
|
|
3
3
|
from __future__ import annotations
|
|
4
4
|
|
|
5
|
-
import asyncio
|
|
6
5
|
import time
|
|
7
6
|
from datetime import datetime
|
|
8
7
|
from typing import TYPE_CHECKING, Self, final
|
|
9
8
|
|
|
9
|
+
import anyio
|
|
10
|
+
|
|
10
11
|
from .date_point import DatePoint
|
|
11
12
|
from .timezone import local_timezone, resolve_timezone
|
|
12
13
|
|
|
@@ -59,8 +60,12 @@ class SystemClock:
|
|
|
59
60
|
time.sleep(seconds)
|
|
60
61
|
|
|
61
62
|
async def sleep_async(self, seconds: float) -> None:
|
|
62
|
-
"""Wait ``seconds`` without blocking the event loop.
|
|
63
|
-
|
|
63
|
+
"""Wait ``seconds`` without blocking the event loop.
|
|
64
|
+
|
|
65
|
+
The wait goes through anyio, so the clock works on whichever async
|
|
66
|
+
backend the application runs on rather than only on asyncio.
|
|
67
|
+
"""
|
|
68
|
+
await anyio.sleep(max(seconds, 0.0))
|
|
64
69
|
|
|
65
70
|
def with_timezone(self, timezone: str | tzinfo) -> Self:
|
|
66
71
|
"""Return the same clock reporting in ``timezone``.
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|