Skip to content

Commit 04cd8a1

Browse files
committed
fix: format float timezone offsets as integer fields
`altz_to_utctz_str` accepts float seconds, but floor division preserves their float type. Formatting `19800.0` therefore emitted `-5.030.0` rather than Git's `-0530` offset. Compute whole minutes as an integer and split them with `divmod`, preserving existing sign and sub-minute truncation behavior. Add six regressions covering zero, positive/negative offsets and fractional seconds. All six fail before the fix and pass afterward; applicable pre-commit checks pass. Assisted-by: OpenAI Codex (model: unknown, autonomous)
1 parent cfb222f commit 04cd8a1

2 files changed

Lines changed: 12 additions & 2 deletions

File tree

‎git/objects/util.py‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,8 +161,7 @@ def altz_to_utctz_str(altz: float) -> str:
161161
:param altz:
162162
Timezone offset in seconds west of UTC.
163163
"""
164-
hours = abs(altz) // 3600
165-
minutes = (abs(altz) % 3600) // 60
164+
hours, minutes = divmod(int(abs(altz) // 60), 60)
166165
sign = "-" if altz >= 60 else "+"
167166
return "{}{:02}{:02}".format(sign, hours, minutes)
168167

‎test/test_util.py‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,17 @@ def __repr__(self):
280280
return f"{type(self).__name__}({self.name!r})"
281281

282282

283+
@pytest.mark.parametrize(
284+
"offset, expected",
285+
[(0.0, "+0000"), (19800.0, "-0530"), (-19800.0, "+0530"), (34200.9, "-0930"), (60.1, "-0001"), (-59.9, "+0000")],
286+
)
287+
def test_altz_to_utctz_str_accepts_float_seconds(offset, expected):
288+
result = altz_to_utctz_str(offset)
289+
assert result == expected
290+
assert verify_utctz(result) == result
291+
assert utctz_to_altz(result) == utctz_to_altz(expected)
292+
293+
283294
@ddt.ddt
284295
class TestUtils(TestBase):
285296
"""Tests for most utilities in :mod:`git.util`."""

0 commit comments

Comments
 (0)