Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deserializing DateTime from unix timestamp is not implemented, despite description #1484

Closed
jmaximusix opened this issue Mar 4, 2024 · 1 comment · Fixed by #1519
Closed

Comments

@jmaximusix
Copy link

The Deserialize Trait can't deserialize DateTimes from timestamps despite the error message you get. I tried tracking it down in the source code, I hope the cited references are correct.

The Deserialize Trait only calls the deserialize_str() function

impl<'de> de::Deserialize<'de> for DateTime<FixedOffset> {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: de::Deserializer<'de>,
{
deserializer.deserialize_str(DateTimeVisitor)
}
}

where, in spite of the expecting() stating it accepts "a formatted date and time string or a unix timestamp" it only calls the parse() function

impl<'de> de::Visitor<'de> for DateTimeVisitor {
type Value = DateTime<FixedOffset>;
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
formatter.write_str("a formatted date and time string or a unix timestamp")
}
fn visit_str<E>(self, value: &str) -> Result<Self::Value, E>
where
E: de::Error,
{
value.parse().map_err(E::custom)
}
}

which in turn is implemented to only try and deserialize rfc3339 strings

chrono/src/format/parse.rs

Lines 523 to 534 in f4adc28

impl str::FromStr for DateTime<FixedOffset> {
type Err = ParseError;
fn from_str(s: &str) -> ParseResult<DateTime<FixedOffset>> {
let mut parsed = Parsed::new();
let (s, _) = parse_rfc3339_relaxed(&mut parsed, s)?;
if !s.trim_start().is_empty() {
return Err(TOO_LONG);
}
parsed.to_datetime()
}
}

@jmaximusix jmaximusix changed the title Deserializing from a unix timestamp is not implemented, despite description Deserializing DateTime from unix timestamp is not implemented, despite description Mar 4, 2024
@pitdicker
Copy link
Collaborator

That error message, "a formatted date and time string or a unix timestamp", seems not right indeed.

We do not really have a solution yet. There is an old PR to add deserializing from strings to the existing timestamp deserializers (#926). And another one to add full string serialization and deserialization modules (#1067).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants