0.5: Support space separate in datetime.

cc #224
This commit is contained in:
Eric Huss 2018-07-10 17:14:16 -07:00
parent 04c1fe9df6
commit 5204d8b50a
3 changed files with 19 additions and 3 deletions

View file

@ -176,7 +176,8 @@ impl FromStr for Datetime {
// Next parse the "partial-time" if available // Next parse the "partial-time" if available
let partial_time = if full_date.is_some() && let partial_time = if full_date.is_some() &&
chars.clone().next() == Some('T') { (chars.clone().next() == Some('T')
|| chars.clone().next() == Some(' ')) {
chars.next(); chars.next();
true true
} else { } else {

View file

@ -1010,6 +1010,17 @@ impl<'a> Deserializer<'a> {
-> Result<(Span, &'a str), Error> { -> Result<(Span, &'a str), Error> {
let start = self.tokens.substr_offset(date); let start = self.tokens.substr_offset(date);
// Check for space separated date and time.
if let Some((_, Token::Whitespace(s))) = self.peek()? {
if s == " " {
self.next()?;
// Skip past the hour.
if let Some((_, Token::Keylike(_))) = self.peek()? {
self.next()?;
}
}
}
if colon_eaten || self.eat(Token::Colon)? { if colon_eaten || self.eat(Token::Colon)? {
// minutes // minutes
match self.next()? { match self.next()? {

View file

@ -6,10 +6,14 @@ use toml::Value;
#[test] #[test]
fn times() { fn times() {
fn good(s: &str) { fn dogood(s: &str, serialized: &str) {
let to_parse = format!("foo = {}", s); let to_parse = format!("foo = {}", s);
let value = Value::from_str(&to_parse).unwrap(); let value = Value::from_str(&to_parse).unwrap();
assert_eq!(value["foo"].as_datetime().unwrap().to_string(), s); assert_eq!(value["foo"].as_datetime().unwrap().to_string(), serialized);
}
fn good(s: &str) {
dogood(s, s);
dogood(&s.replace("T", " "), s);
} }
good("1997-09-09T09:09:09Z"); good("1997-09-09T09:09:09Z");