Skip to content

Commit

Permalink
Improved UX of creating TimestampNanosecondArray with timezones (#3088
Browse files Browse the repository at this point in the history
)

* Make with_timezone more flexible

Make with_timezone method accept both &str and String values.

* Add alias for UTC

Add a method to PrimitiveArray<T: ArrowTimestampType> for using UTC as
the timezone.

Co-authored-by: Raphael Taylor-Davies <r.taylordavies@googlemail.com>
  • Loading branch information
src255 and tustvold committed Nov 11, 2022
1 parent 02a3f5c commit 561f63a
Showing 1 changed file with 22 additions and 2 deletions.
24 changes: 22 additions & 2 deletions arrow-array/src/array/primitive_array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -801,8 +801,13 @@ impl<T: ArrowTimestampType> PrimitiveArray<T> {
}

/// Construct a timestamp array with new timezone
pub fn with_timezone(&self, timezone: String) -> Self {
self.with_timezone_opt(Some(timezone))
pub fn with_timezone(&self, timezone: impl Into<String>) -> Self {
self.with_timezone_opt(Some(timezone.into()))
}

/// Construct a timestamp array with UTC
pub fn with_timezone_utc(&self) -> Self {
self.with_timezone("+00:00")
}

/// Construct a timestamp array with an optional timezone
Expand Down Expand Up @@ -1344,6 +1349,21 @@ mod tests {
);
}

#[test]
fn test_timestamp_utc_fmt_debug() {
let arr: PrimitiveArray<TimestampMillisecondType> =
TimestampMillisecondArray::from(vec![
1546214400000,
1546214400000,
-1546214400000,
])
.with_timezone_utc();
assert_eq!(
"PrimitiveArray<Timestamp(Millisecond, Some(\"+00:00\"))>\n[\n 2018-12-31T00:00:00+00:00,\n 2018-12-31T00:00:00+00:00,\n 1921-01-02T00:00:00+00:00,\n]",
format!("{:?}", arr)
);
}

#[test]
#[cfg(feature = "chrono-tz")]
fn test_timestamp_with_named_tz_fmt_debug() {
Expand Down

0 comments on commit 561f63a

Please sign in to comment.