Skip to content

Commit 163c9fb

Browse files
committed
fix hday.today() type
1 parent cddcc68 commit 163c9fb

2 files changed

Lines changed: 21 additions & 0 deletions

File tree

src/hamster/lib/datetime.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,21 @@ class date(pdt.date):
4646
def __new__(cls, year, month, day):
4747
return pdt.date.__new__(cls, year, month, day)
4848

49+
def __add__(self, other):
50+
# python date.__add__ was not type stable prior to 3.8
51+
return self.from_pdt(self.to_pdt() + other)
52+
53+
__radd__ = __add__
54+
55+
def __sub__(self, other):
56+
# python date.__sub__ was not type stable prior to 3.8
57+
if isinstance(other, timedelta):
58+
return self.from_pdt(self.to_pdt() - other)
59+
elif isinstance(other, date):
60+
return timedelta.from_pdt(self.to_pdt() - other)
61+
else:
62+
raise NotImplementedError("subtract {}".format(type(other)))
63+
4964
@classmethod
5065
def parse(cls, s):
5166
"""Return date from string."""
@@ -73,6 +88,10 @@ def from_pdt(cls, d):
7388
"""Convert python date to hamster date."""
7489
return cls(d.year, d.month, d.day)
7590

91+
def to_pdt(self):
92+
"""Convert to python date."""
93+
return pdt.date(self.year, self.month, self.day)
94+
7695
# For datetime that will need to be outside the class.
7796
# Same here for consistency
7897
date.re = re.compile(date.pattern(), flags=re.VERBOSE)

tests/stuff_test.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,8 @@ def test_datetime_hday(self):
281281
date_time = dt.datetime(2018, 8, 14, 0, 10) # 2018-08-14 0:10
282282
expected = dt.date(2018, 8, 13)
283283
self.assertEqual(date_time.hday(), expected)
284+
today = dt.hday.today()
285+
self.assertEqual(type(today), dt.hday)
284286

285287
def test_parse_date(self):
286288
date = dt.date.parse("2020-01-05")

0 commit comments

Comments
 (0)