infdate 0.2.0__py3-none-any.whl → 0.2.1__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.
- infdate/__init__.py +49 -7
- {infdate-0.2.0.dist-info → infdate-0.2.1.dist-info}/METADATA +21 -15
- infdate-0.2.1.dist-info/RECORD +6 -0
- infdate-0.2.0.dist-info/RECORD +0 -6
- {infdate-0.2.0.dist-info → infdate-0.2.1.dist-info}/WHEEL +0 -0
- {infdate-0.2.0.dist-info → infdate-0.2.1.dist-info}/licenses/LICENSE +0 -0
infdate/__init__.py
CHANGED
@@ -10,6 +10,11 @@ from math import inf
|
|
10
10
|
from typing import final, overload, Any, Final, TypeVar
|
11
11
|
|
12
12
|
|
13
|
+
# -----------------------------------------------------------------------------
|
14
|
+
# Public module constants:
|
15
|
+
# display definitions, format strings, upper and loer ordinal boundaries
|
16
|
+
# -----------------------------------------------------------------------------
|
17
|
+
|
13
18
|
INFINITE_DATE_DISPLAY: Final[str] = "<inf>"
|
14
19
|
NEGATIVE_INFINITE_DATE_DISPLAY: Final[str] = "<-inf>"
|
15
20
|
|
@@ -24,9 +29,19 @@ MIN_ORDINAL: Final[int] = date.min.toordinal()
|
|
24
29
|
MAX_ORDINAL: Final[int] = date.max.toordinal()
|
25
30
|
|
26
31
|
|
32
|
+
# -----------------------------------------------------------------------------
|
33
|
+
# Internal module constants:
|
34
|
+
# TypeVar for GenericDate
|
35
|
+
# -----------------------------------------------------------------------------
|
36
|
+
|
27
37
|
_GD = TypeVar("_GD", bound="GenericDate")
|
28
38
|
|
29
39
|
|
40
|
+
# -----------------------------------------------------------------------------
|
41
|
+
# Classes
|
42
|
+
# -----------------------------------------------------------------------------
|
43
|
+
|
44
|
+
|
30
45
|
class GenericDate:
|
31
46
|
"""Base Date object derived from an ordinal"""
|
32
47
|
|
@@ -204,12 +219,18 @@ class RealDate(GenericDate):
|
|
204
219
|
)
|
205
220
|
|
206
221
|
|
207
|
-
#
|
222
|
+
# -----------------------------------------------------------------------------
|
223
|
+
# Public module constants continued:
|
224
|
+
# absolute minimum and maximum dates
|
225
|
+
# -----------------------------------------------------------------------------
|
226
|
+
|
208
227
|
MIN: Final[GenericDate] = InfinityDate(past_bound=True)
|
209
228
|
MAX: Final[GenericDate] = InfinityDate(past_bound=False)
|
210
229
|
|
211
230
|
|
212
|
-
#
|
231
|
+
# -----------------------------------------------------------------------------
|
232
|
+
# Module-level factory functions
|
233
|
+
# -----------------------------------------------------------------------------
|
213
234
|
|
214
235
|
|
215
236
|
def from_datetime_object(source: date | datetime, /) -> GenericDate:
|
@@ -241,6 +262,18 @@ def from_native_type(
|
|
241
262
|
raise ValueError(f"Don’t know how to convert {source!r} into a date")
|
242
263
|
|
243
264
|
|
265
|
+
def fromtimestamp(timestamp: float) -> GenericDate:
|
266
|
+
"""Create an InfinityDate or RealDate instance from the provided timestamp"""
|
267
|
+
if timestamp == -inf:
|
268
|
+
return MIN
|
269
|
+
#
|
270
|
+
if timestamp == inf:
|
271
|
+
return MAX
|
272
|
+
#
|
273
|
+
stdlib_date_object = date.fromtimestamp(timestamp)
|
274
|
+
return from_datetime_object(stdlib_date_object)
|
275
|
+
|
276
|
+
|
244
277
|
def fromordinal(ordinal: float | int) -> GenericDate:
|
245
278
|
"""Create an InfinityDate or RealDate instance from the provided ordinal"""
|
246
279
|
if ordinal == -inf:
|
@@ -261,6 +294,20 @@ def fromordinal(ordinal: float | int) -> GenericDate:
|
|
261
294
|
return from_datetime_object(stdlib_date_object)
|
262
295
|
|
263
296
|
|
297
|
+
# -----------------------------------------------------------------------------
|
298
|
+
# Public module constants continued:
|
299
|
+
# minimum and maximum real dates
|
300
|
+
# -----------------------------------------------------------------------------
|
301
|
+
|
302
|
+
REAL_MIN: Final[GenericDate] = fromordinal(MIN_ORDINAL)
|
303
|
+
REAL_MAX: Final[GenericDate] = fromordinal(MAX_ORDINAL)
|
304
|
+
|
305
|
+
|
306
|
+
# -----------------------------------------------------------------------------
|
307
|
+
# Module-level factory functions continued
|
308
|
+
# -----------------------------------------------------------------------------
|
309
|
+
|
310
|
+
|
264
311
|
def fromisoformat(source: str, /) -> GenericDate:
|
265
312
|
"""Create an InfinityDate or RealDate instance from an iso format representation"""
|
266
313
|
lower_source_stripped = source.strip().lower()
|
@@ -281,8 +328,3 @@ def fromisocalendar(year: int, week: int, weekday: int) -> GenericDate:
|
|
281
328
|
def today() -> GenericDate:
|
282
329
|
"""Today as RealDate object"""
|
283
330
|
return from_datetime_object(date.today())
|
284
|
-
|
285
|
-
|
286
|
-
# Minimum and maximum real dates
|
287
|
-
# setattr(RealDate, "min", fromordinal(MIN_ORDINAL))
|
288
|
-
# setattr(RealDate, "max", fromordinal(MAX_ORDINAL))
|
@@ -1,6 +1,6 @@
|
|
1
1
|
Metadata-Version: 2.4
|
2
2
|
Name: infdate
|
3
|
-
Version: 0.2.
|
3
|
+
Version: 0.2.1
|
4
4
|
Summary: Date object wrapper supporting infinity
|
5
5
|
Project-URL: Homepage, https://gitlab.com/blackstream-x/infdate
|
6
6
|
Project-URL: CI, https://gitlab.com/blackstream-x/infdate/-/pipelines
|
@@ -28,38 +28,41 @@ _Python module for date calculations implementing a concept of infinity_
|
|
28
28
|
|
29
29
|
## Module description
|
30
30
|
|
31
|
-
|
31
|
+
### Classes overview
|
32
32
|
|
33
33
|
└── GenericDate
|
34
34
|
├── InfinityDate
|
35
35
|
└── RealDate
|
36
36
|
|
37
37
|
|
38
|
+
The base class **GenericDate** should not be instantiated but can be used as a type annotation.
|
38
39
|
|
39
40
|
**InfinityDate** can represent either positive or negative infinity.
|
40
41
|
The module-level constants **MIN** and **MAX** contain the two possible
|
41
42
|
**InfinityDate** instance variations.
|
42
43
|
|
43
44
|
**RealDate** instances represent real dates like the standard library’s
|
44
|
-
**datetime.date** class, with mostly equal or
|
45
|
+
**datetime.date** class, with mostly equal or similar semantics. The module -level constants **REAL_MIN** and **REAL_MAX** are the eqivalents of **datetime.date.min** and **datetime.date.max** as **RealDate** instances.
|
45
46
|
|
46
47
|
For any valid **RealDate** instance, the following is **True**:
|
47
48
|
|
48
49
|
``` python
|
49
|
-
infdate.MIN < infdate.
|
50
|
+
infdate.MIN < infdate.REAL_MIN <= real_date_instance <= infdate.REAL_MAX < infdate.MAX
|
50
51
|
```
|
51
52
|
|
53
|
+
### Module-level factory functions
|
54
|
+
|
55
|
+
|
52
56
|
The following factory methods from the **datetime.date** class
|
53
57
|
are provided as module-level functions:
|
54
58
|
|
59
|
+
* **fromtimestamp()** (also accepting **-math.inf** or **math.inf**)
|
55
60
|
* **fromordinal()** (also accepting **-math.inf** or **math.inf**)
|
56
61
|
* **fromisoformat()**
|
57
62
|
* **fromisocalendar()**
|
58
63
|
* **today()**
|
59
64
|
|
60
|
-
|
61
|
-
|
62
|
-
Two additional factory functions are provided in the module:
|
65
|
+
Two additional factory functions are provided:
|
63
66
|
|
64
67
|
* **from_datetime_object()** to create a **RealDate** instance from a
|
65
68
|
**datetime.date** or **datetime.datetime** instance
|
@@ -70,18 +73,20 @@ Two additional factory functions are provided in the module:
|
|
70
73
|
This can come handy when dealing with API representations of dates,
|
71
74
|
eg. in GitLab’s [Personal Access Tokens API].
|
72
75
|
|
73
|
-
|
76
|
+
|
77
|
+
### Differences between infdate classes and datetime.date
|
78
|
+
|
79
|
+
Some notable difference from the **datetime.date** class, mainly due to the design decision to express date differences in pure numbers (ie. **float** because **math.inf** also is a float):
|
74
80
|
|
75
81
|
* The **.toordinal()** method returns **float** instead of **int**
|
76
82
|
|
77
83
|
* The **resolution** attribute is **1.0** instead of **datetime.timedelta(days=1)**
|
78
84
|
but also represents exactly one day.
|
79
85
|
|
80
|
-
* Subtracting a date from an **InfinityDate** or **RealDate** always returns a float
|
81
|
-
(because **math.inf** is a float), not a **datetime.timedelta** instance.
|
86
|
+
* Subtracting a date from an **InfinityDate** or **RealDate** always returns a float instead of a **datetime.timedelta** instance.
|
82
87
|
|
83
88
|
* Likewise, you cannot add or subtract **datetime.timedelta** instances
|
84
|
-
from
|
89
|
+
from an **InfinityDate** or **RealDate**, only **float** or **int** (support for adding and subtracting datetime.timedelta instances might be added in the future).
|
85
90
|
|
86
91
|
|
87
92
|
## Example usage
|
@@ -116,10 +121,10 @@ inf
|
|
116
121
|
inf
|
117
122
|
```
|
118
123
|
|
119
|
-
|
120
|
-
|
121
|
-
so subtracting an **InfinityDate** or **RealDate**
|
122
|
-
still
|
124
|
+
**InfinityDate** and **RealDate** instances can be compared with each other, and also with **datetime.date** instances.
|
125
|
+
|
126
|
+
Subtracting instances from each other also works, but currently, `__rsub__` is not implemented yet, so although the other direction works, subtracting an **InfinityDate** or **RealDate** _from_ a **datetime.date**
|
127
|
+
currently still raises a **TypeError**):
|
123
128
|
|
124
129
|
>>> from datetime import date
|
125
130
|
>>> stdlib_today = date.today()
|
@@ -137,5 +142,6 @@ still gives a **TypeError**):
|
|
137
142
|
TypeError: unsupported operand type(s) for -: 'datetime.date' and 'RealDate'
|
138
143
|
|
139
144
|
|
145
|
+
|
140
146
|
* * *
|
141
147
|
[Personal Access Tokens API]: https://docs.gitlab.com/api/personal_access_tokens/
|
@@ -0,0 +1,6 @@
|
|
1
|
+
infdate/__init__.py,sha256=n3WfJnHma_FGelLrkOjBt4gj49P-_rlsqc0FTLmUxCI,11195
|
2
|
+
infdate/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
3
|
+
infdate-0.2.1.dist-info/METADATA,sha256=V85LBfMkyB66rZJI4ceghR0KL-x0b2RQoOo-0_Sr77s,5205
|
4
|
+
infdate-0.2.1.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
|
5
|
+
infdate-0.2.1.dist-info/licenses/LICENSE,sha256=867pxriiObx28vCU1JsRtu3H9kUKyl54e0-xl1IIv3Y,913
|
6
|
+
infdate-0.2.1.dist-info/RECORD,,
|
infdate-0.2.0.dist-info/RECORD
DELETED
@@ -1,6 +0,0 @@
|
|
1
|
-
infdate/__init__.py,sha256=wBa4EXdkOXSSsX3QpBwx8jIpz3s6UETUoJnTlnqRNhc,9422
|
2
|
-
infdate/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
3
|
-
infdate-0.2.0.dist-info/METADATA,sha256=_g58qFMeECVrjNn0Z_t3Bggox3GHYXqY9NGVwA75ld4,4573
|
4
|
-
infdate-0.2.0.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
|
5
|
-
infdate-0.2.0.dist-info/licenses/LICENSE,sha256=867pxriiObx28vCU1JsRtu3H9kUKyl54e0-xl1IIv3Y,913
|
6
|
-
infdate-0.2.0.dist-info/RECORD,,
|
File without changes
|
File without changes
|