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:
parent
d34c0a2777
commit
9dbf231080
@ -807,7 +807,10 @@ def git_ls_remote(baseurl, ref):
|
|||||||
def get_tz_offset():
|
def get_tz_offset():
|
||||||
"""Return a string describing current local timezone offset."""
|
"""Return a string describing current local timezone offset."""
|
||||||
is_dst = time.daylight and time.localtime().tm_isdst > 0
|
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
|
hours = offset / 3600
|
||||||
minutes = (offset / 60) % 60
|
minutes = (offset / 60) % 60
|
||||||
return "%+03d:%02d" % (hours, minutes)
|
return "%+03d:%02d" % (hours, minutes)
|
||||||
|
@ -597,18 +597,18 @@ class TestTZOffset(unittest.TestCase):
|
|||||||
@mock.patch('time.timezone', new=3600)
|
@mock.patch('time.timezone', new=3600)
|
||||||
@mock.patch('time.localtime', new=lambda: mock.Mock(tm_isdst=0))
|
@mock.patch('time.localtime', new=lambda: mock.Mock(tm_isdst=0))
|
||||||
def test_zone_without_dst(self):
|
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.daylight', new=True)
|
||||||
@mock.patch('time.altzone', new=7200)
|
@mock.patch('time.altzone', new=7200)
|
||||||
@mock.patch('time.timezone', new=3600)
|
@mock.patch('time.timezone', new=3600)
|
||||||
@mock.patch('time.localtime', new=lambda: mock.Mock(tm_isdst=0))
|
@mock.patch('time.localtime', new=lambda: mock.Mock(tm_isdst=0))
|
||||||
def test_with_active_dst(self):
|
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.daylight', new=True)
|
||||||
@mock.patch('time.altzone', new=9000)
|
@mock.patch('time.altzone', new=-9000)
|
||||||
@mock.patch('time.timezone', new=3600)
|
@mock.patch('time.timezone', new=-3600)
|
||||||
@mock.patch('time.localtime', new=lambda: mock.Mock(tm_isdst=1))
|
@mock.patch('time.localtime', new=lambda: mock.Mock(tm_isdst=1))
|
||||||
def test_with_inactive_dst(self):
|
def test_with_inactive_dst(self):
|
||||||
self.assertEqual(util.get_tz_offset(), "+02:30")
|
self.assertEqual(util.get_tz_offset(), "+02:30")
|
||||||
|
Loading…
Reference in New Issue
Block a user