Skip to content
This repository has been archived by the owner on Feb 18, 2024. It is now read-only.

Commit

Permalink
Fixed display of timestamp with tz.
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgecarleitao committed Sep 3, 2021
1 parent dbb7b8a commit 9c48af7
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 14 deletions.
24 changes: 12 additions & 12 deletions src/array/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,11 @@ pub fn get_value_display<'a>(array: &'a dyn Array) -> Box<dyn Fn(usize) -> Strin
Time64(_) => unreachable!(), // remaining are not valid
Timestamp(TimeUnit::Second, tz) => {
if let Some(tz) = tz {
let offset = temporal_conversions::parse_offset(tz).unwrap();
dyn_primitive!(array, i64, |x| {
format!(
"{} {}",
chrono::DateTime::<chrono::FixedOffset>::from_utc(
temporal_conversions::timestamp_s_to_datetime(x),
tz
offset,
)
})
} else {
Expand All @@ -81,11 +81,11 @@ pub fn get_value_display<'a>(array: &'a dyn Array) -> Box<dyn Fn(usize) -> Strin
}
Timestamp(TimeUnit::Millisecond, tz) => {
if let Some(tz) = tz {
let offset = temporal_conversions::parse_offset(tz).unwrap();
dyn_primitive!(array, i64, |x| {
format!(
"{} {}",
chrono::DateTime::<chrono::FixedOffset>::from_utc(
temporal_conversions::timestamp_ms_to_datetime(x),
tz
offset,
)
})
} else {
Expand All @@ -94,11 +94,11 @@ pub fn get_value_display<'a>(array: &'a dyn Array) -> Box<dyn Fn(usize) -> Strin
}
Timestamp(TimeUnit::Microsecond, tz) => {
if let Some(tz) = tz {
let offset = temporal_conversions::parse_offset(tz).unwrap();
dyn_primitive!(array, i64, |x| {
format!(
"{} {}",
chrono::DateTime::<chrono::FixedOffset>::from_utc(
temporal_conversions::timestamp_us_to_datetime(x),
tz
offset,
)
})
} else {
Expand All @@ -107,11 +107,11 @@ pub fn get_value_display<'a>(array: &'a dyn Array) -> Box<dyn Fn(usize) -> Strin
}
Timestamp(TimeUnit::Nanosecond, tz) => {
if let Some(tz) = tz {
let offset = temporal_conversions::parse_offset(tz).unwrap();
dyn_primitive!(array, i64, |x| {
format!(
"{} {}",
chrono::DateTime::<chrono::FixedOffset>::from_utc(
temporal_conversions::timestamp_ns_to_datetime(x),
tz
offset,
)
})
} else {
Expand Down
30 changes: 28 additions & 2 deletions src/temporal_conversions.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
//! Conversion methods for dates and times.
use crate::datatypes::TimeUnit;
use chrono::{NaiveDate, NaiveDateTime, NaiveTime};
use chrono::{FixedOffset, NaiveDate, NaiveDateTime, NaiveTime};

use crate::{
datatypes::TimeUnit,
error::{ArrowError, Result},
};

/// Number of seconds in a day
pub const SECONDS_IN_DAY: i64 = 86_400;
Expand Down Expand Up @@ -149,3 +153,25 @@ pub fn timeunit_scale(a: &TimeUnit, b: &TimeUnit) -> f64 {
(TimeUnit::Nanosecond, TimeUnit::Nanosecond) => 1.0,
}
}

pub(crate) fn parse_offset(offset: &str) -> Result<FixedOffset> {
let mut a = offset.split(':');
let first = a.next().map(Ok).unwrap_or_else(|| {
Err(ArrowError::InvalidArgumentError(
"timezone offset must be of the form [-]00:00".to_string(),
))
})?;
let last = a.next().map(Ok).unwrap_or_else(|| {
Err(ArrowError::InvalidArgumentError(
"timezone offset must be of the form [-]00:00".to_string(),
))
})?;
let hours: i32 = first.parse().map_err(|_| {
ArrowError::InvalidArgumentError("timezone offset must be of the form [-]00:00".to_string())
})?;
let minutes: i32 = last.parse().map_err(|_| {
ArrowError::InvalidArgumentError("timezone offset must be of the form [-]00:00".to_string())
})?;

Ok(FixedOffset::east(hours * 60 * 60 + minutes * 60))
}
12 changes: 12 additions & 0 deletions tests/it/array/primitive/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,18 @@ fn display_timestamp_ns() {
);
}

#[test]
fn display_timestamp_tz_ns() {
let array = Int64Array::from(&[Some(1), None, Some(2)]).to(DataType::Timestamp(
TimeUnit::Nanosecond,
Some("+02:00".to_string()),
));
assert_eq!(
format!("{}", array),
"Timestamp(Nanosecond, Some(\"+02:00\"))[1970-01-01 02:00:00.000000001 +02:00, , 1970-01-01 02:00:00.000000002 +02:00]"
);
}

#[test]
fn display_duration_ms() {
let array =
Expand Down

0 comments on commit 9c48af7

Please sign in to comment.