infdate 0.1.0__py3-none-any.whl → 0.2.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.
- infdate/__init__.py +226 -220
- infdate-0.2.0.dist-info/METADATA +141 -0
- infdate-0.2.0.dist-info/RECORD +6 -0
- infdate/__main__.py +0 -8
- infdate-0.1.0.dist-info/METADATA +0 -42
- infdate-0.1.0.dist-info/RECORD +0 -7
- {infdate-0.1.0.dist-info → infdate-0.2.0.dist-info}/WHEEL +0 -0
- {infdate-0.1.0.dist-info → infdate-0.2.0.dist-info}/licenses/LICENSE +0 -0
infdate/__init__.py
CHANGED
@@ -5,278 +5,284 @@ infdate: a wrapper around standard library’s datetime.date objects,
|
|
5
5
|
capable of representing positive and negative infinity
|
6
6
|
"""
|
7
7
|
|
8
|
-
import datetime
|
9
|
-
import
|
10
|
-
|
8
|
+
from datetime import date, datetime
|
9
|
+
from math import inf
|
11
10
|
from typing import final, overload, Any, Final, TypeVar
|
12
11
|
|
13
|
-
INFINITY: Final = math.inf
|
14
|
-
NEGATIVE_INFINITY: Final = -math.inf
|
15
|
-
|
16
|
-
INFINITE_DATE_DISPLAY: Final = "<inf>"
|
17
|
-
NEGATIVE_INFINITE_DATE_DISPLAY: Final = "<-inf>"
|
18
12
|
|
19
|
-
|
20
|
-
|
13
|
+
INFINITE_DATE_DISPLAY: Final[str] = "<inf>"
|
14
|
+
NEGATIVE_INFINITE_DATE_DISPLAY: Final[str] = "<-inf>"
|
21
15
|
|
22
|
-
|
16
|
+
INFINITY_SYMBOL: Final[str] = "∝"
|
17
|
+
UP_TO_SYMBOL: Final[str] = "⤒"
|
18
|
+
FROM_ON_SYMBOL: Final[str] = "↥"
|
23
19
|
|
20
|
+
ISO_DATE_FORMAT: Final[str] = "%Y-%m-%d"
|
21
|
+
ISO_DATETIME_FORMAT_UTC: Final[str] = f"{ISO_DATE_FORMAT}T%H:%M:%S.%fZ"
|
24
22
|
|
25
|
-
|
26
|
-
|
23
|
+
MIN_ORDINAL: Final[int] = date.min.toordinal()
|
24
|
+
MAX_ORDINAL: Final[int] = date.max.toordinal()
|
27
25
|
|
28
|
-
@property
|
29
|
-
def min(cls: type[D], /) -> D: # type: ignore[misc]
|
30
|
-
"""Minimum possible Date"""
|
31
|
-
return cls(NEGATIVE_INFINITY)
|
32
26
|
|
33
|
-
|
34
|
-
def max(cls: type[D], /) -> D: # type: ignore[misc]
|
35
|
-
"""Maximum possible Date"""
|
36
|
-
return cls(INFINITY)
|
27
|
+
_GD = TypeVar("_GD", bound="GenericDate")
|
37
28
|
|
38
29
|
|
39
|
-
class
|
40
|
-
"""Date object
|
30
|
+
class GenericDate:
|
31
|
+
"""Base Date object derived from an ordinal"""
|
41
32
|
|
42
|
-
|
33
|
+
# pylint: disable=invalid-name
|
34
|
+
resolution: Final = 1
|
35
|
+
# pylint: enable=invalid-name
|
43
36
|
|
44
|
-
|
45
|
-
def __init__(self: D, year_or_strange_number: float, /) -> None: ...
|
46
|
-
@overload
|
47
|
-
def __init__(
|
48
|
-
self: D, year_or_strange_number: int, month: int, day: int, /
|
49
|
-
) -> None: ...
|
50
|
-
@final
|
51
|
-
def __init__(
|
52
|
-
self: D, year_or_strange_number: int | float, /, month: int = 0, day: int = 0
|
53
|
-
) -> None:
|
37
|
+
def __init__(self, ordinal: float, /) -> None:
|
54
38
|
"""Create a date-like object"""
|
55
|
-
|
56
|
-
self.__wrapped_date_obj: datetime.date | None = datetime.date(
|
57
|
-
int(year_or_strange_number), month, day
|
58
|
-
)
|
59
|
-
self.__ordinal: float | int = self.__wrapped_date_obj.toordinal()
|
60
|
-
elif math.isnan(year_or_strange_number):
|
61
|
-
raise ValueError("Cannot instantiate from NaN")
|
62
|
-
elif year_or_strange_number in (
|
63
|
-
INFINITY,
|
64
|
-
NEGATIVE_INFINITY,
|
65
|
-
):
|
66
|
-
self.__ordinal = year_or_strange_number
|
67
|
-
self.__wrapped_date_obj = None
|
68
|
-
else:
|
69
|
-
raise ValueError("Cannot instantiate from a regular deterministic float")
|
70
|
-
#
|
39
|
+
self.__ordinal = ordinal
|
71
40
|
|
72
|
-
def toordinal(self:
|
73
|
-
"""to ordinal (almost like
|
41
|
+
def toordinal(self: _GD) -> float:
|
42
|
+
"""to ordinal (almost like date.toordinal())"""
|
74
43
|
return self.__ordinal
|
75
44
|
|
76
|
-
def
|
77
|
-
"""
|
78
|
-
|
79
|
-
return self.__wrapped_date_obj
|
80
|
-
#
|
81
|
-
raise ValueError("Non-deterministic date")
|
82
|
-
|
83
|
-
@property
|
84
|
-
def year(self: D) -> int:
|
85
|
-
"""shortcut: year"""
|
86
|
-
return self.get_date_object().year
|
87
|
-
|
88
|
-
@property
|
89
|
-
def month(self: D) -> int:
|
90
|
-
"""shortcut: month"""
|
91
|
-
return self.get_date_object().month
|
45
|
+
def __lt__(self: _GD, other: _GD, /) -> bool:
|
46
|
+
"""Rich comparison: less"""
|
47
|
+
return self.__ordinal < other.toordinal()
|
92
48
|
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
return self.get_date_object().day
|
49
|
+
def __le__(self: _GD, other: _GD, /) -> bool:
|
50
|
+
"""Rich comparison: less or equal"""
|
51
|
+
return self < other or self == other
|
97
52
|
|
98
|
-
def
|
99
|
-
"""
|
100
|
-
|
101
|
-
return self.factory(
|
102
|
-
internal_object.replace(
|
103
|
-
year=year or internal_object.year,
|
104
|
-
month=month or internal_object.month,
|
105
|
-
day=day or internal_object.day,
|
106
|
-
)
|
107
|
-
)
|
53
|
+
def __gt__(self: _GD, other: _GD, /) -> bool:
|
54
|
+
"""Rich comparison: greater"""
|
55
|
+
return self.__ordinal > other.toordinal()
|
108
56
|
|
109
|
-
def
|
110
|
-
"""
|
111
|
-
return self
|
57
|
+
def __ge__(self: _GD, other: _GD, /) -> bool:
|
58
|
+
"""Rich comparison: greater or equal"""
|
59
|
+
return self > other or self == other
|
112
60
|
|
113
|
-
def
|
114
|
-
"""
|
115
|
-
|
116
|
-
date_object = self.get_date_object()
|
117
|
-
except ValueError as error:
|
118
|
-
if self.__ordinal == INFINITY:
|
119
|
-
return INFINITE_DATE_DISPLAY
|
120
|
-
#
|
121
|
-
if self.__ordinal == NEGATIVE_INFINITY:
|
122
|
-
return NEGATIVE_INFINITE_DATE_DISPLAY
|
123
|
-
#
|
124
|
-
raise error from error
|
125
|
-
#
|
126
|
-
return date_object.strftime(fmt or ISO_DATE_FORMAT)
|
61
|
+
def __eq__(self: _GD, other, /) -> bool:
|
62
|
+
"""Rich comparison: equals"""
|
63
|
+
return self.__ordinal == other.toordinal()
|
127
64
|
|
128
|
-
|
65
|
+
def __ne__(self: _GD, other, /) -> bool:
|
66
|
+
"""Rich comparison: does not equal"""
|
67
|
+
return self.__ordinal != other.toordinal()
|
129
68
|
|
130
|
-
def __bool__(self:
|
131
|
-
"""True if a real date is wrapped"""
|
132
|
-
return
|
69
|
+
def __bool__(self: _GD, /) -> bool:
|
70
|
+
"""True only if a real date is wrapped"""
|
71
|
+
return False
|
133
72
|
|
134
|
-
def __hash__(self:
|
73
|
+
def __hash__(self: _GD, /) -> int:
|
135
74
|
"""hash value"""
|
136
75
|
return hash(f"date with ordinal {self.__ordinal}")
|
137
76
|
|
138
|
-
def
|
77
|
+
def _add_days(self: _GD, delta: int | float, /):
|
139
78
|
"""Add other, respecting maybe-nondeterministic values"""
|
79
|
+
# Check for infinity in either self or delta,
|
80
|
+
# and return a matching InfinityDate if found.
|
81
|
+
# Re-use existing objects if possible.
|
140
82
|
for observed_item in (delta, self.__ordinal):
|
141
|
-
for infinity_form in (
|
83
|
+
for infinity_form in (inf, -inf):
|
142
84
|
if observed_item == infinity_form:
|
143
|
-
|
85
|
+
if observed_item == self.__ordinal:
|
86
|
+
return self
|
87
|
+
#
|
88
|
+
return fromordinal(observed_item)
|
144
89
|
#
|
145
90
|
#
|
146
91
|
#
|
147
|
-
|
92
|
+
# +/- 0 corner case
|
93
|
+
if not delta:
|
94
|
+
return self
|
95
|
+
#
|
96
|
+
# Return a RealDate instance if possible
|
97
|
+
return fromordinal(self.__ordinal + delta)
|
98
|
+
|
99
|
+
def __add__(self: _GD, delta: int | float, /) -> _GD:
|
100
|
+
"""gd_instance1 + number capability"""
|
101
|
+
return self._add_days(delta)
|
148
102
|
|
149
103
|
@overload
|
150
|
-
def __sub__(self:
|
104
|
+
def __sub__(self: _GD, other: int | float, /) -> _GD: ...
|
151
105
|
@overload
|
152
|
-
def __sub__(self:
|
106
|
+
def __sub__(self: _GD, other: _GD, /) -> int | float: ...
|
153
107
|
@final
|
154
|
-
def __sub__(self:
|
108
|
+
def __sub__(self: _GD, other: _GD | int | float, /) -> _GD | int | float:
|
155
109
|
"""subtract other, respecting possibly nondeterministic values"""
|
156
110
|
if isinstance(other, (int, float)):
|
157
|
-
return self
|
111
|
+
return self._add_days(-other)
|
158
112
|
#
|
159
113
|
return self.__ordinal - other.toordinal()
|
160
114
|
|
161
|
-
def
|
162
|
-
"""
|
163
|
-
return self.
|
115
|
+
def __repr__(self: _GD, /) -> str:
|
116
|
+
"""String representation of the object"""
|
117
|
+
return f"{self.__class__.__name__}({repr(self.__ordinal)})"
|
164
118
|
|
165
|
-
def
|
166
|
-
"""
|
167
|
-
return self
|
119
|
+
def __str__(self: _GD, /) -> str:
|
120
|
+
"""String display of the object"""
|
121
|
+
return self.isoformat()
|
168
122
|
|
169
|
-
def
|
170
|
-
"""
|
171
|
-
return self.
|
123
|
+
def isoformat(self: _GD, /) -> str:
|
124
|
+
"""Date representation in ISO format"""
|
125
|
+
return self.strftime(ISO_DATE_FORMAT)
|
172
126
|
|
173
|
-
def
|
174
|
-
"""
|
175
|
-
|
127
|
+
def strftime(self: _GD, fmt: str, /) -> str:
|
128
|
+
"""String representation of the date"""
|
129
|
+
raise NotImplementedError
|
176
130
|
|
177
|
-
def
|
178
|
-
"""
|
179
|
-
|
131
|
+
def replace(self: _GD, /, year: int = 0, month: int = 0, day: int = 0) -> _GD:
|
132
|
+
"""Return a copy with year, month, and/or date replaced"""
|
133
|
+
raise NotImplementedError
|
180
134
|
|
181
|
-
def __ne__(self: D, other, /) -> bool:
|
182
|
-
"""Rich comparison: does not equal"""
|
183
|
-
return self.__ordinal != other.toordinal()
|
184
135
|
|
185
|
-
|
136
|
+
class InfinityDate(GenericDate):
|
137
|
+
"""Infinity Date object"""
|
138
|
+
|
139
|
+
def __init__(self, /, *, past_bound: bool = False) -> None:
|
140
|
+
"""Store -inf or inf"""
|
141
|
+
ordinal = -inf if past_bound else inf
|
142
|
+
super().__init__(ordinal)
|
143
|
+
|
144
|
+
def __repr__(self, /) -> str:
|
186
145
|
"""String representation of the object"""
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
146
|
+
return f"{self.__class__.__name__}(past_bound={self.toordinal() == -inf})"
|
147
|
+
|
148
|
+
def strftime(self, fmt: str, /) -> str:
|
149
|
+
"""String representation of the date"""
|
150
|
+
if self.toordinal() == inf:
|
151
|
+
return INFINITE_DATE_DISPLAY
|
191
152
|
#
|
153
|
+
return NEGATIVE_INFINITE_DATE_DISPLAY
|
154
|
+
|
155
|
+
__format__ = strftime
|
192
156
|
|
193
|
-
def
|
157
|
+
def replace(self, /, year: int = 0, month: int = 0, day: int = 0):
|
158
|
+
"""Not supported in this class"""
|
159
|
+
raise TypeError(
|
160
|
+
f"{self.__class__.__name__} instances do not support .replace()"
|
161
|
+
)
|
162
|
+
|
163
|
+
|
164
|
+
# pylint: disable=too-many-instance-attributes
|
165
|
+
class RealDate(GenericDate):
|
166
|
+
"""Real (deterministic) Date object based on date"""
|
167
|
+
|
168
|
+
def __init__(self, year: int, month: int, day: int) -> None:
|
169
|
+
"""Create a date-like object"""
|
170
|
+
self._wrapped_date_object = date(year, month, day)
|
171
|
+
self.year = year
|
172
|
+
self.month = month
|
173
|
+
self.day = day
|
174
|
+
super().__init__(float(self._wrapped_date_object.toordinal()))
|
175
|
+
self.timetuple = self._wrapped_date_object.timetuple
|
176
|
+
self.weekday = self._wrapped_date_object.weekday
|
177
|
+
self.isoweekday = self._wrapped_date_object.isoweekday
|
178
|
+
self.isocalendar = self._wrapped_date_object.isocalendar
|
179
|
+
self.ctime = self._wrapped_date_object.ctime
|
180
|
+
|
181
|
+
def __bool__(self, /) -> bool:
|
182
|
+
"""True if a real date is wrapped"""
|
183
|
+
return True
|
184
|
+
|
185
|
+
def __repr__(self, /) -> str:
|
186
|
+
"""String representation of the object"""
|
187
|
+
return f"{self.__class__.__name__}({self.year}, {self.month}, {self.day})"
|
188
|
+
|
189
|
+
def strftime(self, fmt: str, /) -> str:
|
194
190
|
"""String representation of the date"""
|
195
|
-
return self.
|
191
|
+
return self._wrapped_date_object.strftime(fmt or ISO_DATE_FORMAT)
|
196
192
|
|
197
|
-
|
198
|
-
def today(cls: type[D], /) -> D:
|
199
|
-
"""Today as Date object"""
|
200
|
-
return cls.factory(datetime.date.today())
|
201
|
-
|
202
|
-
@classmethod
|
203
|
-
def fromisoformat(
|
204
|
-
cls: type[D],
|
205
|
-
source: str,
|
206
|
-
/,
|
207
|
-
) -> D:
|
208
|
-
"""Create an instance from an iso format representation"""
|
209
|
-
lower_source_stripped = source.strip().lower()
|
210
|
-
if lower_source_stripped == INFINITE_DATE_DISPLAY:
|
211
|
-
return cls(INFINITY)
|
212
|
-
#
|
213
|
-
if lower_source_stripped == NEGATIVE_INFINITE_DATE_DISPLAY:
|
214
|
-
return cls(NEGATIVE_INFINITY)
|
215
|
-
#
|
216
|
-
return cls.factory(datetime.date.fromisoformat(lower_source_stripped))
|
217
|
-
|
218
|
-
@classmethod
|
219
|
-
def fromisocalendar(
|
220
|
-
cls: type[D],
|
221
|
-
/,
|
222
|
-
year: int,
|
223
|
-
week: int,
|
224
|
-
day: int,
|
225
|
-
) -> D:
|
226
|
-
"""Create an instance from an iso calendar date"""
|
227
|
-
return cls.factory(datetime.date.fromisocalendar(year, week, day))
|
228
|
-
|
229
|
-
@classmethod
|
230
|
-
def fromordinal(
|
231
|
-
cls: type[D],
|
232
|
-
source: float | int,
|
233
|
-
/,
|
234
|
-
) -> D:
|
235
|
-
"""Create an instance from a date ordinal"""
|
236
|
-
if isinstance(source, int):
|
237
|
-
return cls.factory(datetime.date.fromordinal(source))
|
238
|
-
#
|
239
|
-
if source in (NEGATIVE_INFINITY, INFINITY):
|
240
|
-
return cls(source)
|
241
|
-
#
|
242
|
-
raise ValueError(f"Invalid source for .fromordinal(): {source!r}")
|
243
|
-
|
244
|
-
@classmethod
|
245
|
-
def from_api_data(
|
246
|
-
cls: type[D],
|
247
|
-
source: Any,
|
248
|
-
/,
|
249
|
-
*,
|
250
|
-
fmt: str = ISO_DATETIME_FORMAT_UTC,
|
251
|
-
past_bound: bool = False,
|
252
|
-
) -> D:
|
253
|
-
"""Create an instance from string or another type,
|
254
|
-
assuming infinity in the latter case
|
255
|
-
"""
|
256
|
-
if isinstance(source, str):
|
257
|
-
return cls.factory(datetime.datetime.strptime(source, fmt))
|
258
|
-
#
|
259
|
-
return cls(NEGATIVE_INFINITY if past_bound else INFINITY)
|
193
|
+
__format__ = strftime
|
260
194
|
|
261
|
-
|
262
|
-
|
263
|
-
|
264
|
-
|
265
|
-
|
266
|
-
|
267
|
-
|
268
|
-
|
269
|
-
|
270
|
-
|
271
|
-
) -> D:
|
272
|
-
"""Create a new instance from a datetime.date or datetime.datetime object,
|
273
|
-
from
|
274
|
-
"""
|
275
|
-
if isinstance(source, (datetime.date, datetime.datetime)):
|
276
|
-
return cls(source.year, source.month, source.day)
|
277
|
-
#
|
278
|
-
return cls(source)
|
195
|
+
def replace(self, /, year: int = 0, month: int = 0, day: int = 0):
|
196
|
+
"""Return a copy with year, month, and/or date replaced"""
|
197
|
+
internal_object = self._wrapped_date_object
|
198
|
+
return from_datetime_object(
|
199
|
+
internal_object.replace(
|
200
|
+
year=year or internal_object.year,
|
201
|
+
month=month or internal_object.month,
|
202
|
+
day=day or internal_object.day,
|
203
|
+
)
|
204
|
+
)
|
279
205
|
|
280
206
|
|
281
|
-
|
282
|
-
|
207
|
+
# Absolute minimum and maximum dates
|
208
|
+
MIN: Final[GenericDate] = InfinityDate(past_bound=True)
|
209
|
+
MAX: Final[GenericDate] = InfinityDate(past_bound=False)
|
210
|
+
|
211
|
+
|
212
|
+
# Factory functions
|
213
|
+
|
214
|
+
|
215
|
+
def from_datetime_object(source: date | datetime, /) -> GenericDate:
|
216
|
+
"""Create a new RealDate instance from a
|
217
|
+
date or datetime object
|
218
|
+
"""
|
219
|
+
return RealDate(source.year, source.month, source.day)
|
220
|
+
|
221
|
+
|
222
|
+
def from_native_type(
|
223
|
+
source: Any,
|
224
|
+
/,
|
225
|
+
*,
|
226
|
+
fmt: str = ISO_DATETIME_FORMAT_UTC,
|
227
|
+
past_bound: bool = False,
|
228
|
+
) -> GenericDate:
|
229
|
+
"""Create an InfinityDate or RealDate instance from string or another type,
|
230
|
+
assuming infinity in the latter case
|
231
|
+
"""
|
232
|
+
if isinstance(source, str):
|
233
|
+
return from_datetime_object(datetime.strptime(source, fmt))
|
234
|
+
#
|
235
|
+
if source == -inf or source is None and past_bound:
|
236
|
+
return MIN
|
237
|
+
#
|
238
|
+
if source == inf or source is None and not past_bound:
|
239
|
+
return MAX
|
240
|
+
#
|
241
|
+
raise ValueError(f"Don’t know how to convert {source!r} into a date")
|
242
|
+
|
243
|
+
|
244
|
+
def fromordinal(ordinal: float | int) -> GenericDate:
|
245
|
+
"""Create an InfinityDate or RealDate instance from the provided ordinal"""
|
246
|
+
if ordinal == -inf:
|
247
|
+
return MIN
|
248
|
+
#
|
249
|
+
if ordinal == inf:
|
250
|
+
return MAX
|
251
|
+
#
|
252
|
+
try:
|
253
|
+
new_ordinal = int(ordinal)
|
254
|
+
except ValueError as error:
|
255
|
+
raise ValueError(f"Cannot convert {ordinal!r} to integer") from error
|
256
|
+
#
|
257
|
+
if not MIN_ORDINAL <= new_ordinal <= MAX_ORDINAL:
|
258
|
+
raise OverflowError("RealDate value out of range")
|
259
|
+
#
|
260
|
+
stdlib_date_object = date.fromordinal(new_ordinal)
|
261
|
+
return from_datetime_object(stdlib_date_object)
|
262
|
+
|
263
|
+
|
264
|
+
def fromisoformat(source: str, /) -> GenericDate:
|
265
|
+
"""Create an InfinityDate or RealDate instance from an iso format representation"""
|
266
|
+
lower_source_stripped = source.strip().lower()
|
267
|
+
if lower_source_stripped == INFINITE_DATE_DISPLAY:
|
268
|
+
return MAX
|
269
|
+
#
|
270
|
+
if lower_source_stripped == NEGATIVE_INFINITE_DATE_DISPLAY:
|
271
|
+
return MIN
|
272
|
+
#
|
273
|
+
return from_datetime_object(date.fromisoformat(source))
|
274
|
+
|
275
|
+
|
276
|
+
def fromisocalendar(year: int, week: int, weekday: int) -> GenericDate:
|
277
|
+
"""Create a RealDate instance from an iso calendar date"""
|
278
|
+
return from_datetime_object(date.fromisocalendar(year, week, weekday))
|
279
|
+
|
280
|
+
|
281
|
+
def today() -> GenericDate:
|
282
|
+
"""Today as RealDate object"""
|
283
|
+
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))
|
@@ -0,0 +1,141 @@
|
|
1
|
+
Metadata-Version: 2.4
|
2
|
+
Name: infdate
|
3
|
+
Version: 0.2.0
|
4
|
+
Summary: Date object wrapper supporting infinity
|
5
|
+
Project-URL: Homepage, https://gitlab.com/blackstream-x/infdate
|
6
|
+
Project-URL: CI, https://gitlab.com/blackstream-x/infdate/-/pipelines
|
7
|
+
Project-URL: Bug Tracker, https://gitlab.com/blackstream-x/infdate/-/issues
|
8
|
+
Project-URL: Repository, https://gitlab.com/blackstream-x/infdate.git
|
9
|
+
Author-email: Rainer Schwarzbach <rainer@blackstream.de>
|
10
|
+
License-File: LICENSE
|
11
|
+
Classifier: Development Status :: 4 - Beta
|
12
|
+
Classifier: Intended Audience :: Developers
|
13
|
+
Classifier: License :: OSI Approved :: MIT License
|
14
|
+
Classifier: Operating System :: OS Independent
|
15
|
+
Classifier: Programming Language :: Python :: 3
|
16
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
17
|
+
Classifier: Programming Language :: Python :: 3.11
|
18
|
+
Classifier: Programming Language :: Python :: 3.12
|
19
|
+
Classifier: Programming Language :: Python :: 3.13
|
20
|
+
Classifier: Programming Language :: Python :: 3.14
|
21
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
22
|
+
Requires-Python: >=3.11
|
23
|
+
Description-Content-Type: text/markdown
|
24
|
+
|
25
|
+
# infdate
|
26
|
+
|
27
|
+
_Python module for date calculations implementing a concept of infinity_
|
28
|
+
|
29
|
+
## Module description
|
30
|
+
|
31
|
+
Class hierarchy:
|
32
|
+
|
33
|
+
└── GenericDate
|
34
|
+
├── InfinityDate
|
35
|
+
└── RealDate
|
36
|
+
|
37
|
+
|
38
|
+
|
39
|
+
**InfinityDate** can represent either positive or negative infinity.
|
40
|
+
The module-level constants **MIN** and **MAX** contain the two possible
|
41
|
+
**InfinityDate** instance variations.
|
42
|
+
|
43
|
+
**RealDate** instances represent real dates like the standard library’s
|
44
|
+
**datetime.date** class, with mostly equal or similöar semantics.
|
45
|
+
|
46
|
+
For any valid **RealDate** instance, the following is **True**:
|
47
|
+
|
48
|
+
``` python
|
49
|
+
infdate.MIN < infdate.RealDate(1, 1, 1) <= real_date_instance <= infdate.RealDate(9999, 12, 31) < infdate.MAX
|
50
|
+
```
|
51
|
+
|
52
|
+
The following factory methods from the **datetime.date** class
|
53
|
+
are provided as module-level functions:
|
54
|
+
|
55
|
+
* **fromordinal()** (also accepting **-math.inf** or **math.inf**)
|
56
|
+
* **fromisoformat()**
|
57
|
+
* **fromisocalendar()**
|
58
|
+
* **today()**
|
59
|
+
|
60
|
+
**fromtimestamp()** is still missing by mistake.
|
61
|
+
|
62
|
+
Two additional factory functions are provided in the module:
|
63
|
+
|
64
|
+
* **from_datetime_object()** to create a **RealDate** instance from a
|
65
|
+
**datetime.date** or **datetime.datetime** instance
|
66
|
+
|
67
|
+
* **from_native_type()** to create an **InfinityDate** or **RealDate**
|
68
|
+
instance from a string, from **None**, **-math.inf** or **math.inf**.
|
69
|
+
|
70
|
+
This can come handy when dealing with API representations of dates,
|
71
|
+
eg. in GitLab’s [Personal Access Tokens API].
|
72
|
+
|
73
|
+
Some notable difference from the **datetime.date** class:
|
74
|
+
|
75
|
+
* The **.toordinal()** method returns **float** instead of **int**
|
76
|
+
|
77
|
+
* The **resolution** attribute is **1.0** instead of **datetime.timedelta(days=1)**
|
78
|
+
but also represents exactly one day.
|
79
|
+
|
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.
|
82
|
+
|
83
|
+
* Likewise, you cannot add or subtract **datetime.timedelta** instances
|
84
|
+
from n **InfinityDate** or **RealDate**, only **loat** or **int**.
|
85
|
+
|
86
|
+
|
87
|
+
## Example usage
|
88
|
+
|
89
|
+
``` pycon
|
90
|
+
>>> import infdate
|
91
|
+
>>> today = infdate.today()
|
92
|
+
>>> today
|
93
|
+
RealDate(2025, 6, 25)
|
94
|
+
>>> print(today)
|
95
|
+
2025-06-25
|
96
|
+
>>> print(f"US date notation {today:%m/%d/%y}")
|
97
|
+
US date notation 06/25/25
|
98
|
+
>>> today.ctime()
|
99
|
+
'Wed Jun 25 00:00:00 2025'
|
100
|
+
>>> today.isocalendar()
|
101
|
+
datetime.IsoCalendarDate(year=2025, week=26, weekday=3)
|
102
|
+
>>>
|
103
|
+
>>> yesterday = today - 1
|
104
|
+
>>> yesterday.ctime()
|
105
|
+
'Tue Jun 24 00:00:00 2025'
|
106
|
+
>>>
|
107
|
+
>>> today - yesterday
|
108
|
+
1.0
|
109
|
+
>>> infdate.MIN
|
110
|
+
InfinityDate(past_bound=True)
|
111
|
+
>>> infdate.MAX
|
112
|
+
InfinityDate(past_bound=False)
|
113
|
+
>>> infdate.MAX - today
|
114
|
+
inf
|
115
|
+
>>> infdate.MAX - infdate.MIN
|
116
|
+
inf
|
117
|
+
```
|
118
|
+
|
119
|
+
You can compare **InfinityDate**, **RealDate** and **datetime.date** instances,
|
120
|
+
and subtract them from each other (although currently, `__rsub__` is not implemented yet,
|
121
|
+
so subtracting an **InfinityDate** or **RealDate** from a **datetime.date**
|
122
|
+
still gives a **TypeError**):
|
123
|
+
|
124
|
+
>>> from datetime import date
|
125
|
+
>>> stdlib_today = date.today()
|
126
|
+
>>> today == stdlib_today
|
127
|
+
True
|
128
|
+
>>> yesterday < stdlib_today
|
129
|
+
True
|
130
|
+
>>> yesterday - stdlib_today
|
131
|
+
-1.0
|
132
|
+
>>> stdlib_today - yesterday
|
133
|
+
Traceback (most recent call last):
|
134
|
+
File "<python-input-22>", line 1, in <module>
|
135
|
+
stdlib_today - yesterday
|
136
|
+
~~~~~~~~~~~~~^~~~~~~~~~~
|
137
|
+
TypeError: unsupported operand type(s) for -: 'datetime.date' and 'RealDate'
|
138
|
+
|
139
|
+
|
140
|
+
* * *
|
141
|
+
[Personal Access Tokens API]: https://docs.gitlab.com/api/personal_access_tokens/
|
@@ -0,0 +1,6 @@
|
|
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,,
|
infdate/__main__.py
DELETED
infdate-0.1.0.dist-info/METADATA
DELETED
@@ -1,42 +0,0 @@
|
|
1
|
-
Metadata-Version: 2.4
|
2
|
-
Name: infdate
|
3
|
-
Version: 0.1.0
|
4
|
-
Summary: Date object wrapper supporting infinity
|
5
|
-
Project-URL: Homepage, https://gitlab.com/blackstream-x/infdate
|
6
|
-
Project-URL: CI, https://gitlab.com/blackstream-x/infdate/-/pipelines
|
7
|
-
Project-URL: Bug Tracker, https://gitlab.com/blackstream-x/infdate/-/issues
|
8
|
-
Project-URL: Repository, https://gitlab.com/blackstream-x/infdate.git
|
9
|
-
Author-email: Rainer Schwarzbach <rainer@blackstream.de>
|
10
|
-
License-File: LICENSE
|
11
|
-
Classifier: Development Status :: 4 - Beta
|
12
|
-
Classifier: Intended Audience :: Developers
|
13
|
-
Classifier: License :: OSI Approved :: MIT License
|
14
|
-
Classifier: Operating System :: OS Independent
|
15
|
-
Classifier: Programming Language :: Python :: 3
|
16
|
-
Classifier: Programming Language :: Python :: 3 :: Only
|
17
|
-
Classifier: Programming Language :: Python :: 3.11
|
18
|
-
Classifier: Programming Language :: Python :: 3.12
|
19
|
-
Classifier: Programming Language :: Python :: 3.13
|
20
|
-
Classifier: Programming Language :: Python :: 3.14
|
21
|
-
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
22
|
-
Requires-Python: >=3.11
|
23
|
-
Description-Content-Type: text/markdown
|
24
|
-
|
25
|
-
# infdate
|
26
|
-
|
27
|
-
_Python module for date calculations implementing a concept of infinity_
|
28
|
-
|
29
|
-
The **Date** class provided in this package wraps the standard library’s
|
30
|
-
**datetime.date** class and adds the capability to specify dates in positive
|
31
|
-
(after everything else) or negative (before everything else) infinity,
|
32
|
-
and to do calculations (add days, or subtract days or other **Date** instances)
|
33
|
-
with these objects.
|
34
|
-
|
35
|
-
For easier usage, differences are expressed as integers (1 = one day)
|
36
|
-
or floats (inf and -inf _only_).
|
37
|
-
|
38
|
-
These capabilities can come handy when dealing with API representations of dates,
|
39
|
-
eg. in GitLab’s [Personal Access Tokens API].
|
40
|
-
|
41
|
-
* * *
|
42
|
-
[Personal Access Tokens API]: https://docs.gitlab.com/api/personal_access_tokens/
|
infdate-0.1.0.dist-info/RECORD
DELETED
@@ -1,7 +0,0 @@
|
|
1
|
-
infdate/__init__.py,sha256=_gJvzSUHx1bvs6xCWI9fhDniWhFR2RD5VQUaFKZoP7U,8890
|
2
|
-
infdate/__main__.py,sha256=RtLjAiDLfFTpSFuU68nKhVfBUkxqDhhIY2jNxgL2cW4,113
|
3
|
-
infdate/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
4
|
-
infdate-0.1.0.dist-info/METADATA,sha256=p-zgj6Xlvz79e_QBIC0DrmHBilf83e-u7olcyyjLDA4,1821
|
5
|
-
infdate-0.1.0.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
|
6
|
-
infdate-0.1.0.dist-info/licenses/LICENSE,sha256=867pxriiObx28vCU1JsRtu3H9kUKyl54e0-xl1IIv3Y,913
|
7
|
-
infdate-0.1.0.dist-info/RECORD,,
|
File without changes
|
File without changes
|