Revision 9f37f689
b/lib/utils.py | ||
---|---|---|
1680 | 1680 |
def TimestampForFilename(): |
1681 | 1681 |
"""Returns the current time formatted for filenames. |
1682 | 1682 |
|
1683 |
The format doesn't contain colons as some shells and applications them as
|
|
1684 |
separators.
|
|
1683 |
The format doesn't contain colons as some shells and applications treat them
|
|
1684 |
as separators. Uses the local timezone.
|
|
1685 | 1685 |
|
1686 | 1686 |
""" |
1687 | 1687 |
return time.strftime("%Y-%m-%d_%H_%M_%S") |
... | ... | |
2838 | 2838 |
def FormatTimestampWithTZ(secs): |
2839 | 2839 |
"""Formats a Unix timestamp with the local timezone. |
2840 | 2840 |
|
2841 |
@type secs: number |
|
2842 |
@param secs: Seconds since the Epoch (1970-01-01 00:00:00 UTC) |
|
2843 |
|
|
2841 | 2844 |
""" |
2842 |
return time.strftime("%F %T %Z", time.gmtime(secs))
|
|
2845 |
return time.strftime("%F %T %Z", time.localtime(secs))
|
|
2843 | 2846 |
|
2844 | 2847 |
|
2845 | 2848 |
def _ParseAsn1Generalizedtime(value): |
... | ... | |
3366 | 3369 |
"""Formats a time value. |
3367 | 3370 |
|
3368 | 3371 |
@type val: float or None |
3369 |
@param val: the timestamp as returned by time.time() |
|
3372 |
@param val: Timestamp as returned by time.time() (seconds since Epoch, |
|
3373 |
1970-01-01 00:00:00 UTC) |
|
3370 | 3374 |
@return: a string value or N/A if we don't have a valid timestamp |
3371 | 3375 |
|
3372 | 3376 |
""" |
b/test/ganeti.utils_unittest.py | ||
---|---|---|
1682 | 1682 |
class TestFormatTime(unittest.TestCase): |
1683 | 1683 |
"""Testing case for FormatTime""" |
1684 | 1684 |
|
1685 |
@staticmethod |
|
1686 |
def _TestInProcess(tz, timestamp, expected): |
|
1687 |
os.environ["TZ"] = tz |
|
1688 |
time.tzset() |
|
1689 |
return utils.FormatTime(timestamp) == expected |
|
1690 |
|
|
1691 |
def _Test(self, *args): |
|
1692 |
# Need to use separate process as we want to change TZ |
|
1693 |
self.assert_(utils.RunInSeparateProcess(self._TestInProcess, *args)) |
|
1694 |
|
|
1695 |
def test(self): |
|
1696 |
self._Test("UTC", 0, "1970-01-01 00:00:00") |
|
1697 |
self._Test("America/Sao_Paulo", 1292606926, "2010-12-17 15:28:46") |
|
1698 |
self._Test("Europe/London", 1292606926, "2010-12-17 17:28:46") |
|
1699 |
self._Test("Europe/Zurich", 1292606926, "2010-12-17 18:28:46") |
|
1700 |
self._Test("Australia/Sydney", 1292606926, "2010-12-18 04:28:46") |
|
1701 |
|
|
1685 | 1702 |
def testNone(self): |
1686 | 1703 |
self.failUnlessEqual(FormatTime(None), "N/A") |
1687 | 1704 |
|
... | ... | |
1695 | 1712 |
FormatTime(int(time.time())) |
1696 | 1713 |
|
1697 | 1714 |
|
1715 |
class TestFormatTimestampWithTZ(unittest.TestCase): |
|
1716 |
@staticmethod |
|
1717 |
def _TestInProcess(tz, timestamp, expected): |
|
1718 |
os.environ["TZ"] = tz |
|
1719 |
time.tzset() |
|
1720 |
return utils.FormatTimestampWithTZ(timestamp) == expected |
|
1721 |
|
|
1722 |
def _Test(self, *args): |
|
1723 |
# Need to use separate process as we want to change TZ |
|
1724 |
self.assert_(utils.RunInSeparateProcess(self._TestInProcess, *args)) |
|
1725 |
|
|
1726 |
def test(self): |
|
1727 |
self._Test("UTC", 0, "1970-01-01 00:00:00 UTC") |
|
1728 |
self._Test("America/Sao_Paulo", 1292606926, "2010-12-17 15:28:46 BRST") |
|
1729 |
self._Test("Europe/London", 1292606926, "2010-12-17 17:28:46 GMT") |
|
1730 |
self._Test("Europe/Zurich", 1292606926, "2010-12-17 18:28:46 CET") |
|
1731 |
self._Test("Australia/Sydney", 1292606926, "2010-12-18 04:28:46 EST") |
|
1732 |
|
|
1733 |
|
|
1698 | 1734 |
class RunInSeparateProcess(unittest.TestCase): |
1699 | 1735 |
def test(self): |
1700 | 1736 |
for exp in [True, False]: |
Also available in: Unified diff