Skip to content

Commit 4a93896

Browse files
authored
Fix bug with conversion of Timestamp::Microseconds to chrono::Datetime (#134)
1 parent a7a56df commit 4a93896

File tree

2 files changed

+9
-9
lines changed

2 files changed

+9
-9
lines changed

influxdb/src/query/consts.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,4 @@ pub const MINUTES_PER_HOUR: u128 = 60;
22
pub const SECONDS_PER_MINUTE: u128 = 60;
33
pub const MILLIS_PER_SECOND: u128 = 1000;
44
pub const NANOS_PER_MILLI: u128 = 1_000_000;
5-
6-
#[cfg(test)]
7-
pub const MICROS_PER_NANO: u128 = 1000;
5+
pub const NANOS_PER_MICRO: u128 = 1000;

influxdb/src/query/mod.rs

+8-6
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@ pub mod write_query;
3030
use std::fmt;
3131

3232
use crate::{Error, ReadQuery, WriteQuery};
33-
use consts::{MILLIS_PER_SECOND, MINUTES_PER_HOUR, NANOS_PER_MILLI, SECONDS_PER_MINUTE};
33+
use consts::{
34+
MILLIS_PER_SECOND, MINUTES_PER_HOUR, NANOS_PER_MICRO, NANOS_PER_MILLI, SECONDS_PER_MINUTE,
35+
};
3436

3537
#[cfg(feature = "derive")]
3638
pub use influxdb_derive::InfluxDbWriteable;
@@ -76,8 +78,8 @@ impl From<Timestamp> for DateTime<Utc> {
7678
Utc.timestamp_nanos(nanos.try_into().unwrap())
7779
}
7880
Timestamp::Nanoseconds(nanos) => Utc.timestamp_nanos(nanos.try_into().unwrap()),
79-
Timestamp::Microseconds(mis) => {
80-
let nanos = mis / 10000;
81+
Timestamp::Microseconds(micros) => {
82+
let nanos = micros * NANOS_PER_MICRO;
8183
Utc.timestamp_nanos(nanos.try_into().unwrap())
8284
}
8385
}
@@ -230,7 +232,7 @@ pub enum QueryType {
230232
#[cfg(test)]
231233
mod tests {
232234
use super::consts::{
233-
MICROS_PER_NANO, MILLIS_PER_SECOND, MINUTES_PER_HOUR, NANOS_PER_MILLI, SECONDS_PER_MINUTE,
235+
MILLIS_PER_SECOND, MINUTES_PER_HOUR, NANOS_PER_MICRO, NANOS_PER_MILLI, SECONDS_PER_MINUTE,
234236
};
235237
use crate::query::{Timestamp, ValidQuery};
236238
use chrono::prelude::{DateTime, TimeZone, Utc};
@@ -301,9 +303,9 @@ mod tests {
301303
}
302304
#[test]
303305
fn test_chrono_datetime_from_timestamp_micros() {
304-
let datetime_from_timestamp: DateTime<Utc> = Timestamp::Microseconds(1).into();
306+
let datetime_from_timestamp: DateTime<Utc> = Timestamp::Microseconds(2).into();
305307
assert_eq!(
306-
Utc.timestamp_nanos((1 / MICROS_PER_NANO).try_into().unwrap()),
308+
Utc.timestamp_nanos((2 * NANOS_PER_MICRO).try_into().unwrap()),
307309
datetime_from_timestamp
308310
)
309311
}

0 commit comments

Comments
 (0)