Revision 26a72a48

b/lib/cli.py
2986 2986
  """
2987 2987
  if not isinstance(ts, (tuple, list)) or len(ts) != 2:
2988 2988
    return "?"
2989
  sec, usec = ts
2990
  return time.strftime("%F %T", time.localtime(sec)) + ".%06d" % usec
2989

  
2990
  (sec, usecs) = ts
2991
  return utils.FormatTime(sec, usecs=usecs)
2991 2992

  
2992 2993

  
2993 2994
def ParseTimespec(value):
b/lib/utils/text.py
412 412
  return ", ".join([str(val) for val in names])
413 413

  
414 414

  
415
def FormatTime(val):
415
def FormatTime(val, usecs=None):
416 416
  """Formats a time value.
417 417

  
418 418
  @type val: float or None
......
423 423
  """
424 424
  if val is None or not isinstance(val, (int, float)):
425 425
    return "N/A"
426

  
426 427
  # these two codes works on Linux, but they are not guaranteed on all
427 428
  # platforms
428
  return time.strftime("%F %T", time.localtime(val))
429
  result = time.strftime("%F %T", time.localtime(val))
430

  
431
  if usecs is not None:
432
    result += ".%06d" % usecs
433

  
434
  return result
429 435

  
430 436

  
431 437
def FormatSeconds(secs):
b/test/ganeti.cli_unittest.py
22 22
"""Script for unittesting the cli module"""
23 23

  
24 24
import unittest
25
import time
25 26
from cStringIO import StringIO
26 27

  
27 28
import ganeti
......
908 909
    self.assertEqual(cl.CountPending(), 0)
909 910

  
910 911

  
912
class TestFormatTimestamp(unittest.TestCase):
913
  def testGood(self):
914
    self.assertEqual(cli.FormatTimestamp((0, 1)),
915
                     time.strftime("%F %T", time.localtime(0)) + ".000001")
916
    self.assertEqual(cli.FormatTimestamp((1332944009, 17376)),
917
                     (time.strftime("%F %T", time.localtime(1332944009)) +
918
                      ".017376"))
919

  
920
  def testWrong(self):
921
    for i in [0, [], {}, "", [1]]:
922
      self.assertEqual(cli.FormatTimestamp(i), "?")
923

  
924

  
911 925
if __name__ == '__main__':
912 926
  testutils.GanetiTestProgram()
b/test/ganeti.utils.text_unittest.py
429 429
  """Testing case for FormatTime"""
430 430

  
431 431
  @staticmethod
432
  def _TestInProcess(tz, timestamp, expected):
432
  def _TestInProcess(tz, timestamp, usecs, expected):
433 433
    os.environ["TZ"] = tz
434 434
    time.tzset()
435
    return utils.FormatTime(timestamp) == expected
435
    return utils.FormatTime(timestamp, usecs=usecs) == expected
436 436

  
437 437
  def _Test(self, *args):
438 438
    # Need to use separate process as we want to change TZ
439 439
    self.assert_(utils.RunInSeparateProcess(self._TestInProcess, *args))
440 440

  
441 441
  def test(self):
442
    self._Test("UTC", 0, "1970-01-01 00:00:00")
443
    self._Test("America/Sao_Paulo", 1292606926, "2010-12-17 15:28:46")
444
    self._Test("Europe/London", 1292606926, "2010-12-17 17:28:46")
445
    self._Test("Europe/Zurich", 1292606926, "2010-12-17 18:28:46")
446
    self._Test("Australia/Sydney", 1292606926, "2010-12-18 04:28:46")
442
    self._Test("UTC", 0, None, "1970-01-01 00:00:00")
443
    self._Test("America/Sao_Paulo", 1292606926, None, "2010-12-17 15:28:46")
444
    self._Test("Europe/London", 1292606926, None, "2010-12-17 17:28:46")
445
    self._Test("Europe/Zurich", 1292606926, None, "2010-12-17 18:28:46")
446
    self._Test("Europe/Zurich", 1332944288, 8787, "2012-03-28 16:18:08.008787")
447
    self._Test("Australia/Sydney", 1292606926, None, "2010-12-18 04:28:46")
448
    self._Test("Australia/Sydney", 1292606926, None, "2010-12-18 04:28:46")
449
    self._Test("Australia/Sydney", 1292606926, 999999,
450
               "2010-12-18 04:28:46.999999")
447 451

  
448 452
  def testNone(self):
449 453
    self.failUnlessEqual(utils.FormatTime(None), "N/A")

Also available in: Unified diff