diff options
-rw-r--r-- | rpkid/rpki/sundial.py | 19 |
1 files changed, 16 insertions, 3 deletions
diff --git a/rpkid/rpki/sundial.py b/rpkid/rpki/sundial.py index 1d7ff8bf..9eeea86e 100644 --- a/rpkid/rpki/sundial.py +++ b/rpkid/rpki/sundial.py @@ -101,12 +101,20 @@ class datetime(pydatetime.datetime): def __add__(self, other): """Force correct class for timedelta results.""" - return self.fromdatetime(pydatetime.datetime.__add__(self, other)) + x = pydatetime.datetime.__add__(self, other) + if isinstance(x, pydatetime.timedelta): + return timedelta.fromtimedelta(x) + else: + return datetime.fromdatetime(x) def __sub__(self, other): """Force correct class for timedelta results.""" - return self.fromdatetime(pydatetime.datetime.__sub__(self, other)) - + x = pydatetime.datetime.__sub__(self, other) + if isinstance(x, pydatetime.timedelta): + return timedelta.fromtimedelta(x) + else: + return datetime.fromdatetime(x) + @classmethod def from_sql(cls, x): """Convert from SQL storage format.""" @@ -177,6 +185,11 @@ class timedelta(pydatetime.timedelta): """Convert a timedelta interval to seconds.""" return self.days * 24 * 60 * 60 + self.seconds + @classmethod + def fromtimedelta(cls, x): + """Convert a datetime.timedelta object into this subclass.""" + return cls(days = x.days, seconds = x.seconds, microseconds = x.microseconds) + if __name__ == "__main__": def test(t): |