If the header value is malformed / cannot be parsed, the datetime property is None.
|
def datetime(self) -> _datetime: ... |
For example:
$ python3
Python 3.12.11 (main, Jun 6 2025, 10:48:48) [GCC 14.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from email import message_from_bytes
>>> import email.policy
>>> h = message_from_bytes(b"Date: malformed\r\n", policy=email.policy.default)['date']
>>> type(h)
<class 'email.headerregistry._UniqueDateHeader'>
>>> type(h.datetime)
<class 'NoneType'>
>>> from email import utils
>>> from datetime import datetime, UTC
>>> h = message_from_bytes(f"Date: {utils.format_datetime(datetime.now(tz=UTC))}\r\n".encode("us-ascii"), policy=email.policy.default)['date']
>>> type(h)
<class 'email.headerregistry._UniqueDateHeader'>
>>> h.datetime
datetime.datetime(2025, 9, 26, 0, 43, 53, tzinfo=datetime.timezone.utc)
If the header value is malformed / cannot be parsed, the datetime property is None.
typeshed/stdlib/email/headerregistry.pyi
Line 44 in f97c785
For example: