Skip to content

Commit 54bee37

Browse files
committed
Make time and chrono optional
1 parent bd69769 commit 54bee37

File tree

2 files changed

+32
-12
lines changed

2 files changed

+32
-12
lines changed

Diff for: influxdb/Cargo.toml

+7-3
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,18 @@ include = ["src/**/*", "tests/**/*", "Cargo.toml", "LICENSE"]
1313
repository = "https://github.com/influxdb-rs/influxdb-rust"
1414

1515
[dependencies]
16-
chrono = { version = "0.4.23", features = ["serde"], default-features = false }
16+
chrono = { version = "0.4.23", features = ["serde"], default-features = false, optional = true }
1717
futures-util = "0.3.17"
1818
http = "0.2.4"
1919
influxdb_derive = { version = "0.5.1", optional = true }
2020
lazy-regex = "3.1"
2121
reqwest = { version = "0.11.4", default-features = false, optional = true }
22-
surf = { version = "2.2.0", default-features = false, optional = true }
2322
serde = { version = "1.0.186", optional = true }
2423
serde_derive = { version = "1.0.186", optional = true }
2524
serde_json = { version = "1.0.48", optional = true }
25+
surf = { version = "2.2.0", default-features = false, optional = true }
2626
thiserror = "1.0"
27-
time = "0.3.34"
27+
time = { version = "0.3.34", optional = true }
2828

2929
[features]
3030
default = ["serde", "reqwest-client-rustls"]
@@ -41,6 +41,10 @@ reqwest-client-native-tls = ["reqwest", "reqwest/native-tls-alpn"]
4141
reqwest-client-native-tls-vendored = ["reqwest", "reqwest/native-tls-vendored"]
4242
wasm-client = ["surf", "surf/wasm-client"]
4343

44+
# etc
45+
time = ["dep:time"]
46+
chrono = ["dep:chrono"]
47+
4448
[dev-dependencies]
4549
async-std = { version = "1.6.5", features = ["attributes", "tokio02", "tokio1"] }
4650
indoc = "1.0"

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

+25-9
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@
2020
//! assert!(read_query.is_ok());
2121
//! ```
2222
23-
use chrono::prelude::{DateTime, TimeZone, Utc};
24-
2523
pub mod consts;
2624
mod line_proto_term;
2725
pub mod read_query;
@@ -71,27 +69,32 @@ impl fmt::Display for Timestamp {
7169
}
7270
}
7371

