@@ -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
7897date .re = re .compile (date .pattern (), flags = re .VERBOSE )
0 commit comments