feat: sane date format

This commit is contained in:
Robert Perce 2026-04-05 13:20:00 -05:00
parent c12975926d
commit 4c710dcd20
4 changed files with 17 additions and 13 deletions

View file

@ -57,21 +57,18 @@ impl Display for YearOptionalDate {
impl FromStr for YearOptionalDate {
type Err = anyhow::Error;
fn from_str(str: &str) -> Result<Self, Self::Err> {
let date_re = Regex::new(r"^([0-9]{4}|--)([0-9]{2})([0-9]{2})$").unwrap();
let date_re = Regex::new(r"^(?:([0-9]{4})-)?([0-9]{2})-([0-9]{2})$").unwrap();
if let Some(caps) = date_re.captures(str) {
let year_str = &caps[1];
let year = caps
.get(1)
.map(|yyyy| i16::from_str(yyyy.as_str()).unwrap());
let month = i8::from_str(&caps[2]).unwrap();
let day = i8::from_str(&caps[3]).unwrap();
let year = if year_str == "--" {
None
} else {
Some(i16::from_str(year_str).unwrap())
};
return Ok(Self { year, month, day });
}
Err(anyhow::Error::msg(format!(
"parsing failure in YearOptionalDate: '{}' does not match regex /([0-9]{{4}}|--)[0-9]{{4}}/",
"parsing failure in YearOptionalDate: '{}' does not match regex /([0-9]{{4}}-)?[0-9]{{2}}-[0-9]{{2}}/",
str
)))
}

View file

@ -278,10 +278,10 @@ mod get {
input name="periodicity" id="periodicity" value=(format!("{:#}", contact.periodicity));
span .hint { code { "[0-9]+ (yr|mo|wk|day|h|m|s)" } "(" a href="https://docs.rs/jiff/latest/jiff/struct.Span.html#parsing-and-printing" { "details" } ")" }
}
label { "birthday" }
label for="birthday" { "birthday" }
div {
input name="birthday" value=(contact.birthday.clone().map_or("".to_string(), |b| b.serialize()));
span .hint { code { "(yyyy|--)mmdd" } " or free text" }
input name="birthday" id="birthday" value=(contact.birthday.clone().map_or("".to_string(), |b| format!("{b}")));
span .hint { code { "(yyyy-)?mm-dd" } " or free text" }
}
label for="manually_freshened_on" { "freshened" }
div x-data=(json!({ "date": mfresh_on_str, "stamp": mfresh_at_str })) x-init="today = () => (new Date().toISOString().split('T')[0])" {