Remove utils.FormatTimestampWithTZ
authorMichael Hanselmann <hansmi@google.com>
Tue, 21 Dec 2010 16:37:25 +0000 (17:37 +0100)
committerMichael Hanselmann <hansmi@google.com>
Tue, 21 Dec 2010 18:14:42 +0000 (19:14 +0100)
Long story short: time.strftime("%Z", time.localtime()) doesn't work,
even though it's documented to be equivalent to time.strftime("%Z").

$ TZ=America/Sao_Paulo python -c 'import time; print
time.strftime("%Z"), time.strftime("%Z", time.localtime())'
BRST LMT

References:
http://bugs.python.org/issue762963
https://bugs.launchpad.net/ubuntu/+source/python2.6/+bug/564607
http://stackoverflow.com/questions/4367896/issue-with-timezone-with-time-strftime

Signed-off-by: Michael Hanselmann <hansmi@google.com>
Reviewed-by: Iustin Pop <iustin@google.com>

lib/utils.py
test/ganeti.utils_unittest.py

index 7c7808f..286ef2b 100644 (file)
@@ -2835,21 +2835,12 @@ def TailFile(fname, lines=20):
   return rows[-lines:]
 
 
-def FormatTimestampWithTZ(secs):
-  """Formats a Unix timestamp with the local timezone.
-
-  @type secs: number
-  @param secs: Seconds since the Epoch (1970-01-01 00:00:00 UTC)
-
-  """
-  return time.strftime("%F %T %Z", time.localtime(secs))
-
-
 def _ParseAsn1Generalizedtime(value):
   """Parses an ASN1 GENERALIZEDTIME timestamp as used by pyOpenSSL.
 
   @type value: string
   @param value: ASN1 GENERALIZEDTIME timestamp
+  @return: Seconds since the Epoch (1970-01-01 00:00:00 UTC)
 
   """
   m = _ASN1_TIME_REGEX.match(value)
@@ -2931,19 +2922,18 @@ def _VerifyCertificateInner(expired, not_before, not_after, now,
 
     if not_before is not None and not_after is not None:
       msg += (" (valid from %s to %s)" %
-              (FormatTimestampWithTZ(not_before),
-               FormatTimestampWithTZ(not_after)))
+              (FormatTime(not_before), FormatTime(not_after)))
     elif not_before is not None:
-      msg += " (valid from %s)" % FormatTimestampWithTZ(not_before)
+      msg += " (valid from %s)" % FormatTime(not_before)
     elif not_after is not None:
-      msg += " (valid until %s)" % FormatTimestampWithTZ(not_after)
+      msg += " (valid until %s)" % FormatTime(not_after)
 
     return (CERT_ERROR, msg)
 
   elif not_before is not None and not_before > now:
     return (CERT_WARNING,
             "Certificate not yet valid (valid from %s)" %
-            FormatTimestampWithTZ(not_before))
+            FormatTime(not_before))
 
   elif not_after is not None:
     remaining_days = int((not_after - now) / (24 * 3600))
index c0b2360..267b4ef 100755 (executable)
@@ -1713,25 +1713,6 @@ class TestFormatTime(unittest.TestCase):
     FormatTime(int(time.time()))
 
 
-class TestFormatTimestampWithTZ(unittest.TestCase):
-  @staticmethod
-  def _TestInProcess(tz, timestamp, expected):
-    os.environ["TZ"] = tz
-    time.tzset()
-    return utils.FormatTimestampWithTZ(timestamp) == 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 UTC")
-    self._Test("America/Sao_Paulo", 1292606926, "2010-12-17 15:28:46 BRST")
-    self._Test("Europe/London", 1292606926, "2010-12-17 17:28:46 GMT")
-    self._Test("Europe/Zurich", 1292606926, "2010-12-17 18:28:46 CET")
-    self._Test("Australia/Sydney", 1292606926, "2010-12-18 04:28:46 EST")
-
-
 class RunInSeparateProcess(unittest.TestCase):
   def test(self):
     for exp in [True, False]: