diff options
author | Gerhard Weis <g.weis@uq.edu.au> | 2021-12-13 08:06:56 +1000 |
---|---|---|
committer | Gerhard Weis <g.weis@uq.edu.au> | 2021-12-13 08:06:56 +1000 |
commit | 07d1602048083415bc22dc72cff152c9c2e0e021 (patch) | |
tree | 6fc5875daf4002f4e8dd4b672da47083aad82b0c /src/isodate/isotime.py | |
parent | 4f36d7e6f6adee4c1ec719bb9beb035df4a7d76c (diff) | |
download | isodate-07d1602048083415bc22dc72cff152c9c2e0e021.tar.gz |
white space
Diffstat (limited to 'src/isodate/isotime.py')
-rw-r--r-- | src/isodate/isotime.py | 116 |
1 files changed, 75 insertions, 41 deletions
diff --git a/src/isodate/isotime.py b/src/isodate/isotime.py index 113d34b..189fc06 100644 --- a/src/isodate/isotime.py +++ b/src/isodate/isotime.py @@ -24,13 +24,13 @@ # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN # CONTRACT, STRICT LIABILITY, OR TORT ############################################################################## -''' +""" This modules provides a method to parse an ISO 8601:2004 time string to a Python datetime.time instance. It supports all basic and extended formats including time zone specifications as described in the ISO standard. -''' +""" import re from decimal import Decimal from datetime import time @@ -44,12 +44,12 @@ TIME_REGEX_CACHE = [] def build_time_regexps(): - ''' + """ Build regular expressions to parse ISO time string. The regular expressions are compiled and stored in TIME_REGEX_CACHE for later reuse. - ''' + """ if not TIME_REGEX_CACHE: # ISO 8601 time representations allow decimal fractions on least # significant time component. Command and Full Stop are both valid @@ -69,32 +69,49 @@ def build_time_regexps(): # isotzinfo.TZ_REGEX # 1. complete time: # hh:mm:ss.ss ... extended format - TIME_REGEX_CACHE.append(re.compile(r"T?(?P<hour>[0-9]{2}):" - r"(?P<minute>[0-9]{2}):" - r"(?P<second>[0-9]{2}" - r"([,.][0-9]+)?)" + TZ_REGEX)) + TIME_REGEX_CACHE.append( + re.compile( + r"T?(?P<hour>[0-9]{2}):" + r"(?P<minute>[0-9]{2}):" + r"(?P<second>[0-9]{2}" + r"([,.][0-9]+)?)" + TZ_REGEX + ) + ) # hhmmss.ss ... basic format - TIME_REGEX_CACHE.append(re.compile(r"T?(?P<hour>[0-9]{2})" - r"(?P<minute>[0-9]{2})" - r"(?P<second>[0-9]{2}" - r"([,.][0-9]+)?)" + TZ_REGEX)) + TIME_REGEX_CACHE.append( + re.compile( + r"T?(?P<hour>[0-9]{2})" + r"(?P<minute>[0-9]{2})" + r"(?P<second>[0-9]{2}" + r"([,.][0-9]+)?)" + TZ_REGEX + ) + ) # 2. reduced accuracy: # hh:mm.mm ... extended format - TIME_REGEX_CACHE.append(re.compile(r"T?(?P<hour>[0-9]{2}):" - r"(?P<minute>[0-9]{2}" - r"([,.][0-9]+)?)" + TZ_REGEX)) + TIME_REGEX_CACHE.append( + re.compile( + r"T?(?P<hour>[0-9]{2}):" + r"(?P<minute>[0-9]{2}" + r"([,.][0-9]+)?)" + TZ_REGEX + ) + ) # hhmm.mm ... basic format - TIME_REGEX_CACHE.append(re.compile(r"T?(?P<hour>[0-9]{2})" - r"(?P<minute>[0-9]{2}" - r"([,.][0-9]+)?)" + TZ_REGEX)) + TIME_REGEX_CACHE.append( + re.compile( + r"T?(?P<hour>[0-9]{2})" + r"(?P<minute>[0-9]{2}" + r"([,.][0-9]+)?)" + TZ_REGEX + ) + ) # hh.hh ... basic format - TIME_REGEX_CACHE.append(re.compile(r"T?(?P<hour>[0-9]{2}" - r"([,.][0-9]+)?)" + TZ_REGEX)) + TIME_REGEX_CACHE.append( + re.compile(r"T?(?P<hour>[0-9]{2}" r"([,.][0-9]+)?)" + TZ_REGEX) + ) return TIME_REGEX_CACHE def parse_time(timestring): - ''' + """ Parses ISO 8601 times into datetime.time objects. Following ISO 8601 formats are supported: @@ -110,7 +127,7 @@ def parse_time(timestring): +-hhmm basic hours and minutes +-hh:mm extended hours and minutes +-hh hours - ''' + """ isotimes = build_time_regexps() for pattern in isotimes: match = pattern.match(timestring) @@ -118,41 +135,58 @@ def parse_time(timestring): groups = match.groupdict() for key, value in groups.items(): if value is not None: - groups[key] = value.replace(',', '.') - tzinfo = build_tzinfo(groups['tzname'], groups['tzsign'], - int(groups['tzhour'] or 0), - int(groups['tzmin'] or 0)) - if 'second' in groups: + groups[key] = value.replace(",", ".") + tzinfo = build_tzinfo( + groups["tzname"], + groups["tzsign"], + int(groups["tzhour"] or 0), + int(groups["tzmin"] or 0), + ) + if "second" in groups: # round to microseconds if fractional seconds are more precise - second = Decimal(groups['second']).quantize(Decimal('.000001')) + second = Decimal(groups["second"]).quantize(Decimal(".000001")) microsecond = (second - int(second)) * int(1e6) # int(...) ... no rounding # to_integral() ... rounding - return time(int(groups['hour']), int(groups['minute']), - int(second), int(microsecond.to_integral()), - tzinfo) - if 'minute' in groups: - minute = Decimal(groups['minute']) + return time( + int(groups["hour"]), + int(groups["minute"]), + int(second), + int(microsecond.to_integral()), + tzinfo, + ) + if "minute" in groups: + minute = Decimal(groups["minute"]) second = (minute - int(minute)) * 60 microsecond = (second - int(second)) * int(1e6) - return time(int(groups['hour']), int(minute), int(second), - int(microsecond.to_integral()), tzinfo) + return time( + int(groups["hour"]), + int(minute), + int(second), + int(microsecond.to_integral()), + tzinfo, + ) else: microsecond, second, minute = 0, 0, 0 - hour = Decimal(groups['hour']) + hour = Decimal(groups["hour"]) minute = (hour - int(hour)) * 60 second = (minute - int(minute)) * 60 microsecond = (second - int(second)) * int(1e6) - return time(int(hour), int(minute), int(second), - int(microsecond.to_integral()), tzinfo) - raise ISO8601Error('Unrecognised ISO 8601 time format: %r' % timestring) + return time( + int(hour), + int(minute), + int(second), + int(microsecond.to_integral()), + tzinfo, + ) + raise ISO8601Error("Unrecognised ISO 8601 time format: %r" % timestring) def time_isoformat(ttime, format=TIME_EXT_COMPLETE + TZ_EXT): - ''' + """ Format time strings. This method is just a wrapper around isodate.isostrf.strftime and uses Time-Extended-Complete with extended time zone as default format. - ''' + """ return strftime(ttime, format) |