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

Fixed display of timestamp with tz. #375

Merged
merged 1 commit into from
Sep 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
33 changes: 31 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,28 @@ pub fn timeunit_scale(a: &TimeUnit, b: &TimeUnit) -> f64 {
(TimeUnit::Nanosecond, TimeUnit::Nanosecond) => 1.0,
}
}

pub(crate) fn parse_offset(offset: &str) -> Result<FixedOffset> {
if offset == "UTC" {
return Ok(FixedOffset::east(0));
}
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
12 changes: 6 additions & 6 deletions tests/it/io/print.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,12 +185,12 @@ fn write_timestamp_second() {
#[test]
fn write_timestamp_second_with_tz() {
let expected = vec![
"+-------------------------+",
"| f |",
"+-------------------------+",
"| 1970-05-09 14:25:11 UTC |",
"| |",
"+-------------------------+",
"+----------------------------+",
"| f |",
"+----------------------------+",
"| 1970-05-09 14:25:11 +00:00 |",
"| |",
"+----------------------------+",
];
check_datetime!(
i64,
Expand Down