Revision 0f9294f7
b/lib/utils.py | ||
---|---|---|
2835 | 2835 |
return rows[-lines:] |
2836 | 2836 |
|
2837 | 2837 |
|
2838 |
def FormatTimestampWithTZ(secs): |
|
2839 |
"""Formats a Unix timestamp with the local timezone. |
|
2840 |
|
|
2841 |
@type secs: number |
|
2842 |
@param secs: Seconds since the Epoch (1970-01-01 00:00:00 UTC) |
|
2843 |
|
|
2844 |
""" |
|
2845 |
return time.strftime("%F %T %Z", time.localtime(secs)) |
|
2846 |
|
|
2847 |
|
|
2848 | 2838 |
def _ParseAsn1Generalizedtime(value): |
2849 | 2839 |
"""Parses an ASN1 GENERALIZEDTIME timestamp as used by pyOpenSSL. |
2850 | 2840 |
|
2851 | 2841 |
@type value: string |
2852 | 2842 |
@param value: ASN1 GENERALIZEDTIME timestamp |
2843 |
@return: Seconds since the Epoch (1970-01-01 00:00:00 UTC) |
|
2853 | 2844 |
|
2854 | 2845 |
""" |
2855 | 2846 |
m = _ASN1_TIME_REGEX.match(value) |
... | ... | |
2931 | 2922 |
|
2932 | 2923 |
if not_before is not None and not_after is not None: |
2933 | 2924 |
msg += (" (valid from %s to %s)" % |
2934 |
(FormatTimestampWithTZ(not_before), |
|
2935 |
FormatTimestampWithTZ(not_after))) |
|
2925 |
(FormatTime(not_before), FormatTime(not_after))) |
|
2936 | 2926 |
elif not_before is not None: |
2937 |
msg += " (valid from %s)" % FormatTimestampWithTZ(not_before)
|
|
2927 |
msg += " (valid from %s)" % FormatTime(not_before) |
|
2938 | 2928 |
elif not_after is not None: |
2939 |
msg += " (valid until %s)" % FormatTimestampWithTZ(not_after)
|
|
2929 |
msg += " (valid until %s)" % FormatTime(not_after) |
|
2940 | 2930 |
|
2941 | 2931 |
return (CERT_ERROR, msg) |
2942 | 2932 |
|
2943 | 2933 |
elif not_before is not None and not_before > now: |
2944 | 2934 |
return (CERT_WARNING, |
2945 | 2935 |
"Certificate not yet valid (valid from %s)" % |
2946 |
FormatTimestampWithTZ(not_before))
|
|
2936 |
FormatTime(not_before)) |
|
2947 | 2937 |
|
2948 | 2938 |
elif not_after is not None: |
2949 | 2939 |
remaining_days = int((not_after - now) / (24 * 3600)) |
b/test/ganeti.utils_unittest.py | ||
---|---|---|
1713 | 1713 |
FormatTime(int(time.time())) |
1714 | 1714 |
|
1715 | 1715 |
|
1716 |
class TestFormatTimestampWithTZ(unittest.TestCase): |
|
1717 |
@staticmethod |
|
1718 |
def _TestInProcess(tz, timestamp, expected): |
|
1719 |
os.environ["TZ"] = tz |
|
1720 |
time.tzset() |
|
1721 |
return utils.FormatTimestampWithTZ(timestamp) == expected |
|
1722 |
|
|
1723 |
def _Test(self, *args): |
|
1724 |
# Need to use separate process as we want to change TZ |
|
1725 |
self.assert_(utils.RunInSeparateProcess(self._TestInProcess, *args)) |
|
1726 |
|
|
1727 |
def test(self): |
|
1728 |
self._Test("UTC", 0, "1970-01-01 00:00:00 UTC") |
|
1729 |
self._Test("America/Sao_Paulo", 1292606926, "2010-12-17 15:28:46 BRST") |
|
1730 |
self._Test("Europe/London", 1292606926, "2010-12-17 17:28:46 GMT") |
|
1731 |
self._Test("Europe/Zurich", 1292606926, "2010-12-17 18:28:46 CET") |
|
1732 |
self._Test("Australia/Sydney", 1292606926, "2010-12-18 04:28:46 EST") |
|
1733 |
|
|
1734 |
|
|
1735 | 1716 |
class RunInSeparateProcess(unittest.TestCase): |
1736 | 1717 |
def test(self): |
1737 | 1718 |
for exp in [True, False]: |
Also available in: Unified diff