use chrono::{DateTime, NaiveDate, NaiveDateTime, Utc};
use serde_json::Value;
pub trait DinocoJson: Sized + Send + Sync + 'static {
fn from_json_row(value: &Value) -> Option<Self>;
}
pub fn datetime_from_json(value: &Value) -> Option<DateTime<Utc>> {
let text = value.as_str()?;
if let Ok(value) = DateTime::parse_from_rfc3339(text) {
return Some(value.with_timezone(&Utc));
}
for format in ["%Y-%m-%d %H:%M:%S%.f", "%Y-%m-%dT%H:%M:%S%.f"] {
if let Ok(value) = NaiveDateTime::parse_from_str(text, format) {
return Some(value.and_utc());
}
}
None
}
pub fn naive_date_from_json(value: &Value) -> Option<NaiveDate> {
let text = value.as_str()?;
if let Ok(value) = NaiveDate::parse_from_str(text, "%Y-%m-%d") {
return Some(value);
}
text.get(0..10).and_then(|prefix| NaiveDate::parse_from_str(prefix, "%Y-%m-%d").ok())
}