crashbytes-dateutils 1.0.0__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.
- crashbytes_dateutils-1.0.0/.github/CODEOWNERS +1 -0
- crashbytes_dateutils-1.0.0/.github/workflows/ci.yml +23 -0
- crashbytes_dateutils-1.0.0/.github/workflows/publish.yml +19 -0
- crashbytes_dateutils-1.0.0/.gitignore +11 -0
- crashbytes_dateutils-1.0.0/LICENSE +21 -0
- crashbytes_dateutils-1.0.0/PKG-INFO +57 -0
- crashbytes_dateutils-1.0.0/README.md +29 -0
- crashbytes_dateutils-1.0.0/pyproject.toml +53 -0
- crashbytes_dateutils-1.0.0/src/crashbytes_dateutils/__init__.py +39 -0
- crashbytes_dateutils-1.0.0/src/crashbytes_dateutils/_core.py +155 -0
- crashbytes_dateutils-1.0.0/src/crashbytes_dateutils/py.typed +0 -0
- crashbytes_dateutils-1.0.0/tests/test_dateutils.py +209 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
* @CrashBytes
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
name: CI
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
push:
|
|
5
|
+
branches: [main]
|
|
6
|
+
pull_request:
|
|
7
|
+
branches: [main]
|
|
8
|
+
|
|
9
|
+
jobs:
|
|
10
|
+
test:
|
|
11
|
+
runs-on: ubuntu-latest
|
|
12
|
+
strategy:
|
|
13
|
+
matrix:
|
|
14
|
+
python-version: ["3.10", "3.11", "3.12", "3.13"]
|
|
15
|
+
steps:
|
|
16
|
+
- uses: actions/checkout@v4
|
|
17
|
+
- uses: actions/setup-python@v5
|
|
18
|
+
with:
|
|
19
|
+
python-version: ${{ matrix.python-version }}
|
|
20
|
+
- run: pip install -e ".[dev]"
|
|
21
|
+
- run: ruff check src/ tests/
|
|
22
|
+
- run: mypy --strict src/
|
|
23
|
+
- run: pytest --cov=crashbytes_dateutils --cov-branch --cov-fail-under=90
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
name: Publish to PyPI
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
push:
|
|
5
|
+
tags: ["*"]
|
|
6
|
+
|
|
7
|
+
jobs:
|
|
8
|
+
publish:
|
|
9
|
+
runs-on: ubuntu-latest
|
|
10
|
+
permissions:
|
|
11
|
+
id-token: write
|
|
12
|
+
steps:
|
|
13
|
+
- uses: actions/checkout@v4
|
|
14
|
+
- uses: actions/setup-python@v5
|
|
15
|
+
with:
|
|
16
|
+
python-version: "3.12"
|
|
17
|
+
- run: pip install build
|
|
18
|
+
- run: python -m build
|
|
19
|
+
- uses: pypa/gh-action-pypi-publish@release/v1
|
|
@@ -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.
|
|
@@ -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,29 @@
|
|
|
1
|
+
# crashbytes-dateutils
|
|
2
|
+
|
|
3
|
+
Zero-dependency datetime business helpers — business days, fiscal quarters, relative strings.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
pip install crashbytes-dateutils
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
```python
|
|
14
|
+
from datetime import date
|
|
15
|
+
from crashbytes_dateutils import (
|
|
16
|
+
add_business_days, business_days_between, to_relative_string,
|
|
17
|
+
fiscal_quarter, age, start_of_month, end_of_month,
|
|
18
|
+
)
|
|
19
|
+
|
|
20
|
+
add_business_days(date(2024, 6, 14), 1) # date(2024, 6, 17) — skips weekend
|
|
21
|
+
business_days_between(date(2024, 6, 17), date(2024, 6, 21)) # 4
|
|
22
|
+
to_relative_string(date(2024, 6, 12), today=date(2024, 6, 15)) # "3 days ago"
|
|
23
|
+
fiscal_quarter(date(2024, 7, 15), start_month=7) # 1
|
|
24
|
+
age(date(1990, 6, 15), today=date(2024, 6, 15)) # 34
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
## License
|
|
28
|
+
|
|
29
|
+
MIT
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["hatchling"]
|
|
3
|
+
build-backend = "hatchling.build"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "crashbytes-dateutils"
|
|
7
|
+
version = "1.0.0"
|
|
8
|
+
description = "Zero-dependency datetime business helpers — business days, fiscal quarters, relative strings."
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
license = "MIT"
|
|
11
|
+
requires-python = ">=3.10"
|
|
12
|
+
authors = [{ name = "CrashBytes", email = "crashbytes@users.noreply.github.com" }]
|
|
13
|
+
keywords = ["datetime", "business-days", "fiscal-quarter", "date-utilities"]
|
|
14
|
+
classifiers = [
|
|
15
|
+
"Development Status :: 5 - Production/Stable",
|
|
16
|
+
"Intended Audience :: Developers",
|
|
17
|
+
"License :: OSI Approved :: MIT License",
|
|
18
|
+
"Programming Language :: Python :: 3",
|
|
19
|
+
"Programming Language :: Python :: 3.10",
|
|
20
|
+
"Programming Language :: Python :: 3.11",
|
|
21
|
+
"Programming Language :: Python :: 3.12",
|
|
22
|
+
"Programming Language :: Python :: 3.13",
|
|
23
|
+
"Typing :: Typed",
|
|
24
|
+
]
|
|
25
|
+
|
|
26
|
+
[project.optional-dependencies]
|
|
27
|
+
dev = ["pytest", "pytest-cov", "mypy", "ruff"]
|
|
28
|
+
|
|
29
|
+
[project.urls]
|
|
30
|
+
Homepage = "https://github.com/CrashBytes/crashbytes-dateutils"
|
|
31
|
+
Repository = "https://github.com/CrashBytes/crashbytes-dateutils"
|
|
32
|
+
Issues = "https://github.com/CrashBytes/crashbytes-dateutils/issues"
|
|
33
|
+
|
|
34
|
+
[tool.ruff]
|
|
35
|
+
target-version = "py310"
|
|
36
|
+
line-length = 99
|
|
37
|
+
|
|
38
|
+
[tool.ruff.lint]
|
|
39
|
+
select = ["E", "F", "I", "N", "UP", "B", "SIM", "TCH"]
|
|
40
|
+
|
|
41
|
+
[tool.mypy]
|
|
42
|
+
strict = true
|
|
43
|
+
python_version = "3.10"
|
|
44
|
+
|
|
45
|
+
[tool.pytest.ini_options]
|
|
46
|
+
testpaths = ["tests"]
|
|
47
|
+
|
|
48
|
+
[tool.coverage.run]
|
|
49
|
+
branch = true
|
|
50
|
+
source = ["crashbytes_dateutils"]
|
|
51
|
+
|
|
52
|
+
[tool.coverage.report]
|
|
53
|
+
fail_under = 90
|
|
@@ -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,209 @@
|
|
|
1
|
+
"""Tests for crashbytes-dateutils."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from datetime import date, datetime
|
|
6
|
+
|
|
7
|
+
from crashbytes_dateutils import (
|
|
8
|
+
add_business_days,
|
|
9
|
+
age,
|
|
10
|
+
business_days_between,
|
|
11
|
+
end_of_day,
|
|
12
|
+
end_of_month,
|
|
13
|
+
end_of_year,
|
|
14
|
+
fiscal_quarter,
|
|
15
|
+
fiscal_year,
|
|
16
|
+
is_business_day,
|
|
17
|
+
is_weekend,
|
|
18
|
+
next_business_day,
|
|
19
|
+
previous_business_day,
|
|
20
|
+
start_of_day,
|
|
21
|
+
start_of_month,
|
|
22
|
+
start_of_year,
|
|
23
|
+
to_relative_string,
|
|
24
|
+
)
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
class TestStartEndOfDay:
|
|
28
|
+
def test_start_of_day(self) -> None:
|
|
29
|
+
dt = datetime(2024, 6, 15, 14, 30, 45, 123456)
|
|
30
|
+
result = start_of_day(dt)
|
|
31
|
+
assert result == datetime(2024, 6, 15, 0, 0, 0, 0)
|
|
32
|
+
|
|
33
|
+
def test_end_of_day(self) -> None:
|
|
34
|
+
dt = datetime(2024, 6, 15, 14, 30, 45)
|
|
35
|
+
result = end_of_day(dt)
|
|
36
|
+
assert result == datetime(2024, 6, 15, 23, 59, 59, 999999)
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
class TestStartEndOfMonth:
|
|
40
|
+
def test_start_of_month(self) -> None:
|
|
41
|
+
assert start_of_month(date(2024, 6, 15)) == date(2024, 6, 1)
|
|
42
|
+
|
|
43
|
+
def test_end_of_month_june(self) -> None:
|
|
44
|
+
assert end_of_month(date(2024, 6, 15)) == date(2024, 6, 30)
|
|
45
|
+
|
|
46
|
+
def test_end_of_month_february_leap(self) -> None:
|
|
47
|
+
assert end_of_month(date(2024, 2, 1)) == date(2024, 2, 29)
|
|
48
|
+
|
|
49
|
+
def test_end_of_month_february_non_leap(self) -> None:
|
|
50
|
+
assert end_of_month(date(2023, 2, 1)) == date(2023, 2, 28)
|
|
51
|
+
|
|
52
|
+
def test_end_of_month_december(self) -> None:
|
|
53
|
+
assert end_of_month(date(2024, 12, 1)) == date(2024, 12, 31)
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
class TestStartEndOfYear:
|
|
57
|
+
def test_start_of_year(self) -> None:
|
|
58
|
+
assert start_of_year(date(2024, 6, 15)) == date(2024, 1, 1)
|
|
59
|
+
|
|
60
|
+
def test_end_of_year(self) -> None:
|
|
61
|
+
assert end_of_year(date(2024, 6, 15)) == date(2024, 12, 31)
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
class TestWeekend:
|
|
65
|
+
def test_saturday(self) -> None:
|
|
66
|
+
assert is_weekend(date(2024, 6, 15)) is True # Saturday
|
|
67
|
+
|
|
68
|
+
def test_sunday(self) -> None:
|
|
69
|
+
assert is_weekend(date(2024, 6, 16)) is True # Sunday
|
|
70
|
+
|
|
71
|
+
def test_monday(self) -> None:
|
|
72
|
+
assert is_weekend(date(2024, 6, 17)) is False # Monday
|
|
73
|
+
|
|
74
|
+
def test_friday(self) -> None:
|
|
75
|
+
assert is_weekend(date(2024, 6, 14)) is False # Friday
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
class TestBusinessDay:
|
|
79
|
+
def test_weekday(self) -> None:
|
|
80
|
+
assert is_business_day(date(2024, 6, 17)) is True # Monday
|
|
81
|
+
|
|
82
|
+
def test_weekend(self) -> None:
|
|
83
|
+
assert is_business_day(date(2024, 6, 15)) is False # Saturday
|
|
84
|
+
|
|
85
|
+
def test_holiday(self) -> None:
|
|
86
|
+
holidays = [date(2024, 6, 17)]
|
|
87
|
+
assert is_business_day(date(2024, 6, 17), holidays) is False
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
class TestAddBusinessDays:
|
|
91
|
+
def test_add_one(self) -> None:
|
|
92
|
+
# Friday + 1 = Monday
|
|
93
|
+
assert add_business_days(date(2024, 6, 14), 1) == date(2024, 6, 17)
|
|
94
|
+
|
|
95
|
+
def test_add_five(self) -> None:
|
|
96
|
+
# Monday + 5 = next Monday
|
|
97
|
+
assert add_business_days(date(2024, 6, 17), 5) == date(2024, 6, 24)
|
|
98
|
+
|
|
99
|
+
def test_add_zero(self) -> None:
|
|
100
|
+
d = date(2024, 6, 17)
|
|
101
|
+
assert add_business_days(d, 0) is d
|
|
102
|
+
|
|
103
|
+
def test_subtract_one(self) -> None:
|
|
104
|
+
# Monday - 1 = previous Friday
|
|
105
|
+
assert add_business_days(date(2024, 6, 17), -1) == date(2024, 6, 14)
|
|
106
|
+
|
|
107
|
+
def test_with_holidays(self) -> None:
|
|
108
|
+
holidays = [date(2024, 6, 17)] # Monday is holiday
|
|
109
|
+
# Friday + 1, skip Monday holiday = Tuesday
|
|
110
|
+
assert add_business_days(date(2024, 6, 14), 1, holidays) == date(2024, 6, 18)
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
class TestBusinessDaysBetween:
|
|
114
|
+
def test_same_week(self) -> None:
|
|
115
|
+
# Mon to Fri = 4 business days
|
|
116
|
+
assert business_days_between(date(2024, 6, 17), date(2024, 6, 21)) == 4
|
|
117
|
+
|
|
118
|
+
def test_across_weekend(self) -> None:
|
|
119
|
+
# Fri to next Mon = 1 business day
|
|
120
|
+
assert business_days_between(date(2024, 6, 14), date(2024, 6, 17)) == 1
|
|
121
|
+
|
|
122
|
+
def test_same_day(self) -> None:
|
|
123
|
+
assert business_days_between(date(2024, 6, 17), date(2024, 6, 17)) == 0
|
|
124
|
+
|
|
125
|
+
def test_start_after_end(self) -> None:
|
|
126
|
+
assert business_days_between(date(2024, 6, 20), date(2024, 6, 17)) == 0
|
|
127
|
+
|
|
128
|
+
def test_with_holidays(self) -> None:
|
|
129
|
+
holidays = [date(2024, 6, 18)]
|
|
130
|
+
# Mon to Fri, minus Tuesday holiday = 3
|
|
131
|
+
assert business_days_between(date(2024, 6, 17), date(2024, 6, 21), holidays) == 3
|
|
132
|
+
|
|
133
|
+
|
|
134
|
+
class TestRelativeString:
|
|
135
|
+
def test_today(self) -> None:
|
|
136
|
+
d = date(2024, 6, 15)
|
|
137
|
+
assert to_relative_string(d, today=d) == "today"
|
|
138
|
+
|
|
139
|
+
def test_tomorrow(self) -> None:
|
|
140
|
+
assert to_relative_string(date(2024, 6, 16), today=date(2024, 6, 15)) == "tomorrow"
|
|
141
|
+
|
|
142
|
+
def test_yesterday(self) -> None:
|
|
143
|
+
assert to_relative_string(date(2024, 6, 14), today=date(2024, 6, 15)) == "yesterday"
|
|
144
|
+
|
|
145
|
+
def test_days_ago(self) -> None:
|
|
146
|
+
assert to_relative_string(date(2024, 6, 12), today=date(2024, 6, 15)) == "3 days ago"
|
|
147
|
+
|
|
148
|
+
def test_days_future(self) -> None:
|
|
149
|
+
assert to_relative_string(date(2024, 6, 18), today=date(2024, 6, 15)) == "in 3 days"
|
|
150
|
+
|
|
151
|
+
def test_weeks(self) -> None:
|
|
152
|
+
assert to_relative_string(date(2024, 5, 25), today=date(2024, 6, 15)) == "3 weeks ago"
|
|
153
|
+
|
|
154
|
+
def test_months(self) -> None:
|
|
155
|
+
assert to_relative_string(date(2024, 3, 15), today=date(2024, 6, 15)) == "3 months ago"
|
|
156
|
+
|
|
157
|
+
def test_years(self) -> None:
|
|
158
|
+
assert to_relative_string(date(2022, 6, 15), today=date(2024, 6, 15)) == "2 years ago"
|
|
159
|
+
|
|
160
|
+
def test_1_week(self) -> None:
|
|
161
|
+
assert to_relative_string(date(2024, 6, 22), today=date(2024, 6, 15)) == "in 1 week"
|
|
162
|
+
|
|
163
|
+
def test_1_month(self) -> None:
|
|
164
|
+
assert to_relative_string(date(2024, 7, 15), today=date(2024, 6, 15)) == "in 1 month"
|
|
165
|
+
|
|
166
|
+
def test_1_year(self) -> None:
|
|
167
|
+
assert to_relative_string(date(2025, 6, 15), today=date(2024, 6, 15)) == "in 1 year"
|
|
168
|
+
|
|
169
|
+
|
|
170
|
+
class TestAge:
|
|
171
|
+
def test_basic(self) -> None:
|
|
172
|
+
assert age(date(1990, 6, 15), today=date(2024, 6, 15)) == 34
|
|
173
|
+
|
|
174
|
+
def test_before_birthday(self) -> None:
|
|
175
|
+
assert age(date(1990, 6, 15), today=date(2024, 6, 14)) == 33
|
|
176
|
+
|
|
177
|
+
def test_after_birthday(self) -> None:
|
|
178
|
+
assert age(date(1990, 6, 15), today=date(2024, 6, 16)) == 34
|
|
179
|
+
|
|
180
|
+
|
|
181
|
+
class TestFiscalQuarter:
|
|
182
|
+
def test_calendar_year(self) -> None:
|
|
183
|
+
assert fiscal_quarter(date(2024, 1, 15)) == 1
|
|
184
|
+
assert fiscal_quarter(date(2024, 4, 15)) == 2
|
|
185
|
+
assert fiscal_quarter(date(2024, 7, 15)) == 3
|
|
186
|
+
assert fiscal_quarter(date(2024, 10, 15)) == 4
|
|
187
|
+
|
|
188
|
+
def test_fiscal_year_july(self) -> None:
|
|
189
|
+
assert fiscal_quarter(date(2024, 7, 15), start_month=7) == 1
|
|
190
|
+
assert fiscal_quarter(date(2024, 10, 15), start_month=7) == 2
|
|
191
|
+
assert fiscal_quarter(date(2025, 1, 15), start_month=7) == 3
|
|
192
|
+
assert fiscal_quarter(date(2025, 4, 15), start_month=7) == 4
|
|
193
|
+
|
|
194
|
+
|
|
195
|
+
class TestFiscalYear:
|
|
196
|
+
def test_calendar_year(self) -> None:
|
|
197
|
+
assert fiscal_year(date(2024, 6, 15)) == 2024
|
|
198
|
+
|
|
199
|
+
def test_fiscal_year_july(self) -> None:
|
|
200
|
+
assert fiscal_year(date(2024, 7, 15), start_month=7) == 2024
|
|
201
|
+
assert fiscal_year(date(2024, 6, 15), start_month=7) == 2023
|
|
202
|
+
|
|
203
|
+
|
|
204
|
+
class TestNextPrevBusinessDay:
|
|
205
|
+
def test_next_from_friday(self) -> None:
|
|
206
|
+
assert next_business_day(date(2024, 6, 14)) == date(2024, 6, 17)
|
|
207
|
+
|
|
208
|
+
def test_previous_from_monday(self) -> None:
|
|
209
|
+
assert previous_business_day(date(2024, 6, 17)) == date(2024, 6, 14)
|