How to parse a date from a string with the last_two year representation enabled. #570
-
As per the title, I am trying to parse a date string of the format: Using Date::parse(). However I am getting the error that the "'Parsed' struct did not include enough information to construct the type". I suspect this is related to the last_two representation of the year being ambiguous to the parser, however I don't know how I am supposed to use the last_two year representation if that is the case. How is the last_two year representation intended to be used? On a side note, how does the padding field interact with the last_two representation? The documentation states that the padding has a width of four, is this reduced to two when the last_two representation is used? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
You're correct that the parser ignores the "last two" representation when parsing due to the ambiguity. Right now, the only way to handle this is to go through the use time::parsing::Parsed;
use time::macros::*;
const FORMAT: &[FormatItem<'_>] = format_description!("[month padding:none]/[day]/[year repr:last_two]");
// replace this with the actual input
let input = "4/09/23";
// parse the items from the input
let mut parsed = Parsed::new();
parsed.parse_items(input, FORMAT)?;
// set the year however you want
let year = parsed.year_last_two()? + 2000;
parsed.set_year(year)?;
// convert into the final type
let date = Date::try_from(parsed)?; I've written this off-hand, so minor modifications will likely be necessary to make it compile. As an aside, you don't need to specify |
Beta Was this translation helpful? Give feedback.
You're correct that the parser ignores the "last two" representation when parsing due to the ambiguity. Right now, the only way to handle this is to go through the
time::parsing::Parsed
struct. For example:I…