From 0f9294f71bf84a7061b1fc4d404d9e3d228cc531 Mon Sep 17 00:00:00 2001 From: Michael Hanselmann <hansmi@google.com> Date: Tue, 21 Dec 2010 17:37:25 +0100 Subject: [PATCH] Remove utils.FormatTimestampWithTZ 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 | 20 +++++--------------- test/ganeti.utils_unittest.py | 19 ------------------- 2 files changed, 5 insertions(+), 34 deletions(-) diff --git a/lib/utils.py b/lib/utils.py index 7c7808f26..286ef2b23 100644 --- a/lib/utils.py +++ b/lib/utils.py @@ -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)) diff --git a/test/ganeti.utils_unittest.py b/test/ganeti.utils_unittest.py index c0b236079..267b4ef45 100755 --- a/test/ganeti.utils_unittest.py +++ b/test/ganeti.utils_unittest.py @@ -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]: -- GitLab