summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--openstack/common/timeutils.py13
-rw-r--r--tests/unit/test_timeutils.py7
2 files changed, 20 insertions, 0 deletions
diff --git a/openstack/common/timeutils.py b/openstack/common/timeutils.py
index 8600439..4bf19a5 100644
--- a/openstack/common/timeutils.py
+++ b/openstack/common/timeutils.py
@@ -135,3 +135,16 @@ def unmarshall_time(tyme):
minute=tyme['minute'],
second=tyme['second'],
microsecond=tyme['microsecond'])
+
+
+def delta_seconds(before, after):
+ """
+ Compute the difference in seconds between two date, time, or
+ datetime objects (as a float, to microsecond resolution).
+ """
+ delta = after - before
+ try:
+ return delta.total_seconds()
+ except AttributeError:
+ return ((delta.days * 24 * 3600) + delta.seconds +
+ float(delta.microseconds) / (10 ** 6))
diff --git a/tests/unit/test_timeutils.py b/tests/unit/test_timeutils.py
index 38ac3ed..8236032 100644
--- a/tests/unit/test_timeutils.py
+++ b/tests/unit/test_timeutils.py
@@ -122,6 +122,13 @@ class TimeUtilsTest(unittest.TestCase):
backagain = timeutils.unmarshall_time(binary)
self.assertEqual(now, backagain)
+ def test_delta_seconds(self):
+ before = timeutils.utcnow()
+ after = before + datetime.timedelta(days=7, seconds=59,
+ microseconds=123456)
+ self.assertAlmostEquals(604859.123456,
+ timeutils.delta_seconds(before, after))
+
class TestIso8601Time(unittest.TestCase):