Skip to content

Fix bug with conversion of Timestamp::Microseconds to chrono::Datetime #134

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 14, 2024
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
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