Skip to content

Parse an hour-only time in relative dates, e.g. "tomorrow at 12" - #1450

Open
AdrianAtZyte wants to merge 3 commits into
scrapinghub:masterfrom
AdrianAtZyte:relative-hour-only
Open

AdrianAtZyte wants to merge 3 commits into
scrapinghub:masterfrom
AdrianAtZyte:relative-hour-only

Conversation

@AdrianAtZyte

Copy link
Copy Markdown
Member

Fixes #1080

@codspeed

codspeed Bot commented Sep 25, 2026 •

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 7 untouched benchmarks


Comparing AdrianAtZyte:relative-hour-only (6833ada) with master (9a266e5)

Open in CodSpeed

@codecov

codecov Bot commented Sep 25, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 98.82%. Comparing base (0961388) to head (6833ada).
⚠️ Report is 6 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #1450   +/-   ##
=======================================
  Coverage   98.81%   98.82%           
=======================================
  Files         237      237           
  Lines        3218     3221    +3     
=======================================
+ Hits         3180     3183    +3     
  Misses         38       38           

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@AdrianAtZyte
AdrianAtZyte marked this pull request as ready for review September 25, 2026 21:38

@serhii73 serhii73 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, tomorrow at 12 (and demain à 14, вчера в 12, morgen um 10) work now. One side effect:
since translation drops at, any bare trailing number becomes the hour. search_dates usually keeps
the count that follows a relative date in listings, so minute/hour offsets get a wrong time:

from datetime import datetime
from dateparser.search import search_dates

S = {"RELATIVE_BASE": datetime(2020, 6, 15, 9, 0)}
for s in ["Posted 5 minutes ago 3 replies", "John Doe 2 hours ago 3 replies",
          "Updated 3 hours ago 20 comments", "2 hours ago 5 likes"]:
    print(s, search_dates(s, settings=S))
input master this branch
Posted 5 minutes ago 3 replies 08:55 03:00
John Doe 2 hours ago 3 replies 07:00 03:00
Updated 3 hours ago 20 comments 06:00 20:00 (in the future)
2 hours ago 5 likes 07:00 05:00

(parse("5 minutes ago 3") gives the same 03:00.) Could the hour-only reading be limited to relative
dates without an hour/minute/second unit? A clock hour contradicts those anyway:

has_clock_units = any(
    unit in ("hour", "minute", "second") for _, unit in PATTERN.findall(date_string)
)
date_string = PATTERN.sub("", date_string)
date_string = re.sub(r"\b(?:ago|in)\b", "", date_string).strip()
if not has_clock_units and re.fullmatch(r"\d{1,2}", date_string) and int(date_string) < 24:
    return time(int(date_string))

With that, the new tests still pass and the cases above give the same results as master.
Posted 3 days ago 15 comments would still get 15:00, but the date stays right.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Incomplete time - missing minutes are effecively making the hour ignored

2 participants