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
"""
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):
"""Script for unittesting the cli module"""
import unittest
+import time
from cStringIO import StringIO
import ganeti
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()
"""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")