Skip to content

Commit

Permalink
Merge pull request #1622 from finos/filter-timezone
Browse files Browse the repository at this point in the history
  • Loading branch information
texodus authored Nov 22, 2021
2 parents efd0edd + e91b1c6 commit 91f447a
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 29 deletions.
36 changes: 7 additions & 29 deletions rust/perspective-viewer/src/rust/components/filter_item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@ use crate::custom_elements::filter_dropdown::*;
use crate::dragdrop::*;
use crate::renderer::*;
use crate::session::*;
use crate::utils::{posix_to_utc_str, str_to_utc_posix};
use crate::*;

use super::containers::dragdrop_list::*;
use super::containers::dropdown::*;

use chrono::{Local, NaiveDate, NaiveDateTime, TimeZone, Utc};
use chrono::{NaiveDate, TimeZone, Utc};
use wasm_bindgen::JsCast;
use web_sys::*;
use yew::prelude::*;
Expand Down Expand Up @@ -82,7 +83,6 @@ impl FilterItemProperties {
if *x > 0_f64 {
Some(
Utc.timestamp(*x as i64 / 1000, (*x as u32 % 1000) * 1000)
.with_timezone(&Local)
.format("%Y-%m-%d")
.to_string(),
)
Expand All @@ -92,19 +92,7 @@ impl FilterItemProperties {
}
(Type::Datetime, FilterTerm::Scalar(Scalar::Float(x)))
| (Type::Datetime, FilterTerm::Scalar(Scalar::DateTime(x))) => {
if *x > 0_f64 {
Some(
Utc.timestamp(
*x as i64 / 1000,
((*x as i64 % 1000) * 1000000) as u32,
)
.with_timezone(&Local)
.format("%Y-%m-%dT%H:%M:%S%.3f")
.to_string(),
)
} else {
None
}
posix_to_utc_str(*x).ok()
}
(Type::Bool, FilterTerm::Scalar(Scalar::Bool(x))) => {
Some((if *x { "true" } else { "false" }).to_owned())
Expand Down Expand Up @@ -209,20 +197,10 @@ impl FilterItemProperties {
})
}
Some(Type::Datetime) => {
filter_item.2 = FilterTerm::Scalar(
match NaiveDateTime::parse_from_str(
&val,
"%Y-%m-%dT%H:%M:%S%.3f",
) {
Ok(ref posix) => Scalar::DateTime(
Utc.from_local_datetime(posix)
.unwrap()
.timestamp_millis()
as f64,
),
_ => Scalar::Null,
},
)
let posix = str_to_utc_posix(&val)
.map(Scalar::DateTime)
.unwrap_or(Scalar::Null);
filter_item.2 = FilterTerm::Scalar(posix);
}
Some(Type::Bool) => {
filter_item.2 = FilterTerm::Scalar(match val.as_str() {
Expand Down
41 changes: 41 additions & 0 deletions rust/perspective-viewer/src/rust/utils/datetime.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
////////////////////////////////////////////////////////////////////////////////
//
// Copyright (c) 2018, the Perspective Authors.
//
// This file is part of the Perspective library, distributed under the terms
// of the Apache License 2.0. The full license can be found in the LICENSE
// file.

use crate::utils::*;

use chrono::{DateTime, FixedOffset, NaiveDateTime, TimeZone, Utc};
use wasm_bindgen::prelude::*;

pub fn posix_to_utc_str(x: f64) -> Result<String, JsValue> {
let tz = FixedOffset::west(
js_sys::Date::new(&0.into()).get_timezone_offset() as i32 * 60,
);

if x > 0_f64 {
Ok(Utc
.timestamp(x as i64 / 1000, ((x as i64 % 1000) * 1000000) as u32)
.with_timezone(&tz)
.format("%Y-%m-%dT%H:%M:%S%.3f")
.to_string())
} else {
Err(format!("Unknown timestamp {}", x).into())
}
}

pub fn str_to_utc_posix(val: &str) -> Result<f64, JsValue> {
let tz = FixedOffset::west(
js_sys::Date::new(&0.into()).get_timezone_offset() as i32 * 60,
);

NaiveDateTime::parse_from_str(val, "%Y-%m-%dT%H:%M:%S%.3f")
.map(|ref posix| {
DateTime::<Utc>::from(tz.from_local_datetime(posix).unwrap())
.timestamp_millis() as f64
})
.into_jserror()
}
6 changes: 6 additions & 0 deletions rust/perspective-viewer/src/rust/utils/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,9 @@ impl<T> ToJsValueError<T> for Result<T, std::io::Error> {
self.map_err(|x: std::io::Error| JsValue::from(&format!("{}", x)))
}
}

impl<T> ToJsValueError<T> for Result<T, chrono::ParseError> {
fn into_jserror(self) -> Result<T, JsValue> {
self.map_err(|x: chrono::ParseError| JsValue::from(&format!("{}", x)))
}
}
2 changes: 2 additions & 0 deletions rust/perspective-viewer/src/rust/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

mod async_callback;
mod closure;
mod datetime;
mod debounce;
mod errors;
mod future_to_promise;
Expand All @@ -22,6 +23,7 @@ mod tests;

pub use self::async_callback::*;
pub use self::closure::*;
pub use self::datetime::*;
pub use self::debounce::*;
pub use self::errors::*;
pub use self::future_to_promise::*;
Expand Down

0 comments on commit 91f447a

Please sign in to comment.