Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion rust/basecamp-sdk/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,12 @@ impl From<Date> for jiff::civil::Date {
}
}

/// `YYYY-MM-DD`, through the same `%Y-%m-%d` that [`FromStr`] reads, so every date prints as
/// something it reads back. jiff's own `Display` writes a year before 0 with six digits
/// (`-000001`), which `%Y` does not read.
impl fmt::Display for Date {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Display::fmt(&self.0, f)
fmt::Display::fmt(&self.0.strftime("%Y-%m-%d"), f)
}
}

Expand Down Expand Up @@ -477,6 +480,25 @@ mod tests {
assert_eq!("1970-01-01", Date::default().to_string());
}

/// Every date prints as something `Date::parse` and serde read back, the calendar's ends
/// and the years before 0 included.
#[test]
fn every_date_reads_back_what_it_prints() {
for (year, month, day) in [
(-9999, 1, 1),
(-1, 1, 1),
(0, 1, 1),
(99, 12, 31),
(2026, 3, 4),
(9999, 12, 31),
] {
let date = Date::new(year, month, day).unwrap();
assert_eq!(date, Date::parse(&date.to_string()).unwrap(), "{date}");
let json = serde_json::to_string(&date).unwrap();
assert_eq!(date, serde_json::from_str::<Date>(&json).unwrap(), "{json}");
}
}

/// Instants read with any offset and write back in UTC, with the fraction only when
/// there is one, as they did when the type was chrono's.
#[test]
Expand Down
Loading