Skip to content

fallible offset conversion #145

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
May 26, 2025

Conversation

yuankunzhang
Copy link
Contributor

No description provided.

@sylvestre
Copy link
Contributor

please add test to make sure it doesn't happen in the future, thanks!

@yuankunzhang yuankunzhang force-pushed the fallible-offset-conversion branch from e96aedd to ab91b2b Compare May 20, 2025 13:30
#[test]
fn offset_overflow() {
assert!(parse_datetime("m+12").is_err());
assert!(parse_datetime("24:00").is_err());
Copy link
Contributor

Choose a reason for hiding this comment

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

In another PR I had the fuzzer fail on "m1y-12".

Maybe worth adding?
assert!(parse_datetime("m1y-12").is_ok());

(-12 is an acceptable offset)

Copy link
Collaborator

Choose a reason for hiding this comment

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

Maybe I misunderstand something, but shouldn't it be:

assert!(parse_datetime("m1y-12").is_err());

and

assert!(parse_datetime("m+12").is_ok());

With GNU date I get:

$ date -d "m1y-12"
date: invalid date ‘m1y-12’
$ date -d "m+12"
Sun May 25 02:00:00 AM CEST 2025

Copy link
Contributor

Choose a reason for hiding this comment

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

Goodness....

So I have no idea what m1y is supposed to parse to ,-P But it seems like we accept it, while GNU coreutils always rejects it. So I think this is orthogonal to the problem here. So maybe let's drop my suggestion. Filed #147.

BUT, GNU coreutils accepts all offsets between -24 and +24, so we should do that too:

$ date -d "m"
Sun May 25 02:00:00 PM CEST 2025
$ date -d "m+24"
Sat May 24 02:00:00 PM CEST 2025
$ date -d "m+25"
date: invalid date ‘m+25’
$ date -d "m-25"
date: invalid date ‘m-25’
$ date -d "m-24"
Mon May 26 02:00:00 PM CEST 2025

Copy link
Contributor Author

@yuankunzhang yuankunzhang May 26, 2025

Choose a reason for hiding this comment

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

We are using chrono::FixedOffset to represent offsets, which accepts offsets only in the range -24 to +24. Note that the literal m+12 will overflow because the letter m already denotes a +12 offset. Achieving GNU date compatibility in this regard will require re-working the underlining data models. Since this PR is just a trivial fix, I suggest we merge it first and address the larger redesign in a separate effort.

@drinkcat drinkcat mentioned this pull request May 26, 2025
@sylvestre sylvestre requested review from drinkcat and cakebaker May 26, 2025 14:51
Copy link
Collaborator

@cakebaker cakebaker left a comment

Choose a reason for hiding this comment

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

It looks good to me.

Copy link
Contributor

@drinkcat drinkcat left a comment

Choose a reason for hiding this comment

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

Yeah this is fine, let's iterate on that (also curious if jiff can or cannot help with this, but my conversion has other problems right now...)

@sylvestre sylvestre merged commit 1552608 into uutils:main May 26, 2025
17 checks passed
Copy link

codecov bot commented May 26, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 0.00%. Comparing base (cc7c143) to head (ab91b2b).
Report is 7 commits behind head on main.

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

☔ 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.

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.

4 participants