Skip to content

Parse relative weekdays like "next monday" #128

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 25, 2025

Conversation

dan-hipschman
Copy link
Contributor

Fixes #127 and uutils/coreutils#7662. Enables GNU test tests/date/date-next-dow.pl to pass:

$ bash util/run-gnu-test.sh tests/date/date-next-dow.pl
...
PASS: tests/date/date-next-dow.pl
============================================================================
Testsuite summary for GNU coreutils 9.6-dirty
============================================================================
# TOTAL: 1
# PASS:  1
# SKIP:  0
# XFAIL: 0
# FAIL:  0
# XPASS: 0
# ERROR: 0
============================================================================

Note, this also allows some formats that GNU date doesn't accept, but I think they're reasonable:

$ date +"%a %F"
Mon 2025-04-21

$ set d "1 monday ago" ; date -d $d +"%a %F" ; cargo run -q date -d $d +"%a %F"
date: invalid date ‘1 monday ago’
Mon 2025-04-14

$ set d "-1 monday" ; date -d $d +"%a %F" ; cargo run -q date -d $d +"%a %F"
date: invalid date ‘-1 monday’
Mon 2025-04-14

$ set d "+1 monday" ; date -d $d +"%a %F" ; cargo run -q date -d $d +"%a %F"
date: invalid date ‘+1 monday’
Mon 2025-04-28

# Strangely enough, GNU does recognize this format:
$ set d "1 monday" ; date -d $d +"%a %F" ; cargo run -q date -d $d +"%a %F"
Mon 2025-04-28
Mon 2025-04-28

@sylvestre sylvestre requested a review from Copilot April 21, 2025 20:27
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR adds support for parsing relative weekdays (e.g. "next monday") in human-readable time strings, fixing related issues and enabling GNU test cases to pass. Key changes include extending the regex pattern to capture weekday strings, introducing the adjust_for_weekday helper function for calculating the correct date, and adding comprehensive tests for "this", "last", "next", and numbered weekday specifications.

Reviewed Changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.

File Description
src/parse_relative_time.rs Updated regex and parsing logic to capture weekday inputs and added tests for relative weekday manipulations
Cargo.toml Bumped version from 0.8.0 to 0.9.0 to reflect the new feature

@sylvestre
Copy link
Contributor

it is now conflicting, could you please rebase it? thanks

@dan-hipschman dan-hipschman force-pushed the parse-relative-weekdays branch from 33d0670 to acf250a Compare April 22, 2025 16:57
@dan-hipschman dan-hipschman force-pushed the parse-relative-weekdays branch from acf250a to 65fb914 Compare April 22, 2025 17:00
@dan-hipschman
Copy link
Contributor Author

it is now conflicting, could you please rebase it? thanks

Yes, no problem. Rebased.

BTW I noticed a case I missed before, so fixed it and added more tests. The case is when using weekdays, GNU date truncates the time to midnight. This output is with parse_datetime from this PR:

# Preserves time
$ set d "today" ; date -Rd "$d" ; cargo run -q date -Rd "$d"
Tue, 22 Apr 2025 10:05:03 -0700
Tue, 22 Apr 2025 10:05:03 -0700

$ set d "now + 1 day" ; date -Rd "$d" ; cargo run -q date -Rd "$d"
Wed, 23 Apr 2025 10:05:13 -0700
Wed, 23 Apr 2025 10:05:13 -0700

# Truncates time
$ set d "wed" ; date -Rd "$d" ; cargo run -q date -Rd "$d"
Wed, 23 Apr 2025 00:00:00 -0700
Wed, 23 Apr 2025 00:00:00 -0700

$ set d "last wed" ; date -Rd "$d" ; cargo run -q date -Rd "$d"
Wed, 16 Apr 2025 00:00:00 -0700
Wed, 16 Apr 2025 00:00:00 -0700

Confirmed unit tests for parse_datetime and coreutils pass, and GNU test tests/date/date-next-dow.pl still passes.

@cakebaker cakebaker merged commit b9630d4 into uutils:main Apr 25, 2025
14 checks passed
Copy link

codecov bot commented Apr 25, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 0.00%. Comparing base (9b22a18) to head (65fb914).
Report is 2 commits behind head on main.

Additional details and impacted files
@@     Coverage Diff     @@
##   main   #128   +/-   ##
===========================
===========================

☔ View full report in Codecov by Sentry.
📢 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.

@cakebaker
Copy link
Collaborator

Thanks!

@@ -1,7 +1,7 @@
[package]
name = "parse_datetime"
description = "parsing human-readable time strings and converting them to a DateTime"
version = "0.8.0"
version = "0.9.0"
Copy link
Contributor

Choose a reason for hiding this comment

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

i wish that was done with a different commit message and a different PR
i missed it, sorry

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sorry I wasn't sure whether to bump it or let someone else

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.

Relative days of the week are not parsed
3 participants