Truncate fractional seconds to picoseconds

Close https://github.com/alexcrichton/toml-rs/issues/186
This commit is contained in:
Alan Du 2017-06-01 20:35:39 +01:00
parent 10d15333b4
commit 4e246b2142
4 changed files with 14 additions and 1 deletions

View file

@ -218,7 +218,10 @@ impl FromStr for Datetime {
return Err(DatetimeParseError { _private: () }) return Err(DatetimeParseError { _private: () })
} }
chars = whole[end..].chars(); chars = whole[end..].chars();
match format!("0.{}", &whole[..end]).parse() {
// truncate to picoseconds precision
let last = if end > 12 { 12 } else { end };
match format!("0.{}", &whole[..last]).parse() {
Ok(f) => f, Ok(f) => f,
Err(_) => return Err(DatetimeParseError { _private: () }), Err(_) => return Err(DatetimeParseError { _private: () }),
} }

View file

@ -190,6 +190,9 @@ test!(example_bom,
include_str!("valid/example-bom.toml"), include_str!("valid/example-bom.toml"),
include_str!("valid/example.json")); include_str!("valid/example.json"));
test!(datetime_truncate,
include_str!("valid/datetime-truncate.toml"),
include_str!("valid/datetime-truncate.json"));
test!(table_array_nest_no_keys, test!(table_array_nest_no_keys,
include_str!("valid/table-array-nest-no-keys.toml"), include_str!("valid/table-array-nest-no-keys.toml"),
include_str!("valid/table-array-nest-no-keys.json")); include_str!("valid/table-array-nest-no-keys.json"));

View file

@ -0,0 +1,6 @@
{
"bestdayever": {
"type": "datetime",
"value": "1987-07-05T17:45:00.123456789012Z"
}
}

View file

@ -0,0 +1 @@
bestdayever = 1987-07-05T17:45:00.123456789012345Z