Mercurial > libervia-backend
diff sat/tools/xmpp_datetime.py @ 3933:cecf45416403
plugin XEP-0373 and XEP-0374: Implementation of OX and OXIM:
GPGME is used as the GPG provider.
rel 374
author | Syndace <me@syndace.dev> |
---|---|
date | Tue, 20 Sep 2022 16:22:18 +0200 |
parents | 8289ac1b34f4 |
children |
line wrap: on
line diff
--- a/sat/tools/xmpp_datetime.py Mon Oct 10 15:23:59 2022 +0200 +++ b/sat/tools/xmpp_datetime.py Tue Sep 20 16:22:18 2022 +0200 @@ -80,12 +80,15 @@ @param value: A string containing date information formatted according to the Date profile specified in XEP-0082. @return: The date parsed from the input string. - @raise ValueError: if the input string is not correctly formatted. + @raise exceptions.ParsingError: if the input string is not correctly formatted. """ # CCYY-MM-DD # The Date profile of XEP-0082 is equal to the ISO 8601 format. - return date.fromisoformat(value) + try: + return date.fromisoformat(value) + except ValueError as e: + raise exceptions.ParsingError() from e def format_datetime( @@ -125,13 +128,16 @@ @param value: A string containing datetime information formatted according to the DateTime profile specified in XEP-0082. @return: The datetime parsed from the input string. - @raise ValueError: if the input string is not correctly formatted. + @raise exceptions.ParsingError: if the input string is not correctly formatted. """ # CCYY-MM-DDThh:mm:ss[.sss]TZD value, microsecond = __parse_fraction_of_a_second(value) - result = datetime.strptime(value, "%Y-%m-%dT%H:%M:%S%z") + try: + result = datetime.strptime(value, "%Y-%m-%dT%H:%M:%S%z") + except ValueError as e: + raise exceptions.ParsingError() from e if microsecond is not None: result = result.replace(microsecond=microsecond) @@ -167,7 +173,7 @@ @param value: A string containing time information formatted according to the Time profile specified in XEP-0082. @return: The time parsed from the input string. - @raise ValueError: if the input string is not correctly formatted. + @raise exceptions.ParsingError: if the input string is not correctly formatted. """ # hh:mm:ss[.sss][TZD] @@ -177,7 +183,10 @@ # profile, except that it doesn't handle the letter Z as time zone information for # UTC. This can be fixed with a simple string replacement of 'Z' with "+00:00", which # is another way to represent UTC. - result = time.fromisoformat(value.replace('Z', "+00:00")) + try: + result = time.fromisoformat(value.replace('Z', "+00:00")) + except ValueError as e: + raise exceptions.ParsingError() from e if microsecond is not None: result = result.replace(microsecond=microsecond)