Skip to content

Commit

Permalink
fix: improve date parsing performance
Browse files Browse the repository at this point in the history
- try dateutil parser first as that one works faster on standard dates
- support only English for dateparser as going through all locales is
  super slow (can takes second for a single lookup)
  • Loading branch information
nijel committed Dec 9, 2024
1 parent 7fcea7b commit 2afefb7
Showing 1 changed file with 19 additions and 20 deletions.
39 changes: 19 additions & 20 deletions weblate/utils/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,29 +251,28 @@ def human_date_parse(
second: int = 55,
microsecond: int = 0,
) -> datetime | tuple[datetime, datetime]:
# Lazily import as this can be expensive
from dateparser import parse as dateparser_parse

tzinfo = timezone.get_current_timezone()

# Attempts to parse the text using dateparser
# If the text is unparsable it will return None
result = dateparser_parse(text)
try:
# Here we inject 5:55:55 time and if that was not changed
# during parsing, we assume it was not specified while
# generating the query
result = dateutil_parse(
text,
default=timezone.now().replace(
hour=hour, minute=minute, second=second, microsecond=microsecond
),
)
except ParserError:
# Lazily import as this can be expensive
from dateparser import parse as dateparser_parse

# Attempts to parse the text using dateparser
# If the text is unparsable it will return None
result = dateparser_parse(text, locales=["en"])
if not result:
try:
# Here we inject 5:55:55 time and if that was not changed
# during parsing, we assume it was not specified while
# generating the query
result = dateutil_parse(
text,
default=timezone.now().replace(
hour=hour, minute=minute, second=second, microsecond=microsecond
),
)
except ParserError as error:
raise ValueError(
gettext("Invalid timestamp: {}").format(error)
) from error
msg = "Could not parse timestamp"
raise ValueError(msg)

result = result.replace(
hour=hour,
Expand Down

0 comments on commit 2afefb7

Please sign in to comment.