From 4975c4d26bcc96c7cdcad60b95ca73a11c4de172 Mon Sep 17 00:00:00 2001 From: Rob Austein Date: Tue, 28 Apr 2009 18:47:19 +0000 Subject: Fix handling of arithmetic operations that return timedelta rather than datetime. svn path=/rpkid/rpki/sundial.py; revision=2367 --- rpkid/rpki/sundial.py | 19 ++++++++++++++++--- 1 file 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): -- cgit v1.2.3