async-graphql/src/scalars/datetime.rs

20 lines
454 B
Rust
Raw Normal View History

2020-03-03 11:15:18 +00:00
use crate::{Result, Scalar, Value};
2020-03-01 10:54:34 +00:00
use chrono::{DateTime, TimeZone, Utc};
2020-03-01 13:35:39 +00:00
impl Scalar for DateTime<Utc> {
2020-03-01 10:54:34 +00:00
fn type_name() -> &'static str {
"DateTime"
}
2020-03-04 02:38:07 +00:00
fn parse(value: &Value) -> Option<Self> {
2020-03-01 10:54:34 +00:00
match value {
2020-03-03 11:15:18 +00:00
Value::String(s) => Some(Utc.datetime_from_str(&s, "%+").ok()?),
_ => None,
2020-03-01 10:54:34 +00:00
}
}
2020-03-02 00:24:49 +00:00
fn to_json(&self) -> Result<serde_json::Value> {
2020-03-01 10:54:34 +00:00
Ok(self.to_rfc3339().into())
}
}