fix: changed DateValue to both be UTC and be serialized reliably

This commit is contained in:
Bram Dingelstad 2025-04-11 14:17:39 +02:00
parent b664f777d3
commit 1686293bac

View file

@ -1,6 +1,6 @@
use std::collections::HashMap;
use chrono::{DateTime, NaiveTime, Utc};
use chrono::{DateTime, Utc};
use lazy_static::lazy_static;
use regex::Regex;
use serde::de::Error as SerdeError;
@ -1573,8 +1573,8 @@ impl std::fmt::Display for Date {
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
#[serde(try_from = "String", into = "String")]
pub enum DateValue {
Date(DateTime<Utc>),
DateTime(DateTime<Utc>),
Date(chrono::NaiveDate),
}
impl TryFrom<String> for DateValue {
@ -1583,9 +1583,7 @@ impl TryFrom<String> for DateValue {
fn try_from(string: String) -> Result<DateValue> {
// NOTE: is either ISO 8601 Date or assumed to be ISO 8601 DateTime
let value = if ISO_8601_DATE.is_match(&string) {
DateValue::Date(
DateTime::parse_from_rfc3339(&format!("{string}T00:00:00Z"))?.date_naive(),
)
DateValue::Date(DateTime::parse_from_rfc3339(&format!("{string}T00:00:00Z"))?.into())
} else {
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 {
fn fmt(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
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(),
};
Ok(write!(formatter, "{}", value)?)