Skip to content

Commit

Permalink
Don't allow defaults in to_datetime
Browse files Browse the repository at this point in the history
  • Loading branch information
pitdicker committed Jun 12, 2023
1 parent 84bb9b0 commit d845980
Showing 1 changed file with 61 additions and 1 deletion.
62 changes: 61 additions & 1 deletion src/format/parsed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -580,7 +580,7 @@ impl Parsed {
(None, Some(_)) => 0, // UNIX timestamp may assume 0 offset
(None, None) => return Err(NOT_ENOUGH),
};
let datetime = self.to_naive_datetime_with_offset(offset)?;
let datetime = self.to_naive_datetime_with_offset_inner(offset, false)?;
let offset = FixedOffset::east_opt(offset).ok_or(OUT_OF_RANGE)?;

// this is used to prevent an overflow when calling FixedOffset::from_local_datetime
Expand Down Expand Up @@ -1269,6 +1269,66 @@ mod tests {
); // `FixedOffset` does not support such huge offset
}

#[test]
fn test_parsed_to_datetime_no_fallback() {
macro_rules! parse {
($($k:ident: $v:expr),*) => (
Parsed { $($k: Some($v),)* ..Parsed::new() }.to_datetime()
)
}

let ymdhmsn = |y, m, d, h, n, s, off| {
Ok(FixedOffset::east_opt(off)
.unwrap()
.from_local_datetime(
&NaiveDate::from_ymd_opt(y, m, d).unwrap().and_hms_opt(h, n, s).unwrap(),
)
.unwrap())
};

assert_eq!(
parse!(year: 2023, month: 6, day: 1, hour_div_12: 1, hour_mod_12: 3, minute: 5,
second: 0, offset: 3600),
ymdhmsn(2023, 6, 1, 15, 5, 0, 3600)
);
assert_eq!(
parse!(year: 2023, month: 6, hour_div_12: 1, hour_mod_12: 3, minute: 5, second: 0,
offset: 3600),
Err(NOT_ENOUGH)
);
assert_eq!(
parse!(year: 2023, week_from_mon: 22, hour_div_12: 1, hour_mod_12: 3, minute: 5,
second: 0, offset: 3600),
Err(NOT_ENOUGH)
);
assert_eq!(
parse!(year: 2023, week_from_sun: 22, hour_div_12: 1, hour_mod_12: 3, minute: 5,
second: 0, offset: 3600),
Err(NOT_ENOUGH)
);
assert_eq!(
parse!(isoyear: 2023, isoweek: 22, hour_div_12: 1, hour_mod_12: 3, minute: 5,
second: 0, offset: 3600),
Err(NOT_ENOUGH)
);
assert_eq!(
parse!(year: 2023, month: 6, day: 1, minute: 5, second: 0,
offset: 3600),
Err(NOT_ENOUGH)
);
assert_eq!(
parse!(year: 2023, month: 6, day: 1, hour_div_12: 1, hour_mod_12: 3, second: 0,
offset: 3600),
Err(NOT_ENOUGH)
);
// Seconds are allowed to be missing.
assert_eq!(
parse!(year: 2023, month: 6, day: 1, hour_div_12: 1, hour_mod_12: 3, minute: 5,
offset: 3600),
ymdhmsn(2023, 6, 1, 15, 5, 0, 3600)
);
}

#[test]
fn test_parsed_to_datetime_with_timezone() {
macro_rules! parse {
Expand Down

0 comments on commit d845980

Please sign in to comment.