Skip to content

Commit

Permalink
Add API to change timezone for timestamp array (#2347)
Browse files Browse the repository at this point in the history
* Add with_timezone API

* Use into_builder()

* Fix clippy
  • Loading branch information
viirya committed Aug 7, 2022
1 parent a2de363 commit 5fae299
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions arrow/src/array/array_primitive.rs
Expand Up @@ -549,6 +549,18 @@ impl<T: ArrowTimestampType> PrimitiveArray<T> {
let array_data = unsafe { array_data.build_unchecked() };
PrimitiveArray::from(array_data)
}

/// Construct a timestamp array with new timezone
pub fn with_timezone(&self, timezone: String) -> Self {
let array_data = unsafe {
self.data
.clone()
.into_builder()
.data_type(DataType::Timestamp(T::get_time_unit(), Some(timezone)))
.build_unchecked()
};
PrimitiveArray::from(array_data)
}
}

impl<T: ArrowTimestampType> PrimitiveArray<T> {
Expand Down Expand Up @@ -1099,4 +1111,21 @@ mod tests {
BooleanArray::from(vec![true, true, true, true, true])
);
}

#[cfg(feature = "chrono-tz")]
#[test]
fn test_with_timezone() {
use crate::compute::hour;
let a: TimestampMicrosecondArray = vec![37800000000, 86339000000].into();

let b = hour(&a).unwrap();
assert_eq!(10, b.value(0));
assert_eq!(23, b.value(1));

let a = a.with_timezone(String::from("America/Los_Angeles"));

let b = hour(&a).unwrap();
assert_eq!(2, b.value(0));
assert_eq!(15, b.value(1));
}
}

0 comments on commit 5fae299

Please sign in to comment.