-
Notifications
You must be signed in to change notification settings - Fork 542
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
Chrono can't parse a date from iso8601 #163
Comments
@quodlibetor I don't parse it manually, I use |
In that case if you need to use it you can write Something like the following (untested) code: pub mod custom_deserialize {
use std::fmt;
use serde::de;
use chrono::NaiveDateTime;
struct NaiveDateTimeFromMyFormatVisitor;
pub fn deserialize<'de, D>(d: D) -> Result<NaiveDateTime, D::Error>
where D: de::Deserializer<'de>
{
Ok(try!(d.deserialize_str(NaiveDateTimeFromMyFormatVisitor)))
}
impl<'de> de::Visitor<'de> for NaiveDateTimeFromSecondsVisitor {
type Value = NaiveDateTime;
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result
{
write!(formatter, "a datetime string")
}
fn visit_str<E>(self, value: &str) -> Result<NaiveDateTime, E>
where E: de::Error
{
DateTime::parse_from_str(value, "%Y-%m-%d %H:%M:%S%z")
.map_err(|e| E::custom(format!("Parse error {} for {}", e, value)))
}
}
} Should allow you to use serde's |
@quodlibetor thanks a lot, I will try that if the resource refuse to support the correct version of time stamp. |
@quodlibetor the service api author gave me this link which says the |
Ha, the specific text in the note in wikipedia is:
so, if you agree that the Adding the ability to accept the space would require an attempted parse and a fallback. That seems like an unacceptable hidden performance hit for default behavior. I'll investigate alternate parsing techniques, but it won't be super fast, our current code is pretty... specific. If you want to accept strings in the format that you are currently receiving I'd recommend going with the alternate deserialization I described in a previous comment. |
Some server sends me the date time in the json:
But I can't parse it neither trough
Datetime
norNaiveDateTime
. However, if I change the space between the date and the time toT
as in2017-06-22T13:32:16Z
it parses okay.I could not find any information does this conform iso8601 or not, different sources say different answers, iso8601. If it is
iso8601
so thechrono
must be able to parse it.The text was updated successfully, but these errors were encountered: