Commit 26a72a48 authored by Michael Hanselmann's avatar Michael Hanselmann
Browse files

Merge cli.FormatTimestamp and utils.FormatTime



… to some degree at least. Unittests are included.
Signed-off-by: default avatarMichael Hanselmann <hansmi@google.com>
Reviewed-by: default avatarIustin Pop <iustin@google.com>
parent 0b04b188
......@@ -2986,8 +2986,9 @@ def FormatTimestamp(ts):
"""
if not isinstance(ts, (tuple, list)) or len(ts) != 2:
return "?"
sec, usec = ts
return time.strftime("%F %T", time.localtime(sec)) + ".%06d" % usec
(sec, usecs) = ts
return utils.FormatTime(sec, usecs=usecs)
def ParseTimespec(value):
......
......@@ -412,7 +412,7 @@ def CommaJoin(names):
return ", ".join([str(val) for val in names])
def FormatTime(val):
def FormatTime(val, usecs=None):
"""Formats a time value.
@type val: float or None
......@@ -423,9 +423,15 @@ def FormatTime(val):
"""
if val is None or not isinstance(val, (int, float)):
return "N/A"
# these two codes works on Linux, but they are not guaranteed on all
# platforms
return time.strftime("%F %T", time.localtime(val))
result = time.strftime("%F %T", time.localtime(val))
if usecs is not None:
result += ".%06d" % usecs
return result
def FormatSeconds(secs):
......
......@@ -22,6 +22,7 @@
"""Script for unittesting the cli module"""
import unittest
import time
from cStringIO import StringIO
import ganeti
......@@ -908,5 +909,18 @@ class TestGetOnlineNodes(unittest.TestCase):
self.assertEqual(cl.CountPending(), 0)
class TestFormatTimestamp(unittest.TestCase):
def testGood(self):
self.assertEqual(cli.FormatTimestamp((0, 1)),
time.strftime("%F %T", time.localtime(0)) + ".000001")
self.assertEqual(cli.FormatTimestamp((1332944009, 17376)),
(time.strftime("%F %T", time.localtime(1332944009)) +
".017376"))
def testWrong(self):
for i in [0, [], {}, "", [1]]:
self.assertEqual(cli.FormatTimestamp(i), "?")
if __name__ == '__main__':
testutils.GanetiTestProgram()
......@@ -429,21 +429,25 @@ class TestFormatTime(unittest.TestCase):
"""Testing case for FormatTime"""
@staticmethod
def _TestInProcess(tz, timestamp, expected):
def _TestInProcess(tz, timestamp, usecs, expected):
os.environ["TZ"] = tz
time.tzset()
return utils.FormatTime(timestamp) == expected
return utils.FormatTime(timestamp, usecs=usecs) == expected
def _Test(self, *args):
# Need to use separate process as we want to change TZ
self.assert_(utils.RunInSeparateProcess(self._TestInProcess, *args))
def test(self):
self._Test("UTC", 0, "1970-01-01 00:00:00")
self._Test("America/Sao_Paulo", 1292606926, "2010-12-17 15:28:46")
self._Test("Europe/London", 1292606926, "2010-12-17 17:28:46")
self._Test("Europe/Zurich", 1292606926, "2010-12-17 18:28:46")
self._Test("Australia/Sydney", 1292606926, "2010-12-18 04:28:46")
self._Test("UTC", 0, None, "1970-01-01 00:00:00")
self._Test("America/Sao_Paulo", 1292606926, None, "2010-12-17 15:28:46")
self._Test("Europe/London", 1292606926, None, "2010-12-17 17:28:46")
self._Test("Europe/Zurich", 1292606926, None, "2010-12-17 18:28:46")
self._Test("Europe/Zurich", 1332944288, 8787, "2012-03-28 16:18:08.008787")
self._Test("Australia/Sydney", 1292606926, None, "2010-12-18 04:28:46")
self._Test("Australia/Sydney", 1292606926, None, "2010-12-18 04:28:46")
self._Test("Australia/Sydney", 1292606926, 999999,
"2010-12-18 04:28:46.999999")
def testNone(self):
self.failUnlessEqual(utils.FormatTime(None), "N/A")
......
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