Work around pytz tzname bug

Fixes https://github.com/esphome/issues/issues/445
This commit is contained in:
Otto Winter 2019-07-03 17:13:40 +02:00
parent c6512013bb
commit 85195436c1
No known key found for this signature in database
GPG key ID: DB66C0BE6013F97E

View file

@ -2,6 +2,7 @@ import bisect
import datetime import datetime
import logging import logging
import math import math
import string
import pytz import pytz
import tzlocal import tzlocal
@ -52,8 +53,18 @@ def _tz_dst_str(dt):
_tz_timedelta(td)) _tz_timedelta(td))
def _non_dst_tz(tz, dt): def _safe_tzname(tz, dt):
tzname = tz.tzname(dt) tzname = tz.tzname(dt)
# pytz does not always return valid tznames
# For example: 'Europe/Saratov' returns '+04'
# Work around it by using a generic name for the timezone
if not all(c in string.ascii_letters for c in tzname):
return 'TZ'
return tzname
def _non_dst_tz(tz, dt):
tzname = _safe_tzname(tz, dt)
utcoffset = tz.utcoffset(dt) utcoffset = tz.utcoffset(dt)
_LOGGER.info("Detected timezone '%s' with UTC offset %s", _LOGGER.info("Detected timezone '%s' with UTC offset %s",
tzname, _tz_timedelta(utcoffset)) tzname, _tz_timedelta(utcoffset))