Skip to content

PdfCraft: An open-source PDF reader and editor written by scratch in pure Rust

Merge remote-tracking branch 'upstream/main' into fix/util-scand

Eric Kaden628c82757258
diff --cc crates/js/src/formcalc.rs
index 43f32a11b,1ab7ca24e..a1b807a26
--- a/crates/js/src/formcalc.rs
+++ b/crates/js/src/formcalc.rs
@@@ -1924,11 -1944,16 +1944,16 @@@ fn parse_date(text: &str, picture: &str
}
/// Number of days in month `m` (1-12) of year `y`, leap years included.
-fn days_in(y: i64, m: u32) -> u32 {
+pub(crate) fn days_in(y: i64, m: u32) -> u32 {
- match m {
- 2 if (y % 4 == 0 && y % 100 != 0) || y % 400 == 0 => 29,
- 2 => 28,
- 4 | 6 | 9 | 11 => 30,
+ // The length of the month from chrono (the first of the next month, less the first of this
+ // one). An invalid month keeps the old 31-day default.
+ // Month lengths repeat every 400 years, so the year within its cycle is enough (and always
+ // in chrono's range, whatever `y` is).
+ let y = y.rem_euclid(400);
+ let first = |y: i64, m: u32| i32::try_from(y).ok().and_then(|y| chrono::NaiveDate::from_ymd_opt(y, m, 1));
+ let next = if m == 12 { first(y + 1, 1) } else { first(y, m + 1) };
+ match (first(y, m), next) {
+ (Some(a), Some(b)) => (b - a).num_days() as u32,
_ => 31,
}
}