74-
impl From<Timestamp> for DateTime<Utc> {
75-
fn from(ts: Timestamp) -> DateTime<Utc> {
76-
Utc.timestamp_nanos(ts.nanos() as i64)
72+
#[cfg(feature = "chrono")]
73+
impl From<Timestamp> for chrono::prelude::DateTime<chrono::prelude::Utc> {
74+
fn from(ts: Timestamp) -> chrono::prelude::DateTime<chrono::prelude::Utc> {
75+
use chrono::prelude::TimeZone;
76+
chrono::prelude::Utc.timestamp_nanos(ts.nanos() as i64)
7777
}
7878
}
7979

80-
impl<T> From<DateTime<T>> for Timestamp
80+
#[cfg(feature = "chrono")]
81+
impl<T> From<chrono::prelude::DateTime<T>> for Timestamp
8182
where
82-
T: TimeZone,
83+
T: chrono::prelude::TimeZone,
8384
{
84-
fn from(date_time: DateTime<T>) -> Self {
85+
fn from(date_time: chrono::prelude::DateTime<T>) -> Self {
8586
Timestamp::Nanoseconds(date_time.timestamp_nanos_opt().unwrap() as u128)
8687
}
8788
}
8889

90+
#[cfg(feature = "time")]
8991
impl From<Timestamp> for time::OffsetDateTime {
9092
fn from(value: Timestamp) -> Self {
9193
time::OffsetDateTime::from_unix_timestamp_nanos(value.nanos() as i128).unwrap()
9294
}
9395
}
9496

97+
#[cfg(feature = "time")]
9598
impl From<time::OffsetDateTime> for Timestamp {
9699
fn from(value: time::OffsetDateTime) -> Self {
97100
Timestamp::Nanoseconds(value.unix_timestamp_nanos() as u128)
@@ -238,7 +241,6 @@ mod tests {
238241
MILLIS_PER_SECOND, MINUTES_PER_HOUR, NANOS_PER_MICRO, NANOS_PER_MILLI, SECONDS_PER_MINUTE,
239242
};
240243
use crate::query::{Timestamp, ValidQuery};
241-
use chrono::prelude::{DateTime, TimeZone, Utc};
242244
use std::convert::TryInto;
243245
#[test]
244246
fn test_equality_str() {
@@ -255,8 +257,10 @@ mod tests {
255257
fn test_format_for_timestamp_else() {
256258
assert!(format!("{}", Timestamp::Nanoseconds(100)) == "100");
257259
}
260+
#[cfg(feature = "chrono")]
258261
#[test]
259262
fn test_chrono_datetime_from_timestamp_hours() {
263+
use chrono::prelude::*;
260264
let datetime_from_timestamp: DateTime<Utc> = Timestamp::Hours(2).into();
261265
assert_eq!(
262266
Utc.timestamp_nanos(
@@ -267,8 +271,10 @@ mod tests {
267271
datetime_from_timestamp
268272
)
269273
}
274+
#[cfg(feature = "chrono")]
270275
#[test]
271276
fn test_chrono_datetime_from_timestamp_minutes() {
277+
use chrono::prelude::*;
272278
let datetime_from_timestamp: DateTime<Utc> = Timestamp::Minutes(2).into();
273279
assert_eq!(
274280
Utc.timestamp_nanos(
@@ -279,8 +285,10 @@ mod tests {
279285
datetime_from_timestamp
280286
)
281287
}
288+
#[cfg(feature = "chrono")]
282289
#[test]
283290
fn test_chrono_datetime_from_timestamp_seconds() {
291+
use chrono::prelude::*;
284292
let datetime_from_timestamp: DateTime<Utc> = Timestamp::Seconds(2).into();
285293
assert_eq!(
286294
Utc.timestamp_nanos(
@@ -291,29 +299,37 @@ mod tests {
291299
datetime_from_timestamp
292300
)
293301
}
302+
#[cfg(feature = "chrono")]
294303
#[test]
295304
fn test_chrono_datetime_from_timestamp_millis() {
305+
use chrono::prelude::*;
296306
let datetime_from_timestamp: DateTime<Utc> = Timestamp::Milliseconds(2).into();
297307
assert_eq!(
298308
Utc.timestamp_nanos((2 * NANOS_PER_MILLI).try_into().unwrap()),
299309
datetime_from_timestamp
300310
)
301311
}
312+
#[cfg(feature = "chrono")]
302313
#[test]
303314
fn test_chrono_datetime_from_timestamp_nanos() {
315+
use chrono::prelude::*;
304316
let datetime_from_timestamp: DateTime<Utc> = Timestamp::Nanoseconds(1).into();
305317
assert_eq!(Utc.timestamp_nanos(1), datetime_from_timestamp)
306318
}
319+
#[cfg(feature = "chrono")]
307320
#[test]
308321
fn test_chrono_datetime_from_timestamp_micros() {
322+
use chrono::prelude::*;
309323
let datetime_from_timestamp: DateTime<Utc> = Timestamp::Microseconds(2).into();
310324
assert_eq!(
311325
Utc.timestamp_nanos((2 * NANOS_PER_MICRO).try_into().unwrap()),
312326
datetime_from_timestamp
313327
)
314328
}
329+
#[cfg(feature = "chrono")]
315330
#[test]
316331
fn test_timestamp_from_chrono_date() {
332+
use chrono::prelude::*;
317333
let timestamp_from_datetime: Timestamp = Utc
318334
.with_ymd_and_hms(1970, 1, 1, 0, 0, 1)
319335
.single()

0 commit comments

Comments
 (0)