summaryrefslogtreecommitdiff
path: root/git/objects/util.py
diff options
context:
space:
mode:
authorJames Cowgill <jcowgill@jcowgill.uk>2023-01-24 12:40:29 +0000
committerJames Cowgill <jcowgill@jcowgill.uk>2023-01-24 12:41:24 +0000
commit854a2d1f7fde596babe85ba9f76f282e9d53086d (patch)
tree05aaf9032c8be41b5b7f7885819ed8078e0064a4 /git/objects/util.py
parentcc92d5126b2fc80f7cc6866fc8f99b3ffb0189ae (diff)
downloadgitpython-854a2d1f7fde596babe85ba9f76f282e9d53086d.tar.gz
Fix timezone parsing functions for non-hour timezones
The `utctz_to_altz` and `altz_to_utctz_str` functions fail to handle timezones with UTC offsets that are not a multiple of one hour. Rewrite them and add some unit tests. Fixes #630
Diffstat (limited to 'git/objects/util.py')
-rw-r--r--git/objects/util.py35
1 files changed, 19 insertions, 16 deletions
diff --git a/git/objects/util.py b/git/objects/util.py
index f405d628..af279154 100644
--- a/git/objects/util.py
+++ b/git/objects/util.py
@@ -137,22 +137,25 @@ def get_object_type_by_name(
def utctz_to_altz(utctz: str) -> int:
- """we convert utctz to the timezone in seconds, it is the format time.altzone
- returns. Git stores it as UTC timezone which has the opposite sign as well,
- which explains the -1 * ( that was made explicit here )
-
- :param utctz: git utc timezone string, i.e. +0200"""
- return -1 * int(float(utctz) / 100 * 3600)
-
-
-def altz_to_utctz_str(altz: float) -> str:
- """As above, but inverses the operation, returning a string that can be used
- in commit objects"""
- utci = -1 * int((float(altz) / 3600) * 100)
- utcs = str(abs(utci))
- utcs = "0" * (4 - len(utcs)) + utcs
- prefix = (utci < 0 and "-") or "+"
- return prefix + utcs
+ """Convert a git timezone offset into a timezone offset west of
+ UTC in seconds (compatible with time.altzone).
+
+ :param utctz: git utc timezone string, i.e. +0200
+ """
+ int_utctz = int(utctz)
+ seconds = ((abs(int_utctz) // 100) * 3600 + (abs(int_utctz) % 100) * 60)
+ return seconds if int_utctz < 0 else -seconds
+
+
+def altz_to_utctz_str(altz: int) -> str:
+ """Convert a timezone offset west of UTC in seconds into a git timezone offset string
+
+ :param altz: timezone offset in seconds west of UTC
+ """
+ hours = abs(altz) // 3600
+ minutes = (abs(altz) % 3600) // 60
+ sign = "-" if altz >= 60 else "+"
+ return "{}{:02}{:02}".format(sign, hours, minutes)
def verify_utctz(offset: str) -> str: