Skip to content

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 force-pushed the ag/relaxed-weekday branch from a84c762 to 7e1c54a Compare July 28, 2024 13:53
@BurntSushi BurntSushi merged commit 70c6ca0 into master Jul 28, 2024
@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.

2 participants