aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRob Austein <sra@hactrn.net>2009-04-28 18:47:19 +0000
committerRob Austein <sra@hactrn.net>2009-04-28 18:47:19 +0000
commit4975c4d26bcc96c7cdcad60b95ca73a11c4de172 (patch)
tree3b3649c0650a85085e9d2892536b88ff7bd13bcb
parent1ca7907abe51fb31257c52f8a99628ffd7450284 (diff)
Fix handling of arithmetic operations that return timedelta rather
than datetime. svn path=/rpkid/rpki/sundial.py; revision=2367
-rw-r--r--rpkid/rpki/sundial.py19
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):