Skip to content

Commit

Permalink
support more fixedoffset tz format (#2936)
Browse files Browse the repository at this point in the history
  • Loading branch information
waitingkuo committed Oct 26, 2022
1 parent 1d36bdf commit c0d0ac0
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 17 deletions.
41 changes: 26 additions & 15 deletions arrow-array/src/timezone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,24 @@ pub use private::{Tz, TzOffset};

/// Parses a fixed offset of the form "+09:00"
fn parse_fixed_offset(tz: &str) -> Result<FixedOffset, ArrowError> {
if tz.len() != 6 {
return Err(ArrowError::ParseError(format!(
"Invalid timezone \"{}\": Expected format [+-]XX:XX",
tz
)));
let mut parsed = Parsed::new();

if let Ok(fixed_offset) = parse(&mut parsed, tz, StrftimeItems::new("%:z"))
.and_then(|_| parsed.to_fixed_offset())
{
return Ok(fixed_offset);
}

let mut parsed = Parsed::new();
parse(&mut parsed, tz, StrftimeItems::new("%:z"))
if let Ok(fixed_offset) = parse(&mut parsed, tz, StrftimeItems::new("%#z"))
.and_then(|_| parsed.to_fixed_offset())
.map_err(|e| {
ArrowError::ParseError(format!("Invalid timezone \"{}\": {}", tz, e))
})
{
return Ok(fixed_offset);
}

Err(ArrowError::ParseError(format!(
"Invalid timezone \"{}\": Expected format [+-]XX:XX, [+-]XX, or [+-]XXXX",
tz
)))
}

#[cfg(feature = "chrono-tz")]
Expand Down Expand Up @@ -313,13 +318,19 @@ mod tests {
9 * 60 * 60
);

let err = "+9:00".parse::<Tz>().unwrap_err().to_string();
assert!(err.contains("Invalid timezone"), "{}", err);
let tz = "+09".parse::<Tz>().unwrap();
assert_eq!(
tz.offset_from_utc_date(&t).fix().local_minus_utc(),
9 * 60 * 60
);

let err = "+09".parse::<Tz>().unwrap_err().to_string();
assert!(err.contains("Invalid timezone"), "{}", err);
let tz = "+0900".parse::<Tz>().unwrap();
assert_eq!(
tz.offset_from_utc_date(&t).fix().local_minus_utc(),
9 * 60 * 60
);

let err = "+0900".parse::<Tz>().unwrap_err().to_string();
let err = "+9:00".parse::<Tz>().unwrap_err().to_string();
assert!(err.contains("Invalid timezone"), "{}", err);
}
}
12 changes: 10 additions & 2 deletions arrow/src/compute/kernels/temporal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1133,8 +1133,16 @@ mod tests {
fn test_temporal_array_timestamp_hour_with_timezone_without_colon() {
let a = TimestampSecondArray::from(vec![60 * 60 * 10])
.with_timezone("+0100".to_string());
let err = hour(&a).unwrap_err().to_string();
assert!(err.contains("Invalid timezone"), "{}", err);
let b = hour(&a).unwrap();
assert_eq!(11, b.value(0));
}

#[test]
fn test_temporal_array_timestamp_hour_with_timezone_without_minutes() {
let a = TimestampSecondArray::from(vec![60 * 60 * 10])
.with_timezone("+01".to_string());
let b = hour(&a).unwrap();
assert_eq!(11, b.value(0));
}

#[test]
Expand Down

0 comments on commit c0d0ac0

Please sign in to comment.