Skip to content

Commit bd69769

Browse files
committed
Add time crate support
1 parent bfe457c commit bd69769

File tree

2 files changed

+29
-25
lines changed

2 files changed

+29
-25
lines changed

Diff for: influxdb/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ serde = { version = "1.0.186", optional = true }
2424
serde_derive = { version = "1.0.186", optional = true }
2525
serde_json = { version = "1.0.48", optional = true }
2626
thiserror = "1.0"
27+
time = "0.3.34"
2728

2829
[features]
2930
default = ["serde", "reqwest-client-rustls"]

Diff for: influxdb/src/query/mod.rs

+28-25
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
//! ```
2222
2323
use chrono::prelude::{DateTime, TimeZone, Utc};
24-
use std::convert::TryInto;
2524

2625
pub mod consts;
2726
mod line_proto_term;
@@ -47,6 +46,21 @@ pub enum Timestamp {
4746
Hours(u128),
4847
}
4948

49+
impl Timestamp {
50+
pub fn nanos(&self) -> u128 {
51+
match self {
52+
Timestamp::Hours(h) => {
53+
h * MINUTES_PER_HOUR * SECONDS_PER_MINUTE * MILLIS_PER_SECOND * NANOS_PER_MILLI
54+
}
55+
Timestamp::Minutes(m) => m * SECONDS_PER_MINUTE * MILLIS_PER_SECOND * NANOS_PER_MILLI,
56+
Timestamp::Seconds(s) => s * MILLIS_PER_SECOND * NANOS_PER_MILLI,
57+
Timestamp::Milliseconds(millis) => millis * NANOS_PER_MILLI,
58+
Timestamp::Microseconds(micros) => micros * NANOS_PER_MICRO,
59+
Timestamp::Nanoseconds(nanos) => *nanos,
60+
}
61+
}
62+
}
63+
5064
impl fmt::Display for Timestamp {
5165
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
5266
use Timestamp::*;
@@ -59,30 +73,7 @@ impl fmt::Display for Timestamp {
5973

6074
impl From<Timestamp> for DateTime<Utc> {
6175
fn from(ts: Timestamp) -> DateTime<Utc> {
62-
match ts {
63-
Timestamp::Hours(h) => {
64-
let nanos =
65-
h * MINUTES_PER_HOUR * SECONDS_PER_MINUTE * MILLIS_PER_SECOND * NANOS_PER_MILLI;
66-
Utc.timestamp_nanos(nanos.try_into().unwrap())
67-
}
68-
Timestamp::Minutes(m) => {
69-
let nanos = m * SECONDS_PER_MINUTE * MILLIS_PER_SECOND * NANOS_PER_MILLI;
70-
Utc.timestamp_nanos(nanos.try_into().unwrap())
71-
}
72-
Timestamp::Seconds(s) => {
73-
let nanos = s * MILLIS_PER_SECOND * NANOS_PER_MILLI;
74-
Utc.timestamp_nanos(nanos.try_into().unwrap())
75-
}
76-
Timestamp::Milliseconds(millis) => {
77-
let nanos = millis * NANOS_PER_MILLI;
78-
Utc.timestamp_nanos(nanos.try_into().unwrap())
79-
}
80-
Timestamp::Nanoseconds(nanos) => Utc.timestamp_nanos(nanos.try_into().unwrap()),
81-
Timestamp::Microseconds(micros) => {
82-
let nanos = micros * NANOS_PER_MICRO;
83-
Utc.timestamp_nanos(nanos.try_into().unwrap())
84-
}
85-
}
76+
Utc.timestamp_nanos(ts.nanos() as i64)
8677
}
8778
}
8879

@@ -95,6 +86,18 @@ where
9586
}
9687
}
9788

89+
impl From<Timestamp> for time::OffsetDateTime {
90+
fn from(value: Timestamp) -> Self {
91+
time::OffsetDateTime::from_unix_timestamp_nanos(value.nanos() as i128).unwrap()
92+
}
93+
}
94+
95+
impl From<time::OffsetDateTime> for Timestamp {
96+
fn from(value: time::OffsetDateTime) -> Self {
97+
Timestamp::Nanoseconds(value.unix_timestamp_nanos() as u128)
98+
}
99+
}
100+
98101
pub trait Query {
99102
/// Builds valid InfluxSQL which can be run against the Database.
100103
/// In case no fields have been specified, it will return an error,

0 commit comments

Comments
 (0)