Skip to content

Commit

Permalink
Merge pull request #519 from ederag/fix-hday-today-type
Browse files Browse the repository at this point in the history
Fix hday.today() type
  • Loading branch information
ederag authored Jan 24, 2020
2 parents cddcc68 + 163c9fb commit a572ddc
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/hamster/lib/datetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,21 @@ class date(pdt.date):
def __new__(cls, year, month, day):
return pdt.date.__new__(cls, year, month, day)

def __add__(self, other):
# python date.__add__ was not type stable prior to 3.8
return self.from_pdt(self.to_pdt() + other)

__radd__ = __add__

def __sub__(self, other):
# python date.__sub__ was not type stable prior to 3.8
if isinstance(other, timedelta):
return self.from_pdt(self.to_pdt() - other)
elif isinstance(other, date):
return timedelta.from_pdt(self.to_pdt() - other)
else:
raise NotImplementedError("subtract {}".format(type(other)))

@classmethod
def parse(cls, s):
"""Return date from string."""
Expand Down Expand Up @@ -73,6 +88,10 @@ def from_pdt(cls, d):
"""Convert python date to hamster date."""
return cls(d.year, d.month, d.day)

def to_pdt(self):
"""Convert to python date."""
return pdt.date(self.year, self.month, self.day)

# For datetime that will need to be outside the class.
# Same here for consistency
date.re = re.compile(date.pattern(), flags=re.VERBOSE)
Expand Down
2 changes: 2 additions & 0 deletions tests/stuff_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,8 @@ def test_datetime_hday(self):
date_time = dt.datetime(2018, 8, 14, 0, 10) # 2018-08-14 0:10
expected = dt.date(2018, 8, 13)
self.assertEqual(date_time.hday(), expected)
today = dt.hday.today()
self.assertEqual(type(today), dt.hday)

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

0 comments on commit a572ddc

Please sign in to comment.