Skip to content
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

Add time crate support for Timestamp #151

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
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
Next Next commit
Add time crate support
pbzweihander committed Mar 25, 2024

Verified

This commit was signed with the committer’s verified signature.
pbzweihander Kangwook Lee (이강욱)
commit bd69769dc7a80dd8f15fc75c53b022324f543442
1 change: 1 addition & 0 deletions influxdb/Cargo.toml
Original file line number Diff line number Diff line change
@@ -24,6 +24,7 @@ serde = { version = "1.0.186", optional = true }
serde_derive = { version = "1.0.186", optional = true }
serde_json = { version = "1.0.48", optional = true }
thiserror = "1.0"
time = "0.3.34"

[features]
default = ["serde", "reqwest-client-rustls"]
53 changes: 28 additions & 25 deletions influxdb/src/query/mod.rs
Original file line number Diff line number Diff line change
@@ -21,7 +21,6 @@
//! ```
use chrono::prelude::{DateTime, TimeZone, Utc};
use std::convert::TryInto;

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

impl Timestamp {
pub fn nanos(&self) -> u128 {
match self {
Timestamp::Hours(h) => {
h * MINUTES_PER_HOUR * SECONDS_PER_MINUTE * MILLIS_PER_SECOND * NANOS_PER_MILLI
}
Timestamp::Minutes(m) => m * SECONDS_PER_MINUTE * MILLIS_PER_SECOND * NANOS_PER_MILLI,
Timestamp::Seconds(s) => s * MILLIS_PER_SECOND * NANOS_PER_MILLI,
Timestamp::Milliseconds(millis) => millis * NANOS_PER_MILLI,
Timestamp::Microseconds(micros) => micros * NANOS_PER_MICRO,
Timestamp::Nanoseconds(nanos) => *nanos,
}
}
}

impl fmt::Display for Timestamp {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
use Timestamp::*;
@@ -59,30 +73,7 @@ impl fmt::Display for Timestamp {

impl From<Timestamp> for DateTime<Utc> {
fn from(ts: Timestamp) -> DateTime<Utc> {
match ts {
Timestamp::Hours(h) => {
let nanos =
h * MINUTES_PER_HOUR * SECONDS_PER_MINUTE * MILLIS_PER_SECOND * NANOS_PER_MILLI;
Utc.timestamp_nanos(nanos.try_into().unwrap())
}
Timestamp::Minutes(m) => {
let nanos = m * SECONDS_PER_MINUTE * MILLIS_PER_SECOND * NANOS_PER_MILLI;
Utc.timestamp_nanos(nanos.try_into().unwrap())
}
Timestamp::Seconds(s) => {
let nanos = s * MILLIS_PER_SECOND * NANOS_PER_MILLI;
Utc.timestamp_nanos(nanos.try_into().unwrap())
}
Timestamp::Milliseconds(millis) => {
let nanos = millis * NANOS_PER_MILLI;
Utc.timestamp_nanos(nanos.try_into().unwrap())
}
Timestamp::Nanoseconds(nanos) => Utc.timestamp_nanos(nanos.try_into().unwrap()),
Timestamp::Microseconds(micros) => {
let nanos = micros * NANOS_PER_MICRO;
Utc.timestamp_nanos(nanos.try_into().unwrap())
}
}
Utc.timestamp_nanos(ts.nanos() as i64)
}
}

@@ -95,6 +86,18 @@ where
}
}

impl From<Timestamp> for time::OffsetDateTime {
fn from(value: Timestamp) -> Self {
time::OffsetDateTime::from_unix_timestamp_nanos(value.nanos() as i128).unwrap()
}
}

impl From<time::OffsetDateTime> for Timestamp {
fn from(value: time::OffsetDateTime) -> Self {
Timestamp::Nanoseconds(value.unix_timestamp_nanos() as u128)
}
}

pub trait Query {
/// Builds valid InfluxSQL which can be run against the Database.
/// In case no fields have been specified, it will return an error,