Skip to content

Commit abd3277

Browse files
committed
period
1 parent 9247421 commit abd3277

2 files changed

Lines changed: 43 additions & 20 deletions

File tree

ccy/dates/period.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from __future__ import annotations
22

3-
from typing import Any
3+
from typing import Any, Self
44

55

66
def period(pstr: str = "") -> Period:
@@ -31,7 +31,7 @@ def __init__(self, months: int = 0, days: int = 0) -> None:
3131
self._days = days
3232

3333
@classmethod
34-
def make(cls, data: Any) -> Period:
34+
def make(cls, data: Any) -> Self:
3535
if isinstance(data, cls):
3636
return data
3737
elif isinstance(data, str):
@@ -110,7 +110,7 @@ def simple(self) -> str:
110110
else:
111111
return ""
112112

113-
def add_tenure(self, pstr: str) -> Period:
113+
def add_tenure(self, pstr: str) -> Self:
114114
if isinstance(pstr, self.__class__):
115115
self._months += pstr._months
116116
self._days += pstr._days
@@ -141,18 +141,18 @@ def add_tenure(self, pstr: str) -> Period:
141141
st = st[ip:]
142142
return self
143143

144-
def __add__(self, other: Any) -> Period:
144+
def __add__(self, other: Any) -> Self:
145145
p = self.make(other)
146146
return self.__class__(self._months + p._months, self._days + p._days)
147147

148-
def __radd__(self, other: Any) -> Period:
148+
def __radd__(self, other: Any) -> Self:
149149
return self + other
150150

151-
def __sub__(self, other: Any) -> Period:
151+
def __sub__(self, other: Any) -> Self:
152152
p = self.make(other)
153153
return self.__class__(self._months - p._months, self._days - p._days)
154154

155-
def __rsub__(self, other: Any) -> Period:
155+
def __rsub__(self, other: Any) -> Self:
156156
return self.make(other) - self
157157

158158
def __gt__(self, other: Any) -> bool:

docs/dates.md

Lines changed: 36 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,49 @@
11
# Dates & Periods
22

3-
The module is shipped with a `date` module for manipulating time periods and
4-
converting dates between different formats. The *period* function can be used
5-
to create `Period` instances:
3+
The `Period` class represents a calendar period composed of months and days.
4+
Use the `period` factory function to create instances from a period string.
5+
6+
## Period strings
7+
8+
A period string combines one or more components, each a number followed by a unit letter:
9+
10+
| Unit | Meaning |
11+
|------|---------|
12+
| `Y` | Years |
13+
| `M` | Months |
14+
| `W` | Weeks |
15+
| `D` | Days |
16+
17+
Examples: `"1Y"`, `"6M"`, `"2W"`, `"3D"`, `"1Y6M"`, `"2W3D"`.
18+
19+
## Usage
620

721
```python
822
import ccy
9-
p = ccy.period("1m")
10-
p
11-
```
1223

13-
```python
14-
p += "2w"
15-
p
24+
p = ccy.period("1Y6M")
25+
str(p) # "1Y6M"
26+
p.years # 1
27+
p.months # 6
28+
p.totaldays # 390
1629
```
1730

31+
Arithmetic with strings or other `Period` objects:
32+
1833
```python
19-
p += "3m"
20-
p
34+
p = ccy.period("1M")
35+
p += "2W" # Period("1M2W")
36+
p += "3M" # Period("4M2W")
37+
p -= "1W" # Period("4M1W")
2138
```
2239

40+
Comparison:
41+
2342
```python
24-
p -= "1w"
25-
p
43+
ccy.period("1Y") > ccy.period("6M") # True
44+
ccy.period("12M") == ccy.period("1Y") # True
2645
```
46+
47+
## Class reference
48+
49+
::: ccy.dates.period.Period

0 commit comments

Comments
 (0)