Commit 739be818 authored by Michael Hanselmann's avatar Michael Hanselmann
Browse files

Add functions to split time into tuple and merge it back

These will be used for job logs.

Reviewed-by: ultrotter
parent 8bb66978
......@@ -1161,6 +1161,34 @@ def CheckVolumeGroupSize(vglist, vgname, minsize):
return None
def SplitTime(seconds):
"""Splits time as floating point number into a tuple.
@param seconds: Time in seconds
@type seconds: int or float
@return: Tuple containing (seconds, milliseconds)
"""
(seconds, fraction) = divmod(seconds, 1.0)
return (int(seconds), int(round(fraction * 1000, 0)))
def MergeTime(timetuple):
"""Merges a tuple into time as a floating point number.
@param timetuple: Time as tuple, (seconds, milliseconds)
@type timetuple: tuple
@return: Time as a floating point number expressed in seconds
"""
(seconds, milliseconds) = timetuple
assert 0 <= seconds, "Seconds must be larger than 0"
assert 0 <= milliseconds <= 999, "Milliseconds must be 0-999"
return float(seconds) + (float(1) / 1000 * milliseconds)
def LockedMethod(fn):
"""Synchronized object access decorator.
......
......@@ -778,5 +778,27 @@ class TestFileLock(unittest.TestCase):
self.assertRaises(AssertionError, self.lock.Unlock, blocking=False)
class TestTimeFunctions(unittest.TestCase):
"""Test case for time functions"""
def runTest(self):
self.assertEqual(utils.SplitTime(1), (1, 0))
self.assertEqual(utils.SplitTime(1.5), (1, 500))
self.assertEqual(utils.SplitTime(1218448917.4809151), (1218448917, 481))
self.assertEqual(utils.MergeTime((1, 0)), 1.0)
self.assertEqual(utils.MergeTime((1, 500)), 1.5)
self.assertEqual(utils.MergeTime((1218448917, 500)), 1218448917.5)
self.assertEqual(round(utils.MergeTime((1218448917, 481)), 3), 1218448917.481)
self.assertEqual(round(utils.MergeTime((1, 801)), 3), 1.801)
self.assertRaises(AssertionError, utils.MergeTime, (0, -1))
self.assertRaises(AssertionError, utils.MergeTime, (0, 1000))
self.assertRaises(AssertionError, utils.MergeTime, (0, 9999))
self.assertRaises(AssertionError, utils.MergeTime, (-1, 0))
self.assertRaises(AssertionError, utils.MergeTime, (-9999, 0))
if __name__ == '__main__':
unittest.main()
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment