fix: changed DateValue to both be UTC and be serialized reliably
This commit is contained in:
parent
b664f777d3
commit
1686293bac
1 changed files with 4 additions and 6 deletions
10
src/lib.rs
10
src/lib.rs
|
|
@ -1,6 +1,6 @@
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
|
||||||
use chrono::{DateTime, NaiveTime, Utc};
|
use chrono::{DateTime, Utc};
|
||||||
use lazy_static::lazy_static;
|
use lazy_static::lazy_static;
|
||||||
use regex::Regex;
|
use regex::Regex;
|
||||||
use serde::de::Error as SerdeError;
|
use serde::de::Error as SerdeError;
|
||||||
|
|
@ -1573,8 +1573,8 @@ impl std::fmt::Display for Date {
|
||||||
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
|
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
|
||||||
#[serde(try_from = "String", into = "String")]
|
#[serde(try_from = "String", into = "String")]
|
||||||
pub enum DateValue {
|
pub enum DateValue {
|
||||||
|
Date(DateTime<Utc>),
|
||||||
DateTime(DateTime<Utc>),
|
DateTime(DateTime<Utc>),
|
||||||
Date(chrono::NaiveDate),
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl TryFrom<String> for DateValue {
|
impl TryFrom<String> for DateValue {
|
||||||
|
|
@ -1583,9 +1583,7 @@ impl TryFrom<String> for DateValue {
|
||||||
fn try_from(string: String) -> Result<DateValue> {
|
fn try_from(string: String) -> Result<DateValue> {
|
||||||
// NOTE: is either ISO 8601 Date or assumed to be ISO 8601 DateTime
|
// NOTE: is either ISO 8601 Date or assumed to be ISO 8601 DateTime
|
||||||
let value = if ISO_8601_DATE.is_match(&string) {
|
let value = if ISO_8601_DATE.is_match(&string) {
|
||||||
DateValue::Date(
|
DateValue::Date(DateTime::parse_from_rfc3339(&format!("{string}T00:00:00Z"))?.into())
|
||||||
DateTime::parse_from_rfc3339(&format!("{string}T00:00:00Z"))?.date_naive(),
|
|
||||||
)
|
|
||||||
} else {
|
} else {
|
||||||
DateValue::DateTime(DateTime::parse_from_rfc3339(&string)?.with_timezone(&Utc))
|
DateValue::DateTime(DateTime::parse_from_rfc3339(&string)?.with_timezone(&Utc))
|
||||||
};
|
};
|
||||||
|
|
@ -1603,7 +1601,7 @@ impl From<DateValue> for String {
|
||||||
impl std::fmt::Display for DateValue {
|
impl std::fmt::Display for DateValue {
|
||||||
fn fmt(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
|
fn fmt(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
|
||||||
let value = match self {
|
let value = match self {
|
||||||
DateValue::Date(date) => date.and_time(NaiveTime::MIN).and_utc().to_rfc3339(),
|
DateValue::Date(date) => date.format("%Y-%m-%d").to_string(),
|
||||||
DateValue::DateTime(date_time) => date_time.to_rfc3339(),
|
DateValue::DateTime(date_time) => date_time.to_rfc3339(),
|
||||||
};
|
};
|
||||||
Ok(write!(formatter, "{}", value)?)
|
Ok(write!(formatter, "{}", value)?)
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue