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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions src/items/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ fn with_timezone_restore(
offset: time::Offset,
at: DateTime<FixedOffset>,
) -> Option<DateTime<FixedOffset>> {
let offset: FixedOffset = chrono::FixedOffset::from(offset);
let offset: FixedOffset = chrono::FixedOffset::try_from(offset).ok()?;
let copy = at;
let x = at
.with_timezone(&offset)
Expand Down Expand Up @@ -360,7 +360,9 @@ fn at_date_inner(date: Vec<Item>, mut d: DateTime<FixedOffset>) -> Option<DateTi
},
..
}) => {
let offset = offset.map(chrono::FixedOffset::from).unwrap_or(*d.offset());
let offset = offset
.and_then(|o| chrono::FixedOffset::try_from(o).ok())
.unwrap_or(*d.offset());

d = new_date(
year.map(|x| x as i32).unwrap_or(d.year()),
Expand All @@ -379,7 +381,10 @@ fn at_date_inner(date: Vec<Item>, mut d: DateTime<FixedOffset>) -> Option<DateTi
second,
offset,
}) => {
let offset = offset.map(chrono::FixedOffset::from).unwrap_or(*d.offset());
let offset = offset
.and_then(|o| chrono::FixedOffset::try_from(o).ok())
.unwrap_or(*d.offset());

d = new_date(
d.year(),
d.month(),
Expand Down
29 changes: 21 additions & 8 deletions src/items/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ use winnow::{
ModalResult, Parser,
};

use crate::ParseDateTimeError;

use super::{dec_uint, relative, s};

#[derive(PartialEq, Debug, Clone, Default)]
Expand Down Expand Up @@ -95,22 +97,33 @@ impl Offset {
}
}

impl From<Offset> for chrono::FixedOffset {
fn from(
impl TryFrom<Offset> for chrono::FixedOffset {
type Error = ParseDateTimeError;

fn try_from(
Offset {
negative,
hours,
minutes,
}: Offset,
) -> Self {
) -> Result<Self, Self::Error> {
let secs = hours * 3600 + minutes * 60;

if negative {
FixedOffset::west_opt(secs.try_into().expect("secs overflow"))
.expect("timezone overflow")
let offset = if negative {
FixedOffset::west_opt(
secs.try_into()
.map_err(|_| ParseDateTimeError::InvalidInput)?,
)
.ok_or(ParseDateTimeError::InvalidInput)?
} else {
FixedOffset::east_opt(secs.try_into().unwrap()).unwrap()
}
FixedOffset::east_opt(
secs.try_into()
.map_err(|_| ParseDateTimeError::InvalidInput)?,
)
.ok_or(ParseDateTimeError::InvalidInput)?
};

Ok(offset)
}
}

Expand Down
6 changes: 6 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,12 @@ mod tests {
.and_utc();
assert_eq!(actual, expected);
}

#[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.

}
}

#[cfg(test)]
Expand Down