Skip to content

Commit 922c8c3

Browse files
author
Matthew Badger
committed
Enable serializing DateTime<Utc>
1 parent 1a4e399 commit 922c8c3

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

influxdb/src/query/write_query.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use crate::query::line_proto_term::LineProtoTerm;
66
use crate::query::{QueryType, ValidQuery};
77
use crate::{Error, Query, Timestamp};
8+
use chrono::{DateTime, TimeZone};
89
use std::fmt::{Display, Formatter};
910

1011
pub trait WriteType {
@@ -152,6 +153,20 @@ impl From<&str> for Type {
152153
Type::Text(b.into())
153154
}
154155
}
156+
157+
impl<Tz: TimeZone> From<DateTime<Tz>> for Type {
158+
fn from(dt: DateTime<Tz>) -> Self {
159+
match dt.timestamp_nanos_opt() {
160+
Some(nanos) => Type::SignedInteger(nanos),
161+
None => {
162+
// For dates before 1677-09-21, or after
163+
// 2262-04-11, we're just going to return 0.
164+
Type::SignedInteger(0)
165+
}
166+
}
167+
}
168+
}
169+
155170
impl<T> From<&T> for Type
156171
where
157172
T: Copy + Into<Type>,

0 commit comments

Comments
 (0)