Skip to content

Commit

Permalink
Suppress deprecation warning
Browse files Browse the repository at this point in the history
To be handled once python/cpython#70647 is resolved.
  • Loading branch information
treyhunner committed Nov 12, 2024
1 parent 809676f commit 7faf5ed
Showing 1 changed file with 24 additions and 20 deletions.
44 changes: 24 additions & 20 deletions strptime.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from datetime import datetime
import re
import sys
from warnings import filterwarnings


__version__ = "0.2.0"
Expand Down Expand Up @@ -130,28 +131,31 @@ def make_new_format(format_parts, date_string_parts):
return date_format


def can_parse(date_format, text):
try:
datetime.strptime(text, date_format)
except ValueError:
return False
else:
return True


def detect_format(text):
filterwarnings(
"ignore",
category=DeprecationWarning,
message=r"[\s\S]*https://github.com/python/cpython/issues/70647[\s\S]*",
)
for date_format in specific_custom_formats:
try:
datetime.strptime(text, date_format)
except ValueError:
continue
else:
break
else:
all_parts = [p for p in PARTS_RE.split(text) if p]
significant_parts = len([p for p in all_parts if PARTS_RE.fullmatch(p)])
for format_parts in generic_formats.get(significant_parts, []):
date_format = make_new_format(format_parts, all_parts)
try:
datetime.strptime(text, date_format)
except ValueError:
continue
else:
break
else:
raise ValueError("No valid format found.")
return date_format
if can_parse(date_format, text):
return date_format
all_parts = [p for p in PARTS_RE.split(text) if p]
significant_parts = len([p for p in all_parts if PARTS_RE.fullmatch(p)])
for format_parts in generic_formats.get(significant_parts, []):
date_format = make_new_format(format_parts, all_parts)
if can_parse(date_format, text):
return date_format
raise ValueError("No valid format found.")


def prompt_for_date():
Expand Down

0 comments on commit 7faf5ed

Please sign in to comment.