Skip to content

Commit 63a60b3

Browse files
authored
Merge pull request #1547 from jcowgill/non-hour-timezones
Fix timezone parsing functions for non-hour timezones
2 parents cc92d51 + 854a2d1 commit 63a60b3

File tree

2 files changed

+40
-16
lines changed

2 files changed

+40
-16
lines changed

git/objects/util.py

+19-16
Original file line numberDiff line numberDiff line change
@@ -137,22 +137,25 @@ def get_object_type_by_name(
137137

138138

139139
def utctz_to_altz(utctz: str) -> int:
140-
"""we convert utctz to the timezone in seconds, it is the format time.altzone
141-
returns. Git stores it as UTC timezone which has the opposite sign as well,
142-
which explains the -1 * ( that was made explicit here )
143-
144-
:param utctz: git utc timezone string, i.e. +0200"""
145-
return -1 * int(float(utctz) / 100 * 3600)
146-
147-
148-
def altz_to_utctz_str(altz: float) -> str:
149-
"""As above, but inverses the operation, returning a string that can be used
150-
in commit objects"""
151-
utci = -1 * int((float(altz) / 3600) * 100)
152-
utcs = str(abs(utci))
153-
utcs = "0" * (4 - len(utcs)) + utcs
154-
prefix = (utci < 0 and "-") or "+"
155-
return prefix + utcs
140+
"""Convert a git timezone offset into a timezone offset west of
141+
UTC in seconds (compatible with time.altzone).
142+
143+
:param utctz: git utc timezone string, i.e. +0200
144+
"""
145+
int_utctz = int(utctz)
146+
seconds = ((abs(int_utctz) // 100) * 3600 + (abs(int_utctz) % 100) * 60)
147+
return seconds if int_utctz < 0 else -seconds
148+
149+
150+
def altz_to_utctz_str(altz: int) -> str:
151+
"""Convert a timezone offset west of UTC in seconds into a git timezone offset string
152+
153+
:param altz: timezone offset in seconds west of UTC
154+
"""
155+
hours = abs(altz) // 3600
156+
minutes = (abs(altz) % 3600) // 60
157+
sign = "-" if altz >= 60 else "+"
158+
return "{}{:02}{:02}".format(sign, hours, minutes)
156159

157160

158161
def verify_utctz(offset: str) -> str:

test/test_util.py

+21
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,27 @@ def test_iterable_list(self, case):
333333
self.assertRaises(IndexError, ilist.__delitem__, 0)
334334
self.assertRaises(IndexError, ilist.__delitem__, "something")
335335

336+
def test_utctz_to_altz(self):
337+
self.assertEqual(utctz_to_altz("+0000"), 0)
338+
self.assertEqual(utctz_to_altz("+1400"), -(14 * 3600))
339+
self.assertEqual(utctz_to_altz("-1200"), 12 * 3600)
340+
self.assertEqual(utctz_to_altz("+0001"), -60)
341+
self.assertEqual(utctz_to_altz("+0530"), -(5 * 3600 + 1800))
342+
self.assertEqual(utctz_to_altz("-0930"), 9 * 3600 + 1800)
343+
344+
def test_altz_to_utctz_str(self):
345+
self.assertEqual(altz_to_utctz_str(0), "+0000")
346+
self.assertEqual(altz_to_utctz_str(-(14 * 3600)), "+1400")
347+
self.assertEqual(altz_to_utctz_str(12 * 3600), "-1200")
348+
self.assertEqual(altz_to_utctz_str(-60), "+0001")
349+
self.assertEqual(altz_to_utctz_str(-(5 * 3600 + 1800)), "+0530")
350+
self.assertEqual(altz_to_utctz_str(9 * 3600 + 1800), "-0930")
351+
352+
self.assertEqual(altz_to_utctz_str(1), "+0000")
353+
self.assertEqual(altz_to_utctz_str(59), "+0000")
354+
self.assertEqual(altz_to_utctz_str(-1), "+0000")
355+
self.assertEqual(altz_to_utctz_str(-59), "+0000")
356+
336357
def test_from_timestamp(self):
337358
# Correct offset: UTC+2, should return datetime + tzoffset(+2)
338359
altz = utctz_to_altz("+0200")

0 commit comments

Comments
 (0)