You can use the time.timezone
and time.altzone
values.
These provide:
The offset of the local (non-DST) timezone, in seconds west of UTC (negative in most of Western Europe, positive in the US, zero in the UK)
and
The offset of the local DST timezone, in seconds west of UTC, if one is defined. This is negative if the local DST timezone is east of UTC (as in Western Europe, including the UK). Only use this if
daylight
is nonzero.
As the documentation for time.altzone
explains, use the latter when time.daylight
is non-zero, but only if the current time is in summer time:
import time
def my_timezone():
is_dst = time.daylight and time.localtime().tm_isdst
offset = time.altzone if is_dst else time.timezone
westerly = offset > 0
minutes, seconds = divmod(abs(offset), 60)
hours, minutes = divmod(minutes, 60)
return '{}{:02d}{:02d}'.format('-' if westerly else '+', hours, minutes)
Using seconds as a base and divmod()
there should be no rounding errors here.
For my British summertime location, that gives:
>>> my_timezone()
'+0100'