2 hours ago · Tech · hide · 0 comments

Take this code, parsing the date of an annually recurring event that only has a month and day, such as a work anniversary: from datetime import datetime def parse_anniversary(date_str: str) -> datetime: parsed = datetime.strptime(date_str, "%d/%m") return parsed.day, parsed.month With Python 3.13 or 3.14, run it on January 1st, and it will log a warning, but still parse the date: >>> parse_anniversary("01/01") /.../example.py:7: DeprecationWarning: Parsing dates involving a day of month without a year specified is ambiguous and fails to parse leap day. The default behavior will change in Python 3.15 to either always raise an exception or to use a different default year (TBD). To avoid trouble, add a specific year to the input & format. See https://github.com/python/cpython/issues/70647. parsed = datetime.strptime(date_str, "%d/%m") (1, 1) Note the returned value’s year is 1900. But, parse February 29th, and the warning is followed by a ValueError: >>> parse_anniversary("29/02")…

No comments yet. Log in to reply on the Fediverse. Comments will appear here.