Skip to content

How to parse a date from a string with the last_two year representation enabled. #570

Closed Answered by jhpratt
thepackett asked this question in Question
Discussion options

You must be logged in to vote

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:

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…

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@thepackett
Comment options

Answer selected by thepackett
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants