From 6a425ee8914891e36f92bf6a22e7e23f484606ab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lubom=C3=ADr=20Sedl=C3=A1=C5=99?= Date: Mon, 11 Sep 2017 10:23:07 +0200 Subject: [PATCH] Fix formatting timezone offset MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Displaying the offset in seconds makes very little sense. We should adhere to ISO 8601 format of `+HH:MM` which is much easier to understand. Signed-off-by: Lubomír Sedlář --- bin/pungi-koji | 5 ++--- pungi/util.py | 9 +++++++++ tests/test_util.py | 30 ++++++++++++++++++++++++++++++ 3 files changed, 41 insertions(+), 3 deletions(-) diff --git a/bin/pungi-koji b/bin/pungi-koji index 5475bf4d..79ff331f 100755 --- a/bin/pungi-koji +++ b/bin/pungi-koji @@ -252,6 +252,7 @@ def main(): def run_compose(compose, create_latest_link=True, latest_link_status=None): import pungi.phases import pungi.metadata + import pungi.util errors = [] @@ -262,9 +263,7 @@ def run_compose(compose, create_latest_link=True, latest_link_status=None): compose.log_info("Working directory: %s" % os.getcwd()) compose.log_info("Command line: %s" % " ".join([pipes.quote(arg) for arg in sys.argv])) compose.log_info("Compose top directory: %s" % compose.topdir) - is_dst = time.daylight and time.localtime().tm_isdst > 0 - compose.log_info("Current timezone offset: %s" - % time.altzone if is_dst else time.timezone) + compose.log_info("Current timezone offset: %s" % pungi.util.get_tz_offset()) compose.read_variants() # dump the config file diff --git a/pungi/util.py b/pungi/util.py index c8c9e306..6f456cb9 100644 --- a/pungi/util.py +++ b/pungi/util.py @@ -802,3 +802,12 @@ def retry(timeout=120, interval=30, wait_on=Exception): @retry(wait_on=RuntimeError) def git_ls_remote(baseurl, ref): return run(['git', 'ls-remote', baseurl, ref]) + + +def get_tz_offset(): + """Return a string describing current local timezone offset.""" + is_dst = time.daylight and time.localtime().tm_isdst > 0 + offset = time.altzone if is_dst else time.timezone + hours = offset / 3600 + minutes = (offset / 60) % 60 + return "%+03d:%02d" % (hours, minutes) diff --git a/tests/test_util.py b/tests/test_util.py index 81630505..d40073fb 100644 --- a/tests/test_util.py +++ b/tests/test_util.py @@ -591,5 +591,35 @@ class TestVersionGenerator(unittest.TestCase): self.assertEqual(util.version_generator(compose, None), None) +class TestTZOffset(unittest.TestCase): + @mock.patch('time.daylight', new=False) + @mock.patch('time.altzone', new=7200) + @mock.patch('time.timezone', new=3600) + @mock.patch('time.localtime', new=lambda: mock.Mock(tm_isdst=0)) + def test_zone_without_dst(self): + self.assertEqual(util.get_tz_offset(), "+01:00") + + @mock.patch('time.daylight', new=True) + @mock.patch('time.altzone', new=7200) + @mock.patch('time.timezone', new=3600) + @mock.patch('time.localtime', new=lambda: mock.Mock(tm_isdst=0)) + def test_with_active_dst(self): + self.assertEqual(util.get_tz_offset(), "+01:00") + + @mock.patch('time.daylight', new=True) + @mock.patch('time.altzone', new=9000) + @mock.patch('time.timezone', new=3600) + @mock.patch('time.localtime', new=lambda: mock.Mock(tm_isdst=1)) + def test_with_inactive_dst(self): + self.assertEqual(util.get_tz_offset(), "+02:30") + + @mock.patch('time.daylight', new=False) + @mock.patch('time.altzone', new=0) + @mock.patch('time.timezone', new=0) + @mock.patch('time.localtime', new=lambda: mock.Mock(tm_isdst=0)) + def test_utc(self): + self.assertEqual(util.get_tz_offset(), "+00:00") + + if __name__ == "__main__": unittest.main()