clock-pattern 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.
- clock_pattern/__init__.py +1 -0
- clock_pattern/clocks/__init__.py +3 -0
- clock_pattern/clocks/system_clock.py +120 -0
- clock_pattern/models/__init__.py +3 -0
- clock_pattern/models/clock.py +59 -0
- clock_pattern/py.typed +0 -0
- clock_pattern-0.1.0.dist-info/METADATA +95 -0
- clock_pattern-0.1.0.dist-info/RECORD +10 -0
- clock_pattern-0.1.0.dist-info/WHEEL +4 -0
- clock_pattern-0.1.0.dist-info/licenses/LICENSE.md +21 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
__version__ = '0.1.0'
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
"""
|
|
2
|
+
SystemClock module.
|
|
3
|
+
"""
|
|
4
|
+
|
|
5
|
+
from sys import version_info
|
|
6
|
+
|
|
7
|
+
if version_info >= (3, 12):
|
|
8
|
+
from typing import override # pragma: no cover
|
|
9
|
+
else:
|
|
10
|
+
from typing_extensions import override # pragma: no cover
|
|
11
|
+
|
|
12
|
+
from datetime import UTC, date, datetime, tzinfo
|
|
13
|
+
from zoneinfo import ZoneInfo
|
|
14
|
+
|
|
15
|
+
from value_object_pattern.usables.dates import StringTimezoneValueObject, TimezoneValueObject
|
|
16
|
+
|
|
17
|
+
from clock_pattern.models.clock import Clock
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
class SystemClock(Clock):
|
|
21
|
+
"""
|
|
22
|
+
SystemClock class is responsible of retrieving the current date/datetime with the provided timezone.
|
|
23
|
+
|
|
24
|
+
Example:
|
|
25
|
+
```python
|
|
26
|
+
from clock_pattern.clocks import SystemClock
|
|
27
|
+
|
|
28
|
+
clock = SystemClock()
|
|
29
|
+
print(clock.now())
|
|
30
|
+
# >>> 2025-06-16 13:57:26.210964+00:00
|
|
31
|
+
```
|
|
32
|
+
"""
|
|
33
|
+
|
|
34
|
+
_timezone: tzinfo
|
|
35
|
+
|
|
36
|
+
def __init__(self, *, timezone: str | tzinfo = UTC) -> None:
|
|
37
|
+
"""
|
|
38
|
+
SystemClock constructor is responsible to store which `timezone` the user wants to use when retrieving the
|
|
39
|
+
current date/datetime.
|
|
40
|
+
|
|
41
|
+
Args:
|
|
42
|
+
timezone: (str | tzinfo, optional): Timezone of the date/datetime to retrieve. Default to UTC.
|
|
43
|
+
|
|
44
|
+
Raises:
|
|
45
|
+
TypeError: If `timezone` is not of type tzinfo.
|
|
46
|
+
TypeError: If `timezone` is not of type string.
|
|
47
|
+
ValueError: If `timezone` is not a valid timezone.
|
|
48
|
+
|
|
49
|
+
Example:
|
|
50
|
+
```python
|
|
51
|
+
from clock_pattern.clocks import SystemClock
|
|
52
|
+
|
|
53
|
+
clock = SystemClock()
|
|
54
|
+
print(clock.now())
|
|
55
|
+
# >>> 2025-06-16 13:57:26.210964+00:00
|
|
56
|
+
```
|
|
57
|
+
"""
|
|
58
|
+
if isinstance(timezone, tzinfo):
|
|
59
|
+
timezone = str(TimezoneValueObject(value=timezone, title='SystemClock', parameter='timezone'))
|
|
60
|
+
|
|
61
|
+
StringTimezoneValueObject(value=timezone, title='SystemClock', parameter='timezone')
|
|
62
|
+
|
|
63
|
+
self._timezone = ZoneInfo(timezone)
|
|
64
|
+
|
|
65
|
+
@override
|
|
66
|
+
def now(self) -> datetime:
|
|
67
|
+
"""
|
|
68
|
+
Retrieve the current datetime (now).
|
|
69
|
+
|
|
70
|
+
Returns:
|
|
71
|
+
datetime: The current datetime.
|
|
72
|
+
|
|
73
|
+
Example:
|
|
74
|
+
```python
|
|
75
|
+
from clock_pattern.clocks import SystemClock
|
|
76
|
+
|
|
77
|
+
clock = SystemClock()
|
|
78
|
+
print(clock.now())
|
|
79
|
+
# >>> 2025-06-16 13:57:26.210964+00:00
|
|
80
|
+
```
|
|
81
|
+
"""
|
|
82
|
+
return datetime.now(tz=self._timezone)
|
|
83
|
+
|
|
84
|
+
@override
|
|
85
|
+
def today(self) -> date:
|
|
86
|
+
"""
|
|
87
|
+
Retrieve the current date (today).
|
|
88
|
+
|
|
89
|
+
Returns:
|
|
90
|
+
date: The current date.
|
|
91
|
+
|
|
92
|
+
Example:
|
|
93
|
+
```python
|
|
94
|
+
from clock_pattern.clocks import SystemClock
|
|
95
|
+
|
|
96
|
+
clock = SystemClock()
|
|
97
|
+
print(clock.now())
|
|
98
|
+
# >>> 2025-06-16
|
|
99
|
+
```
|
|
100
|
+
"""
|
|
101
|
+
return datetime.now(tz=self._timezone).date()
|
|
102
|
+
|
|
103
|
+
@property
|
|
104
|
+
def timezone(self) -> tzinfo:
|
|
105
|
+
"""
|
|
106
|
+
Retrieve the timezone of the clock.
|
|
107
|
+
|
|
108
|
+
Returns:
|
|
109
|
+
tzinfo: The timezone of the clock.
|
|
110
|
+
|
|
111
|
+
Example:
|
|
112
|
+
```python
|
|
113
|
+
from clock_pattern.clocks import SystemClock
|
|
114
|
+
|
|
115
|
+
clock = SystemClock()
|
|
116
|
+
print(clock.timezone)
|
|
117
|
+
# >>> UTC
|
|
118
|
+
```
|
|
119
|
+
"""
|
|
120
|
+
return self._timezone
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Clock module.
|
|
3
|
+
"""
|
|
4
|
+
|
|
5
|
+
from abc import ABC, abstractmethod
|
|
6
|
+
from datetime import date, datetime
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class Clock(ABC):
|
|
10
|
+
"""
|
|
11
|
+
Clock class.
|
|
12
|
+
|
|
13
|
+
***This class is abstract and should not be instantiated directly***.
|
|
14
|
+
|
|
15
|
+
Example:
|
|
16
|
+
```python
|
|
17
|
+
from clock_pattern.clocks import SystemClock
|
|
18
|
+
|
|
19
|
+
clock = SystemClock()
|
|
20
|
+
print(clock.now())
|
|
21
|
+
# >>> 2025-06-16 13:57:26.210964+00:00
|
|
22
|
+
```
|
|
23
|
+
"""
|
|
24
|
+
|
|
25
|
+
@abstractmethod
|
|
26
|
+
def now(self) -> datetime:
|
|
27
|
+
"""
|
|
28
|
+
Retrieve the current datetime (now).
|
|
29
|
+
|
|
30
|
+
Returns:
|
|
31
|
+
datetime: The current datetime.
|
|
32
|
+
|
|
33
|
+
Example:
|
|
34
|
+
```python
|
|
35
|
+
from clock_pattern.clocks import SystemClock
|
|
36
|
+
|
|
37
|
+
clock = SystemClock()
|
|
38
|
+
print(clock.now())
|
|
39
|
+
# >>> 2025-06-16 13:57:26.210964+00:00
|
|
40
|
+
```
|
|
41
|
+
"""
|
|
42
|
+
|
|
43
|
+
@abstractmethod
|
|
44
|
+
def today(self) -> date:
|
|
45
|
+
"""
|
|
46
|
+
Retrieve the current date (today).
|
|
47
|
+
|
|
48
|
+
Returns:
|
|
49
|
+
date: The current date.
|
|
50
|
+
|
|
51
|
+
Example:
|
|
52
|
+
```python
|
|
53
|
+
from clock_pattern.clocks import SystemClock
|
|
54
|
+
|
|
55
|
+
clock = SystemClock()
|
|
56
|
+
print(clock.today())
|
|
57
|
+
# >>> 2025-06-16
|
|
58
|
+
```
|
|
59
|
+
"""
|
clock_pattern/py.typed
ADDED
|
File without changes
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: clock-pattern
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: The Clock Pattern is a Python package that turns time into an injectable dependency.
|
|
5
|
+
Project-URL: Homepage, https://github.com/adriamontoto/clock-pattern
|
|
6
|
+
Project-URL: Repository, https://github.com/adriamontoto/clock-pattern
|
|
7
|
+
Project-URL: Issues, https://github.com/adriamontoto/clock-pattern/issues
|
|
8
|
+
Author: Adria Montoto
|
|
9
|
+
License-Expression: MIT
|
|
10
|
+
License-File: LICENSE.md
|
|
11
|
+
Keywords: clock,dependency-injection,development,domain-driven-design,pattern,python,utilities
|
|
12
|
+
Classifier: Intended Audience :: Developers
|
|
13
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
14
|
+
Classifier: Operating System :: OS Independent
|
|
15
|
+
Classifier: Programming Language :: Python
|
|
16
|
+
Classifier: Programming Language :: Python :: 3
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
20
|
+
Classifier: Typing :: Typed
|
|
21
|
+
Requires-Python: >=3.11
|
|
22
|
+
Requires-Dist: value-object-pattern>=0.5.0
|
|
23
|
+
Description-Content-Type: text/markdown
|
|
24
|
+
|
|
25
|
+
<a name="readme-top"></a>
|
|
26
|
+
|
|
27
|
+
# ๐ฐ๏ธ Clock Pattern
|
|
28
|
+
|
|
29
|
+
<p align="center">
|
|
30
|
+
<a href="https://github.com/adriamontoto/clock-pattern/actions/workflows/ci.yaml?event=push&branch=master" target="_blank">
|
|
31
|
+
<img src="https://github.com/adriamontoto/clock-pattern/actions/workflows/ci.yaml/badge.svg?event=push&branch=master" alt="CI Pipeline">
|
|
32
|
+
</a>
|
|
33
|
+
<a href="https://coverage-badge.samuelcolvin.workers.dev/redirect/adriamontoto/clock-pattern" target="_blank">
|
|
34
|
+
<img src="https://coverage-badge.samuelcolvin.workers.dev/adriamontoto/clock-pattern.svg" alt="Coverage Pipeline">
|
|
35
|
+
</a>
|
|
36
|
+
<a href="https://pypi.org/project/clock-pattern" target="_blank">
|
|
37
|
+
<img src="https://img.shields.io/pypi/v/clock-pattern?color=%2334D058&label=pypi%20package" alt="Package Version">
|
|
38
|
+
</a>
|
|
39
|
+
<a href="https://pypi.org/project/clock-pattern/" target="_blank">
|
|
40
|
+
<img src="https://img.shields.io/pypi/pyversions/clock-pattern.svg?color=%2334D058" alt="Supported Python Versions">
|
|
41
|
+
</a>
|
|
42
|
+
</p>
|
|
43
|
+
|
|
44
|
+
The **Clock Pattern** is a Python ๐ package that turns time into an injectable dependency ๐งฉ. By replacing ad-hoc datetime.now() calls with a swappable Clock interface ๐ฐ๏ธ you unlock deterministic tests ๐งช, decouple business logic from the OS clock, and gain the freedom to swap in high-precision or logical clocks without touching domain code.
|
|
45
|
+
<br><br>
|
|
46
|
+
|
|
47
|
+
## Table of Contents
|
|
48
|
+
|
|
49
|
+
- [๐ฅ Installation](#installation)
|
|
50
|
+
- [๐ค Contributing](#contributing)
|
|
51
|
+
- [๐ License](#license)
|
|
52
|
+
|
|
53
|
+
<p align="right">
|
|
54
|
+
<a href="#readme-top">๐ผ Back to top</a>
|
|
55
|
+
</p><br><br>
|
|
56
|
+
|
|
57
|
+
<a name="installation"></a>
|
|
58
|
+
|
|
59
|
+
## ๐ฅ Installation
|
|
60
|
+
|
|
61
|
+
You can install **Clock Pattern** using `pip`:
|
|
62
|
+
|
|
63
|
+
```bash
|
|
64
|
+
pip install clock-pattern
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
<p align="right">
|
|
68
|
+
<a href="#readme-top">๐ผ Back to top</a>
|
|
69
|
+
</p><br><br>
|
|
70
|
+
|
|
71
|
+
<a name="contributing"></a>
|
|
72
|
+
|
|
73
|
+
## ๐ค Contributing
|
|
74
|
+
|
|
75
|
+
We love community help! Before you open an issue or pull request, please read:
|
|
76
|
+
|
|
77
|
+
- [`๐ค How to Contribute`](https://github.com/adriamontoto/clock-pattern/blob/master/.github/CONTRIBUTING.md)
|
|
78
|
+
- [`๐งญ Code of Conduct`](https://github.com/adriamontoto/clock-pattern/blob/master/.github/CODE_OF_CONDUCT.md)
|
|
79
|
+
- [`๐ Security Policy`](https://github.com/adriamontoto/clock-pattern/blob/master/.github/SECURITY.md)
|
|
80
|
+
|
|
81
|
+
_Thank you for helping make **๐ฐ๏ธ Clock Pattern** package awesome! ๐_
|
|
82
|
+
|
|
83
|
+
<p align="right">
|
|
84
|
+
<a href="#readme-top">๐ผ Back to top</a>
|
|
85
|
+
</p><br><br>
|
|
86
|
+
|
|
87
|
+
<a name="license"></a>
|
|
88
|
+
|
|
89
|
+
## ๐ License
|
|
90
|
+
|
|
91
|
+
This project is licensed under the terms of the [`MIT license`](https://github.com/adriamontoto/clock-pattern/blob/master/LICENSE.md).
|
|
92
|
+
|
|
93
|
+
<p align="right">
|
|
94
|
+
<a href="#readme-top">๐ผ Back to top</a>
|
|
95
|
+
</p>
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
clock_pattern/__init__.py,sha256=IMjkMO3twhQzluVTo8Z6rE7Eg-9U79_LGKMcsWLKBkY,22
|
|
2
|
+
clock_pattern/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
3
|
+
clock_pattern/clocks/__init__.py,sha256=5D4k3fSqGGN_cvIXHs4lqL7pMItbeIMDKQ930nz9ztk,66
|
|
4
|
+
clock_pattern/clocks/system_clock.py,sha256=JmUdwVu9gAfH84-JY-suQUClvR8JMuujF84lccJ-BP0,3055
|
|
5
|
+
clock_pattern/models/__init__.py,sha256=Q3O6MR-aEh4SeDQY4Tgsy24i-Ne4ZSQL4HY5rIhOCe8,47
|
|
6
|
+
clock_pattern/models/clock.py,sha256=RUb2TNy4BAOne1GdAZ4DlTRhfGzjco6E9NY-u-O654o,1147
|
|
7
|
+
clock_pattern-0.1.0.dist-info/METADATA,sha256=ww1foUF6BP19i7W1jfrZMjZY_cJv_Z-92Rz5JwE068A,3723
|
|
8
|
+
clock_pattern-0.1.0.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
|
|
9
|
+
clock_pattern-0.1.0.dist-info/licenses/LICENSE.md,sha256=k3Sxbijlj46RildqRYKoeS0MNBAMcIOMdlixIyMxZ74,1071
|
|
10
|
+
clock_pattern-0.1.0.dist-info/RECORD,,
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) (2025) adriamontoto
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|