Skip to content

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

Parse a bare trailing "a" as AM in AcroForm date/time parsing

NetterSilver4125bddbae5b
diff --git a/crates/forms/src/af.rs b/crates/forms/src/af.rs
index c5b81719a..d95a41ba3 100644
--- a/crates/forms/src/af.rs
+++ b/crates/forms/src/af.rs
@@ -639,7 +639,8 @@ pub fn parse_date(text: &str, fmt: &str) -> Option<DateTime> {
// A bare trailing "p" ("5:30p") only counts when it follows a digit, not a word like "Sep".
let bare_p = lower.strip_suffix('p').is_some_and(|rest| rest.trim_end().ends_with(|c: char| c.is_ascii_digit()));
let pm = lower.contains("pm") || bare_p;
- let am = lower.contains("am");
+ let bare_a = lower.strip_suffix('a').is_some_and(|rest| rest.trim_end().ends_with(|c: char| c.is_ascii_digit()));
+ let am = lower.contains("am") || bare_a;
// Tokens: runs of digits (kept as text, so a run can be split between fields typed without
// separators) and month names.
let mut nums: VecDeque<String> = VecDeque::new();
diff --git a/crates/forms/tests/af_date_parse.rs b/crates/forms/tests/af_date_parse.rs
new file mode 100644
index 000000000..802c40dc6
--- /dev/null
+++ b/crates/forms/tests/af_date_parse.rs
@@ -0,0 +1,10 @@
+use pdfcraft_forms::af::parse_date;
+
+#[test]
+fn bare_a_suffix_is_am() {
+ assert_eq!(parse_date("12:05 a", "h:MM t").map(|d| d.hh), Some(0));
+ assert_eq!(parse_date("12:30a", "h:MM t").map(|d| d.hh), Some(0));
+ assert_eq!(parse_date("5:30 a", "h:MM t").map(|d| d.hh), Some(5));
+ assert_eq!(parse_date("5:30p", "h:MM t").map(|d| d.hh), Some(17));
+ assert_eq!(parse_date("12:05 p", "h:MM t").map(|d| d.hh), Some(12));
+}