Skip to content

Commit

Permalink
Fix bug with conversion of Timestamp::Microseconds to chrono::Datetime (
Browse files Browse the repository at this point in the history
  • Loading branch information
amonin7 authored Feb 14, 2024
1 parent a7a56df commit 4a93896
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
4 changes: 1 addition & 3 deletions influxdb/src/query/consts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,4 @@ 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;

#[cfg(test)]
pub const MICROS_PER_NANO: u128 = 1000;
pub const NANOS_PER_MICRO: u128 = 1000;
14 changes: 8 additions & 6 deletions influxdb/src/query/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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_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;
Expand Down Expand Up @@ -76,8 +78,8 @@ impl From<Timestamp> for DateTime<Utc> {
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())
}
}
Expand Down Expand Up @@ -230,7 +232,7 @@ 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,
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};
Expand Down Expand Up @@ -301,9 +303,9 @@ mod tests {
}
#[test]
fn test_chrono_datetime_from_timestamp_micros() {
let datetime_from_timestamp: DateTime<Utc> = Timestamp::Microseconds(1).into();
let datetime_from_timestamp: DateTime<Utc> = Timestamp::Microseconds(2).into();
assert_eq!(
Utc.timestamp_nanos((1 / MICROS_PER_NANO).try_into().unwrap()),
Utc.timestamp_nanos((2 * NANOS_PER_MICRO).try_into().unwrap()),
datetime_from_timestamp
)
}
Expand Down

0 comments on commit 4a93896

Please sign in to comment.