diff --git a/influxdb/src/query/consts.rs b/influxdb/src/query/consts.rs index b302599..bb3ea8f 100644 --- a/influxdb/src/query/consts.rs +++ b/influxdb/src/query/consts.rs @@ -2,6 +2,7 @@ pub const MINUTES_PER_HOUR: u128 = 60; pub const SECONDS_PER_MINUTE: u128 = 60; pub const MILLIS_PER_SECOND: u128 = 1000; pub const NANOS_PER_MILLI: u128 = 1_000_000; +pub const NANOS_PER_MICRO: u128 = 1000; #[cfg(test)] pub const MICROS_PER_NANO: u128 = 1000; diff --git a/influxdb/src/query/mod.rs b/influxdb/src/query/mod.rs index 0546b2b..5394530 100644 --- a/influxdb/src/query/mod.rs +++ b/influxdb/src/query/mod.rs @@ -30,7 +30,7 @@ pub mod write_query; use std::fmt; use crate::{Error, ReadQuery, WriteQuery}; -use consts::{MILLIS_PER_SECOND, MINUTES_PER_HOUR, NANOS_PER_MILLI, SECONDS_PER_MINUTE}; +use consts::{MILLIS_PER_SECOND, MINUTES_PER_HOUR, NANOS_PER_MICRO, NANOS_PER_MILLI, SECONDS_PER_MINUTE}; #[cfg(feature = "derive")] pub use influxdb_derive::InfluxDbWriteable; @@ -76,8 +76,8 @@ impl From for DateTime { Utc.timestamp_nanos(nanos.try_into().unwrap()) } Timestamp::Nanoseconds(nanos) => Utc.timestamp_nanos(nanos.try_into().unwrap()), - Timestamp::Microseconds(mis) => { - let nanos = mis / 10000; + Timestamp::Microseconds(micros) => { + let nanos = micros * NANOS_PER_MICRO; Utc.timestamp_nanos(nanos.try_into().unwrap()) } } @@ -230,7 +230,8 @@ pub enum QueryType { #[cfg(test)] mod tests { use super::consts::{ - MICROS_PER_NANO, MILLIS_PER_SECOND, MINUTES_PER_HOUR, NANOS_PER_MILLI, SECONDS_PER_MINUTE, + MICROS_PER_NANO, MILLIS_PER_SECOND, MINUTES_PER_HOUR, NANOS_PER_MICRO, NANOS_PER_MILLI, + SECONDS_PER_MINUTE, }; use crate::query::{Timestamp, ValidQuery}; use chrono::prelude::{DateTime, TimeZone, Utc}; @@ -308,6 +309,14 @@ mod tests { ) } #[test] + fn test_chrono_datetime_from_timestamp_micros_second() { + let datetime_from_timestamp: DateTime = Timestamp::Microseconds(2).into(); + assert_eq!( + Utc.timestamp_nanos((2 * NANOS_PER_MICRO).try_into().unwrap()), + datetime_from_timestamp + ) + } + #[test] fn test_timestamp_from_chrono_date() { let timestamp_from_datetime: Timestamp = Utc .with_ymd_and_hms(1970, 1, 1, 0, 0, 1)