This is an automated email from the ASF dual-hosted git repository.

tustvold pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/arrow-rs.git


The following commit(s) were added to refs/heads/master by this push:
     new c0d0ac062 support more fixedoffset tz format (#2936)
c0d0ac062 is described below

commit c0d0ac0623170b8c97f1fa40b987ffbbfe6aee03
Author: Wei-Ting Kuo <waitingkuo0...@gmail.com>
AuthorDate: Thu Oct 27 05:15:37 2022 +0800

    support more fixedoffset tz format (#2936)
---
 arrow-array/src/timezone.rs           | 41 ++++++++++++++++++++++-------------
 arrow/src/compute/kernels/temporal.rs | 12 ++++++++--
 2 files changed, 36 insertions(+), 17 deletions(-)

diff --git a/arrow-array/src/timezone.rs b/arrow-array/src/timezone.rs
index 4e60c0c42..7bd597904 100644
--- a/arrow-array/src/timezone.rs
+++ b/arrow-array/src/timezone.rs
@@ -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")]
@@ -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);
     }
 }
diff --git a/arrow/src/compute/kernels/temporal.rs 
b/arrow/src/compute/kernels/temporal.rs
index ad1bab773..abb8b40c2 100644
--- a/arrow/src/compute/kernels/temporal.rs
+++ b/arrow/src/compute/kernels/temporal.rs
@@ -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]

Reply via email to