diff options
Diffstat (limited to 'src/isodate/isotzinfo.py')
-rw-r--r-- | src/isodate/isotzinfo.py | 62 |
1 files changed, 33 insertions, 29 deletions
diff --git a/src/isodate/isotzinfo.py b/src/isodate/isotzinfo.py index a805ebe..d096dd3 100644 --- a/src/isodate/isotzinfo.py +++ b/src/isodate/isotzinfo.py @@ -24,41 +24,42 @@ # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN # CONTRACT, STRICT LIABILITY, OR TORT ############################################################################## -''' +""" This module provides an ISO 8601:2004 time zone info parser. It offers a function to parse the time zone offset as specified by ISO 8601. -''' +""" import re from isodate.isoerror import ISO8601Error from isodate.tzinfo import UTC, FixedOffset, ZERO -TZ_REGEX = r"(?P<tzname>(Z|(?P<tzsign>[+-])"\ - r"(?P<tzhour>[0-9]{2})(:?(?P<tzmin>[0-9]{2}))?)?)" +TZ_REGEX = ( + r"(?P<tzname>(Z|(?P<tzsign>[+-])" r"(?P<tzhour>[0-9]{2})(:?(?P<tzmin>[0-9]{2}))?)?)" +) TZ_RE = re.compile(TZ_REGEX) -def build_tzinfo(tzname, tzsign='+', tzhour=0, tzmin=0): - ''' +def build_tzinfo(tzname, tzsign="+", tzhour=0, tzmin=0): + """ create a tzinfo instance according to given parameters. tzname: 'Z' ... return UTC '' | None ... return None other ... return FixedOffset - ''' - if tzname is None or tzname == '': + """ + if tzname is None or tzname == "": return None - if tzname == 'Z': + if tzname == "Z": return UTC - tzsign = ((tzsign == '-') and -1) or 1 + tzsign = ((tzsign == "-") and -1) or 1 return FixedOffset(tzsign * tzhour, tzsign * tzmin, tzname) def parse_tzinfo(tzstring): - ''' + """ Parses ISO 8601 time zone designators to tzinfo objecs. A time zone designator can be in the following format: @@ -67,18 +68,21 @@ def parse_tzinfo(tzstring): +-hhmm basic hours and minutes +-hh:mm extended hours and minutes +-hh hours - ''' + """ match = TZ_RE.match(tzstring) if match: groups = match.groupdict() - return build_tzinfo(groups['tzname'], groups['tzsign'], - int(groups['tzhour'] or 0), - int(groups['tzmin'] or 0)) - raise ISO8601Error('%s not a valid time zone info' % tzstring) + return build_tzinfo( + groups["tzname"], + groups["tzsign"], + int(groups["tzhour"] or 0), + int(groups["tzmin"] or 0), + ) + raise ISO8601Error("%s not a valid time zone info" % tzstring) -def tz_isoformat(dt, format='%Z'): - ''' +def tz_isoformat(dt, format="%Z"): + """ return time zone offset ISO 8601 formatted. The various ISO formats can be chosen with the format parameter. @@ -89,24 +93,24 @@ def tz_isoformat(dt, format='%Z'): %h ... +-HH %z ... +-HHMM %Z ... +-HH:MM - ''' + """ tzinfo = dt.tzinfo if (tzinfo is None) or (tzinfo.utcoffset(dt) is None): - return '' + return "" if tzinfo.utcoffset(dt) == ZERO and tzinfo.dst(dt) == ZERO: - return 'Z' + return "Z" tdelta = tzinfo.utcoffset(dt) seconds = tdelta.days * 24 * 60 * 60 + tdelta.seconds - sign = ((seconds < 0) and '-') or '+' + sign = ((seconds < 0) and "-") or "+" seconds = abs(seconds) minutes, seconds = divmod(seconds, 60) hours, minutes = divmod(minutes, 60) if hours > 99: - raise OverflowError('can not handle differences > 99 hours') - if format == '%Z': - return '%s%02d:%02d' % (sign, hours, minutes) - elif format == '%z': - return '%s%02d%02d' % (sign, hours, minutes) - elif format == '%h': - return '%s%02d' % (sign, hours) + raise OverflowError("can not handle differences > 99 hours") + if format == "%Z": + return "%s%02d:%02d" % (sign, hours, minutes) + elif format == "%z": + return "%s%02d%02d" % (sign, hours, minutes) + elif format == "%h": + return "%s%02d" % (sign, hours) raise ValueError('unknown format string "%s"' % format) |