crashbytes-dateutils 1.0.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.
- crashbytes_dateutils/__init__.py +39 -0
- crashbytes_dateutils/_core.py +155 -0
- crashbytes_dateutils/py.typed +0 -0
- crashbytes_dateutils-1.0.0.dist-info/METADATA +57 -0
- crashbytes_dateutils-1.0.0.dist-info/RECORD +7 -0
- crashbytes_dateutils-1.0.0.dist-info/WHEEL +4 -0
- crashbytes_dateutils-1.0.0.dist-info/licenses/LICENSE +21 -0
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
"""crashbytes-dateutils — Zero-dependency datetime business helpers."""
|
|
2
|
+
|
|
3
|
+
from crashbytes_dateutils._core import (
|
|
4
|
+
add_business_days,
|
|
5
|
+
age,
|
|
6
|
+
business_days_between,
|
|
7
|
+
end_of_day,
|
|
8
|
+
end_of_month,
|
|
9
|
+
end_of_year,
|
|
10
|
+
fiscal_quarter,
|
|
11
|
+
fiscal_year,
|
|
12
|
+
is_business_day,
|
|
13
|
+
is_weekend,
|
|
14
|
+
next_business_day,
|
|
15
|
+
previous_business_day,
|
|
16
|
+
start_of_day,
|
|
17
|
+
start_of_month,
|
|
18
|
+
start_of_year,
|
|
19
|
+
to_relative_string,
|
|
20
|
+
)
|
|
21
|
+
|
|
22
|
+
__all__ = [
|
|
23
|
+
"add_business_days",
|
|
24
|
+
"age",
|
|
25
|
+
"business_days_between",
|
|
26
|
+
"end_of_day",
|
|
27
|
+
"end_of_month",
|
|
28
|
+
"end_of_year",
|
|
29
|
+
"fiscal_quarter",
|
|
30
|
+
"fiscal_year",
|
|
31
|
+
"is_business_day",
|
|
32
|
+
"is_weekend",
|
|
33
|
+
"next_business_day",
|
|
34
|
+
"previous_business_day",
|
|
35
|
+
"start_of_day",
|
|
36
|
+
"start_of_month",
|
|
37
|
+
"start_of_year",
|
|
38
|
+
"to_relative_string",
|
|
39
|
+
]
|
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
"""DateTime business helpers — business days, fiscal quarters, relative strings."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from datetime import date, datetime, timedelta
|
|
6
|
+
from typing import TYPE_CHECKING
|
|
7
|
+
|
|
8
|
+
if TYPE_CHECKING:
|
|
9
|
+
from collections.abc import Sequence
|
|
10
|
+
|
|
11
|
+
# Monday=0 ... Sunday=6
|
|
12
|
+
_SATURDAY = 5
|
|
13
|
+
_SUNDAY = 6
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
def start_of_day(dt: datetime) -> datetime:
|
|
17
|
+
"""Return *dt* with time set to 00:00:00."""
|
|
18
|
+
return dt.replace(hour=0, minute=0, second=0, microsecond=0)
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
def end_of_day(dt: datetime) -> datetime:
|
|
22
|
+
"""Return *dt* with time set to 23:59:59.999999."""
|
|
23
|
+
return dt.replace(hour=23, minute=59, second=59, microsecond=999999)
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
def start_of_month(d: date) -> date:
|
|
27
|
+
"""Return the first day of the month for *d*."""
|
|
28
|
+
return d.replace(day=1)
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
def end_of_month(d: date) -> date:
|
|
32
|
+
"""Return the last day of the month for *d*."""
|
|
33
|
+
if d.month == 12:
|
|
34
|
+
return d.replace(day=31)
|
|
35
|
+
return d.replace(month=d.month + 1, day=1) - timedelta(days=1)
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
def start_of_year(d: date) -> date:
|
|
39
|
+
"""Return January 1 of the year for *d*."""
|
|
40
|
+
return d.replace(month=1, day=1)
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
def end_of_year(d: date) -> date:
|
|
44
|
+
"""Return December 31 of the year for *d*."""
|
|
45
|
+
return d.replace(month=12, day=31)
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
def is_weekend(d: date) -> bool:
|
|
49
|
+
"""Check if *d* is a Saturday or Sunday."""
|
|
50
|
+
return d.weekday() >= _SATURDAY
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
def is_business_day(d: date, holidays: Sequence[date] = ()) -> bool:
|
|
54
|
+
"""Check if *d* is a business day (not weekend, not in *holidays*)."""
|
|
55
|
+
return not is_weekend(d) and d not in holidays
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
def add_business_days(
|
|
59
|
+
d: date, days: int, holidays: Sequence[date] = ()
|
|
60
|
+
) -> date:
|
|
61
|
+
"""Add *days* business days to *d*, skipping weekends and *holidays*."""
|
|
62
|
+
if days == 0:
|
|
63
|
+
return d
|
|
64
|
+
step = 1 if days > 0 else -1
|
|
65
|
+
remaining = abs(days)
|
|
66
|
+
current = d
|
|
67
|
+
while remaining > 0:
|
|
68
|
+
current += timedelta(days=step)
|
|
69
|
+
if is_business_day(current, holidays):
|
|
70
|
+
remaining -= 1
|
|
71
|
+
return current
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
def business_days_between(
|
|
75
|
+
start: date, end: date, holidays: Sequence[date] = ()
|
|
76
|
+
) -> int:
|
|
77
|
+
"""Count business days between *start* (exclusive) and *end* (inclusive)."""
|
|
78
|
+
if start >= end:
|
|
79
|
+
return 0
|
|
80
|
+
count = 0
|
|
81
|
+
current = start + timedelta(days=1)
|
|
82
|
+
while current <= end:
|
|
83
|
+
if is_business_day(current, holidays):
|
|
84
|
+
count += 1
|
|
85
|
+
current += timedelta(days=1)
|
|
86
|
+
return count
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
def to_relative_string(d: date, today: date | None = None) -> str:
|
|
90
|
+
"""Return a human-readable relative string like "3 days ago" or "in 2 weeks"."""
|
|
91
|
+
if today is None:
|
|
92
|
+
today = date.today()
|
|
93
|
+
delta = (d - today).days
|
|
94
|
+
if delta == 0:
|
|
95
|
+
return "today"
|
|
96
|
+
if delta == 1:
|
|
97
|
+
return "tomorrow"
|
|
98
|
+
if delta == -1:
|
|
99
|
+
return "yesterday"
|
|
100
|
+
abs_days = abs(delta)
|
|
101
|
+
if abs_days < 7:
|
|
102
|
+
unit = "day" if abs_days == 1 else "days"
|
|
103
|
+
label = f"{abs_days} {unit}"
|
|
104
|
+
elif abs_days < 30:
|
|
105
|
+
weeks = abs_days // 7
|
|
106
|
+
unit = "week" if weeks == 1 else "weeks"
|
|
107
|
+
label = f"{weeks} {unit}"
|
|
108
|
+
elif abs_days < 365:
|
|
109
|
+
months = abs_days // 30
|
|
110
|
+
unit = "month" if months == 1 else "months"
|
|
111
|
+
label = f"{months} {unit}"
|
|
112
|
+
else:
|
|
113
|
+
years = abs_days // 365
|
|
114
|
+
unit = "year" if years == 1 else "years"
|
|
115
|
+
label = f"{years} {unit}"
|
|
116
|
+
return f"in {label}" if delta > 0 else f"{label} ago"
|
|
117
|
+
|
|
118
|
+
|
|
119
|
+
def age(birth: date, today: date | None = None) -> int:
|
|
120
|
+
"""Calculate age in years from *birth* to *today*."""
|
|
121
|
+
if today is None:
|
|
122
|
+
today = date.today()
|
|
123
|
+
years = today.year - birth.year
|
|
124
|
+
if (today.month, today.day) < (birth.month, birth.day):
|
|
125
|
+
years -= 1
|
|
126
|
+
return years
|
|
127
|
+
|
|
128
|
+
|
|
129
|
+
def fiscal_quarter(d: date, start_month: int = 1) -> int:
|
|
130
|
+
"""Return fiscal quarter (1–4) for *d*.
|
|
131
|
+
|
|
132
|
+
*start_month* is the first month of the fiscal year (default: January).
|
|
133
|
+
"""
|
|
134
|
+
adjusted = (d.month - start_month) % 12
|
|
135
|
+
return adjusted // 3 + 1
|
|
136
|
+
|
|
137
|
+
|
|
138
|
+
def fiscal_year(d: date, start_month: int = 1) -> int:
|
|
139
|
+
"""Return fiscal year for *d*.
|
|
140
|
+
|
|
141
|
+
*start_month* is the first month of the fiscal year.
|
|
142
|
+
"""
|
|
143
|
+
if d.month >= start_month:
|
|
144
|
+
return d.year
|
|
145
|
+
return d.year - 1
|
|
146
|
+
|
|
147
|
+
|
|
148
|
+
def next_business_day(d: date, holidays: Sequence[date] = ()) -> date:
|
|
149
|
+
"""Return the next business day after *d*."""
|
|
150
|
+
return add_business_days(d, 1, holidays)
|
|
151
|
+
|
|
152
|
+
|
|
153
|
+
def previous_business_day(d: date, holidays: Sequence[date] = ()) -> date:
|
|
154
|
+
"""Return the previous business day before *d*."""
|
|
155
|
+
return add_business_days(d, -1, holidays)
|
|
File without changes
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: crashbytes-dateutils
|
|
3
|
+
Version: 1.0.0
|
|
4
|
+
Summary: Zero-dependency datetime business helpers — business days, fiscal quarters, relative strings.
|
|
5
|
+
Project-URL: Homepage, https://github.com/CrashBytes/crashbytes-dateutils
|
|
6
|
+
Project-URL: Repository, https://github.com/CrashBytes/crashbytes-dateutils
|
|
7
|
+
Project-URL: Issues, https://github.com/CrashBytes/crashbytes-dateutils/issues
|
|
8
|
+
Author-email: CrashBytes <crashbytes@users.noreply.github.com>
|
|
9
|
+
License-Expression: MIT
|
|
10
|
+
License-File: LICENSE
|
|
11
|
+
Keywords: business-days,date-utilities,datetime,fiscal-quarter
|
|
12
|
+
Classifier: Development Status :: 5 - Production/Stable
|
|
13
|
+
Classifier: Intended Audience :: Developers
|
|
14
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
15
|
+
Classifier: Programming Language :: Python :: 3
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
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.10
|
|
22
|
+
Provides-Extra: dev
|
|
23
|
+
Requires-Dist: mypy; extra == 'dev'
|
|
24
|
+
Requires-Dist: pytest; extra == 'dev'
|
|
25
|
+
Requires-Dist: pytest-cov; extra == 'dev'
|
|
26
|
+
Requires-Dist: ruff; extra == 'dev'
|
|
27
|
+
Description-Content-Type: text/markdown
|
|
28
|
+
|
|
29
|
+
# crashbytes-dateutils
|
|
30
|
+
|
|
31
|
+
Zero-dependency datetime business helpers — business days, fiscal quarters, relative strings.
|
|
32
|
+
|
|
33
|
+
## Install
|
|
34
|
+
|
|
35
|
+
```bash
|
|
36
|
+
pip install crashbytes-dateutils
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
## Usage
|
|
40
|
+
|
|
41
|
+
```python
|
|
42
|
+
from datetime import date
|
|
43
|
+
from crashbytes_dateutils import (
|
|
44
|
+
add_business_days, business_days_between, to_relative_string,
|
|
45
|
+
fiscal_quarter, age, start_of_month, end_of_month,
|
|
46
|
+
)
|
|
47
|
+
|
|
48
|
+
add_business_days(date(2024, 6, 14), 1) # date(2024, 6, 17) — skips weekend
|
|
49
|
+
business_days_between(date(2024, 6, 17), date(2024, 6, 21)) # 4
|
|
50
|
+
to_relative_string(date(2024, 6, 12), today=date(2024, 6, 15)) # "3 days ago"
|
|
51
|
+
fiscal_quarter(date(2024, 7, 15), start_month=7) # 1
|
|
52
|
+
age(date(1990, 6, 15), today=date(2024, 6, 15)) # 34
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
## License
|
|
56
|
+
|
|
57
|
+
MIT
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
crashbytes_dateutils/__init__.py,sha256=EC6xrDIY4xU9AGgqYOktEYSXDFb9XVeTdaW2KPz1qJg,795
|
|
2
|
+
crashbytes_dateutils/_core.py,sha256=1ZLSr5_IrTpCprSEzwAKWCyU63BoSVAx1Dwt1lAwU7Q,4505
|
|
3
|
+
crashbytes_dateutils/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
4
|
+
crashbytes_dateutils-1.0.0.dist-info/METADATA,sha256=-IYvgqBP-xmRIjBw90BubIlfCAnjg41C8zxgvS9Ncig,1986
|
|
5
|
+
crashbytes_dateutils-1.0.0.dist-info/WHEEL,sha256=QccIxa26bgl1E6uMy58deGWi-0aeIkkangHcxk2kWfw,87
|
|
6
|
+
crashbytes_dateutils-1.0.0.dist-info/licenses/LICENSE,sha256=Ic61HOO4EsyXXAGNY-D-1nG62feh_IBbipY1v_QcYcY,1067
|
|
7
|
+
crashbytes_dateutils-1.0.0.dist-info/RECORD,,
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 CrashBytes
|
|
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.
|