From 290df755ebce33afa407d5001075a2989065be41 Mon Sep 17 00:00:00 2001 From: amonin7 Date: Tue, 13 Feb 2024 11:31:02 +0100 Subject: [PATCH 1/2] Fix bug with conversion of Timestamp::Microseconds to chrono::Datetime --- influxdb/src/query/consts.rs | 1 + influxdb/src/query/mod.rs | 17 +++++++++++++---- 2 files changed, 14 insertions(+), 4 deletions(-) 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) From 696cbe4b32557b537745d43c3880885b1469d906 Mon Sep 17 00:00:00 2001 From: Andrey Minin Date: Wed, 14 Feb 2024 10:50:41 +0100 Subject: [PATCH 2/2] Delete broken test and incorrect constant --- influxdb/src/query/consts.rs | 3 --- influxdb/src/query/mod.rs | 15 ++++----------- 2 files changed, 4 insertions(+), 14 deletions(-) diff --git a/influxdb/src/query/consts.rs b/influxdb/src/query/consts.rs index bb3ea8f..96c0b36 100644 --- a/influxdb/src/query/consts.rs +++ b/influxdb/src/query/consts.rs @@ -3,6 +3,3 @@ 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 5394530..ccb3513 100644 --- a/influxdb/src/query/mod.rs +++ b/influxdb/src/query/mod.rs @@ -30,7 +30,9 @@ pub mod write_query; use std::fmt; use crate::{Error, ReadQuery, WriteQuery}; -use consts::{MILLIS_PER_SECOND, MINUTES_PER_HOUR, NANOS_PER_MICRO, 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; @@ -230,8 +232,7 @@ pub enum QueryType { #[cfg(test)] mod tests { use super::consts::{ - MICROS_PER_NANO, MILLIS_PER_SECOND, MINUTES_PER_HOUR, NANOS_PER_MICRO, NANOS_PER_MILLI, - SECONDS_PER_MINUTE, + 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}; @@ -302,14 +303,6 @@ mod tests { } #[test] fn test_chrono_datetime_from_timestamp_micros() { - let datetime_from_timestamp: DateTime = Timestamp::Microseconds(1).into(); - assert_eq!( - Utc.timestamp_nanos((1 / MICROS_PER_NANO).try_into().unwrap()), - datetime_from_timestamp - ) - } - #[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()),