XEP-0203: Prevent naïve datetime from being passed

The specification says “The format MUST adhere to the dateTime format
specified in XEP-0082 and MUST be expressed in UTC.”

We now respect this requirement, by rejecting naïve datetimes with a
ValueError exception, and converting the passed datetime to UTC.

Fixes #3471.
This commit is contained in:
Emmanuel Gil Peyrot 2022-07-12 13:11:24 +02:00
parent 5e5a741994
commit 65d70fe417

View File

@ -30,6 +30,10 @@ class Delay(ElementBase):
def set_stamp(self, value):
if isinstance(value, dt.datetime):
if value.tzinfo is None:
raise ValueError(f'Datetime provided without timezone information: {value}')
if value.tzinfo != dt.timezone.utc:
value = value.astimezone(dt.timezone.utc)
value = xep_0082.format_datetime(value)
self._set_attr('stamp', value)