20
20
//! assert!(read_query.is_ok());
21
21
//! ```
22
22
23
- use chrono:: prelude:: { DateTime , TimeZone , Utc } ;
24
-
25
23
pub mod consts;
26
24
mod line_proto_term;
27
25
pub mod read_query;
@@ -71,27 +69,32 @@ impl fmt::Display for Timestamp {
71
69
}
72
70
}
73
71
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 )
77
77
}
78
78
}
79
79
80
- impl < T > From < DateTime < T > > for Timestamp
80
+ #[ cfg( feature = "chrono" ) ]
81
+ impl < T > From < chrono:: prelude:: DateTime < T > > for Timestamp
81
82
where
82
- T : TimeZone ,
83
+ T : chrono :: prelude :: TimeZone ,
83
84
{
84
- fn from ( date_time : DateTime < T > ) -> Self {
85
+ fn from ( date_time : chrono :: prelude :: DateTime < T > ) -> Self {
85
86
Timestamp :: Nanoseconds ( date_time. timestamp_nanos_opt ( ) . unwrap ( ) as u128 )
86
87
}
87
88
}
88
89
90
+ #[ cfg( feature = "time" ) ]
89
91
impl From < Timestamp > for time:: OffsetDateTime {
90
92
fn from ( value : Timestamp ) -> Self {
91
93
time:: OffsetDateTime :: from_unix_timestamp_nanos ( value. nanos ( ) as i128 ) . unwrap ( )
92
94
}
93
95
}
94
96
97
+ #[ cfg( feature = "time" ) ]
95
98
impl From < time:: OffsetDateTime > for Timestamp {
96
99
fn from ( value : time:: OffsetDateTime ) -> Self {
97
100
Timestamp :: Nanoseconds ( value. unix_timestamp_nanos ( ) as u128 )
@@ -238,7 +241,6 @@ mod tests {
238
241
MILLIS_PER_SECOND , MINUTES_PER_HOUR , NANOS_PER_MICRO , NANOS_PER_MILLI , SECONDS_PER_MINUTE ,
239
242
} ;
240
243
use crate :: query:: { Timestamp , ValidQuery } ;
241
- use chrono:: prelude:: { DateTime , TimeZone , Utc } ;
242
244
use std:: convert:: TryInto ;
243
245
#[ test]
244
246
fn test_equality_str ( ) {
@@ -255,8 +257,10 @@ mod tests {
255
257
fn test_format_for_timestamp_else ( ) {
256
258
assert ! ( format!( "{}" , Timestamp :: Nanoseconds ( 100 ) ) == "100" ) ;
257
259
}
260
+ #[ cfg( feature = "chrono" ) ]
258
261
#[ test]
259
262
fn test_chrono_datetime_from_timestamp_hours ( ) {
263
+ use chrono:: prelude:: * ;
260
264
let datetime_from_timestamp: DateTime < Utc > = Timestamp :: Hours ( 2 ) . into ( ) ;
261
265
assert_eq ! (
262
266
Utc . timestamp_nanos(
@@ -267,8 +271,10 @@ mod tests {
267
271
datetime_from_timestamp
268
272
)
269
273
}
274
+ #[ cfg( feature = "chrono" ) ]
270
275
#[ test]
271
276
fn test_chrono_datetime_from_timestamp_minutes ( ) {
277
+ use chrono:: prelude:: * ;
272
278
let datetime_from_timestamp: DateTime < Utc > = Timestamp :: Minutes ( 2 ) . into ( ) ;
273
279
assert_eq ! (
274
280
Utc . timestamp_nanos(
@@ -279,8 +285,10 @@ mod tests {
279
285
datetime_from_timestamp
280
286
)
281
287
}
288
+ #[ cfg( feature = "chrono" ) ]
282
289
#[ test]
283
290
fn test_chrono_datetime_from_timestamp_seconds ( ) {
291
+ use chrono:: prelude:: * ;
284
292
let datetime_from_timestamp: DateTime < Utc > = Timestamp :: Seconds ( 2 ) . into ( ) ;
285
293
assert_eq ! (
286
294
Utc . timestamp_nanos(
@@ -291,29 +299,37 @@ mod tests {
291
299
datetime_from_timestamp
292
300
)
293
301
}
302
+ #[ cfg( feature = "chrono" ) ]
294
303
#[ test]
295
304
fn test_chrono_datetime_from_timestamp_millis ( ) {
305
+ use chrono:: prelude:: * ;
296
306
let datetime_from_timestamp: DateTime < Utc > = Timestamp :: Milliseconds ( 2 ) . into ( ) ;
297
307
assert_eq ! (
298
308
Utc . timestamp_nanos( ( 2 * NANOS_PER_MILLI ) . try_into( ) . unwrap( ) ) ,
299
309
datetime_from_timestamp
300
310
)
301
311
}
312
+ #[ cfg( feature = "chrono" ) ]
302
313
#[ test]
303
314
fn test_chrono_datetime_from_timestamp_nanos ( ) {
315
+ use chrono:: prelude:: * ;
304
316
let datetime_from_timestamp: DateTime < Utc > = Timestamp :: Nanoseconds ( 1 ) . into ( ) ;
305
317
assert_eq ! ( Utc . timestamp_nanos( 1 ) , datetime_from_timestamp)
306
318
}
319
+ #[ cfg( feature = "chrono" ) ]
307
320
#[ test]
308
321
fn test_chrono_datetime_from_timestamp_micros ( ) {
322
+ use chrono:: prelude:: * ;
309
323
let datetime_from_timestamp: DateTime < Utc > = Timestamp :: Microseconds ( 2 ) . into ( ) ;
310
324
assert_eq ! (
311
325
Utc . timestamp_nanos( ( 2 * NANOS_PER_MICRO ) . try_into( ) . unwrap( ) ) ,
312
326
datetime_from_timestamp
313
327
)
314
328
}
329
+ #[ cfg( feature = "chrono" ) ]
315
330
#[ test]
316
331
fn test_timestamp_from_chrono_date ( ) {
332
+ use chrono:: prelude:: * ;
317
333
let timestamp_from_datetime: Timestamp = Utc
318
334
. with_ymd_and_hms ( 1970 , 1 , 1 , 0 , 0 , 1 )
319
335
. single ( )
0 commit comments