Skip to content

Commit

Permalink
Fixed issue where timestamp parser incorrectly accepted characters af…
Browse files Browse the repository at this point in the history
…ter 'Z (#5189)
  • Loading branch information
razeghi71 authored Dec 8, 2023
1 parent d41e90e commit 93a28a5
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
25 changes: 25 additions & 0 deletions arrow-cast/src/cast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9449,4 +9449,29 @@ mod tests {
let r: Vec<_> = a.as_string::<i32>().iter().map(|x| x.unwrap()).collect();
assert_eq!(r, &["[0, 1, 2]", "[0, null, 2]"]);
}
#[test]
fn test_cast_string_to_timestamp_invalid_tz() {
// content after Z should be ignored
let bad_timestamp = "2023-12-05T21:58:10.45ZZTOP";
let array = StringArray::from(vec![Some(bad_timestamp)]);

let data_types = [
DataType::Timestamp(TimeUnit::Second, None),
DataType::Timestamp(TimeUnit::Millisecond, None),
DataType::Timestamp(TimeUnit::Microsecond, None),
DataType::Timestamp(TimeUnit::Nanosecond, None),
];

let cast_options = CastOptions {
safe: false,
..Default::default()
};

for dt in data_types {
assert_eq!(
cast_with_options(&array, &dt, &cast_options).unwrap_err().to_string(),
"Parser error: Invalid timezone \"ZZTOP\": only offset based timezones supported without chrono-tz feature"
);
}
}
}
2 changes: 1 addition & 1 deletion arrow-cast/src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ pub fn string_to_datetime<T: TimeZone>(timezone: &T, s: &str) -> Result<DateTime
.ok_or_else(|| err("error computing timezone offset"));
}

if bytes[tz_offset] == b'z' || bytes[tz_offset] == b'Z' {
if (bytes[tz_offset] == b'z' || bytes[tz_offset] == b'Z') && tz_offset == bytes.len() - 1 {
return Ok(timezone.from_utc_datetime(&datetime));
}

Expand Down

0 comments on commit 93a28a5

Please sign in to comment.