From 6996fae099cec6b156294588cb3f9b97dfbb1413 Mon Sep 17 00:00:00 2001 From: Samuel Hurel Date: Fri, 24 Apr 2020 18:58:22 +0200 Subject: [PATCH] Add bson::UtcDateTime support --- src/scalars/bson.rs | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/src/scalars/bson.rs b/src/scalars/bson.rs index 96f8020c..a1b93349 100644 --- a/src/scalars/bson.rs +++ b/src/scalars/bson.rs @@ -1,5 +1,6 @@ use crate::{impl_scalar_internal, Result, Scalar, Value}; -use bson::oid::ObjectId; +use bson::{oid::ObjectId, UtcDateTime}; +use chrono::{DateTime, Utc}; impl Scalar for ObjectId { fn type_name() -> &'static str { @@ -19,3 +20,19 @@ impl Scalar for ObjectId { } impl_scalar_internal!(ObjectId); + +impl Scalar for UtcDateTime { + fn type_name() -> &'static str { + "DateTime" + } + + fn parse(value: &Value) -> Option { + DateTime::::parse(value).map(UtcDateTime::from) + } + + fn to_json(&self) -> Result { + (**self).to_json() + } +} + +impl_scalar_internal!(UtcDateTime);