Skip to content
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

fmt: add APIs for skipping weekday checking #53

Merged
merged 1 commit into from
Jul 28, 2024
Merged

Conversation

BurntSushi
Copy link
Owner

@BurntSushi BurntSushi commented Jul 28, 2024

In the course of doing some experiments with using Jiff in gix-date, I
discovered that it currently relies on the fact that parsing RFC 2822
datetimes (and similar) does not do error checking if the date and
weekday are inconsistent. Prior to this PR, Jiff required that the
weekday and date were consistent. This PR adds APIs for skipping this
check.

For RFC 2822, this adds a new relaxed_weekday configuration on the
DateTimeParser builder.

For the strtime APIs, we achieve this by adding more granular accessors
and mutators on BrokenDownTime. So now folks can do this:

let tm = BrokenDownTime::parse("%a, %F", "Wed, 2024-07-27")?;
tm.set_weekday(None);
assert_eq!(tm.to_date().unwrap(), jiff::civil::date(2024, 7, 27));

Where the above succeeds even though 2024-07-27 was a Saturday. It's a
little verbose, but my hope is that this is a rarely needed thing.

This PR also adds getters and setters for individual fields on a
jiff::fmt::strtime::BrokenDownTime. This affords a bit more flexibility
in terms of what can be parsed and formatted. For example, it
lets one parse just a weekday:

use jiff::{civil::Weekday, fmt::strtime::BrokenDownTime};

let tm = BrokenDownTime::parse("%a", "Sat").unwrap();
assert_eq!(tm.weekday(), Some(Weekday::Saturday));

In the course of doing some experiments with using Jiff in `gix-date`, I
discovered that it currently relies on the fact that parsing RFC 2822
datetimes (and similar) does *not* do error checking if the date and
weekday are inconsistent. Prior to this PR, Jiff required that the
weekday and date were consistent. This PR adds APIs for skipping this
check.

For RFC 2822, this adds a new `relaxed_weekday` configuration on the
`DateTimeParser` builder.

For the strtime APIs, we achieve this by adding more granular accessors
and mutators on `BrokenDownTime`. So now folks can do this:

```
let tm = BrokenDownTime::parse("%a, %F", "Wed, 2024-07-27")?;
tm.set_weekday(None);
assert_eq!(tm.to_date().unwrap(), jiff::civil::date(2024, 7, 27));
```

Where the above succeeds even though 2024-07-27 was a Saturday. It's a
little verbose, but my hope is that this is a rarely needed thing.
@BurntSushi BurntSushi merged commit 70c6ca0 into master Jul 28, 2024
14 checks passed
@BurntSushi BurntSushi deleted the ag/relaxed-weekday branch July 28, 2024 16:12
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.

1 participant