From 26a72a48f197585b6e609d4d550cbc0209de2b51 Mon Sep 17 00:00:00 2001 From: Michael Hanselmann <hansmi@google.com> Date: Wed, 28 Mar 2012 16:19:55 +0200 Subject: [PATCH] Merge cli.FormatTimestamp and utils.FormatTime MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit β¦ to some degree at least. Unittests are included. Signed-off-by: Michael Hanselmann <hansmi@google.com> Reviewed-by: Iustin Pop <iustin@google.com> --- lib/cli.py | 5 +++-- lib/utils/text.py | 10 ++++++++-- test/ganeti.cli_unittest.py | 14 ++++++++++++++ test/ganeti.utils.text_unittest.py | 18 +++++++++++------- 4 files changed, 36 insertions(+), 11 deletions(-) diff --git a/lib/cli.py b/lib/cli.py index 87f049360..7947cc65c 100644 --- a/lib/cli.py +++ b/lib/cli.py @@ -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): diff --git a/lib/utils/text.py b/lib/utils/text.py index 0a0e68ceb..e16a86138 100644 --- a/lib/utils/text.py +++ b/lib/utils/text.py @@ -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): diff --git a/test/ganeti.cli_unittest.py b/test/ganeti.cli_unittest.py index b5cbe7cc8..7185e171d 100755 --- a/test/ganeti.cli_unittest.py +++ b/test/ganeti.cli_unittest.py @@ -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() diff --git a/test/ganeti.utils.text_unittest.py b/test/ganeti.utils.text_unittest.py index 5a9af02e7..b787dc8b5 100755 --- a/test/ganeti.utils.text_unittest.py +++ b/test/ganeti.utils.text_unittest.py @@ -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") -- GitLab