There is a good answer here fooobar.com/questions/1723693 / ... (later duplicate of this question)
However, if you are dealing with time zone offsets in a date and time string, you also need to handle negative hours, in which case zero padding in Martijn's answer does not work.
for example, he will return -4:00instead-04:00
To fix this, the code gets a little longer, as shown below:
offset_h, offset_m = divmod(offset_minutes, 60)
sign = '-' if offset_h < 0 else '+'
offset_str = '{}{:02d}{:02d}'.format(sign, abs(offset_h), offset_m)
source
share