util: Fix timezone offset

We need to negate the value: the values are in seconds west of UTC, but
ISO 8601 wants the offset to be negative for times behind UTC (i.e. to
the west).

Signed-off-by: Lubomír Sedlář <lsedlar@redhat.com>
This commit is contained in:
Lubomír Sedlář 2017-09-12 09:36:45 +02:00
parent d34c0a2777
commit 9dbf231080
2 changed files with 8 additions and 5 deletions

View File

@ -807,7 +807,10 @@ def 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
# We need to negate the value: the values are in seconds west of UTC, but
# ISO 8601 wants the offset to be negative for times behind UTC (i.e. to
# the west).
offset = -(time.altzone if is_dst else time.timezone)
hours = offset / 3600
minutes = (offset / 60) % 60
return "%+03d:%02d" % (hours, minutes)

View File

@ -597,18 +597,18 @@ class TestTZOffset(unittest.TestCase):
@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")
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")
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.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")