# HG changeset patch # User Goffi # Date 1661944286 -7200 # Node ID 32087d7c25d40021e551799c47e9542ccd683d50 # Parent 00212260f6590627ca597de46294d6669eb5c92d tools (datetime, utils): fix incorrect use of naive object: `tools.datetime.format_datetime` was expecting aware datetime objects while `tools.utils.xmpp_date` was returning naive datetime objects. A check has been added to the former one, and latter one has been modified to return aware datetime objects. diff -r 00212260f659 -r 32087d7c25d4 sat/tools/datetime.py --- a/sat/tools/datetime.py Tue Aug 23 12:04:11 2022 +0200 +++ b/sat/tools/datetime.py Wed Aug 31 13:11:26 2022 +0200 @@ -19,6 +19,7 @@ # Type-check with `mypy --strict` # Lint with `pylint` +from sat.core import exceptions from datetime import date, datetime, time, timezone import re from typing import Optional, Tuple @@ -95,6 +96,7 @@ ) -> str: """ @param value: The datetime to format. Defaults to the current datetime. + must be an aware datetime object (timezone must be specified) @param include_microsecond: Include the microsecond of the datetime in the output. @return: The datetime formatted according to the DateTime profile specified in XEP-0082. The datetime is always converted to UTC before formatting to avoid @@ -105,11 +107,14 @@ # We format the time in UTC, since the %z formatter of strftime doesn't include colons # to separate hours and minutes which is required by XEP-0082. UTC allows us to put a # simple letter 'Z' as the time zone definition. - value = ( - datetime.now(timezone.utc) - if value is None - else value.astimezone(timezone.utc) # pylint: disable=no-member - ) + if value is not None: + if value.tzinfo is None: + raise exceptions.InternalError( + "an aware datetime object must be used, but a naive one has been provided" + ) + value = value.astimezone(timezone.utc) # pylint: disable=no-member + else: + value = datetime.now(timezone.utc) if include_microsecond: return value.strftime("%Y-%m-%dT%H:%M:%S.%fZ") diff -r 00212260f659 -r 32087d7c25d4 sat/tools/utils.py --- a/sat/tools/utils.py Tue Aug 23 12:04:11 2022 +0200 +++ b/sat/tools/utils.py Wed Aug 31 13:11:26 2022 +0200 @@ -152,8 +152,9 @@ @param with_time(bool): if True include the time @return(unicode): XEP-0082 formatted date and time """ - dtime = datetime.datetime.utcfromtimestamp( - time.time() if timestamp is None else timestamp + dtime = datetime.datetime.fromtimestamp( + time.time() if timestamp is None else timestamp, + datetime.timezone.utc ) return format_datetime(dtime) if with_time else format_date(dtime.date())