Merge cli.FormatTimestamp and utils.FormatTime
authorMichael Hanselmann <hansmi@google.com>
Wed, 28 Mar 2012 14:19:55 +0000 (16:19 +0200)
committerMichael Hanselmann <hansmi@google.com>
Fri, 30 Mar 2012 12:02:39 +0000 (14:02 +0200)
… 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
lib/utils/text.py
test/ganeti.cli_unittest.py
test/ganeti.utils.text_unittest.py

index 87f0493..7947cc6 100644 (file)
@@ -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):
index 0a0e68c..e16a861 100644 (file)
@@ -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):
index b5cbe7c..7185e17 100755 (executable)
@@ -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()
index 5a9af02..b787dc8 100755 (executable)
@@ -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")