MCPcopy Create free account
hub / github.com/LibPDF-js/core / parsePdfDate

Function parsePdfDate

src/helpers/format.ts:45–93  ·  view source on GitHub ↗
(str: string)

Source from the content-addressed store, hash-verified

43 * @returns Parsed Date, or undefined if invalid
44 */
45export function parsePdfDate(str: string): Date | undefined {
46 // Remove D: prefix if present
47 let s = str;
48
49 if (s.startsWith("D:")) {
50 s = s.slice(2);
51 }
52
53 // Minimum: YYYY (4 chars) and must start with digits
54 if (s.length < 4 || !/^\d{4}/.test(s)) {
55 return undefined;
56 }
57
58 try {
59 const year = parseInt(s.slice(0, 4), 10);
60 const month = s.length >= 6 ? parseInt(s.slice(4, 6), 10) - 1 : 0;
61 const day = s.length >= 8 ? parseInt(s.slice(6, 8), 10) : 1;
62 const hour = s.length >= 10 ? parseInt(s.slice(8, 10), 10) : 0;
63 const minute = s.length >= 12 ? parseInt(s.slice(10, 12), 10) : 0;
64 const second = s.length >= 14 ? parseInt(s.slice(12, 14), 10) : 0;
65
66 // Validate parsed values
67 if (Number.isNaN(year) || Number.isNaN(month) || Number.isNaN(day)) {
68 return undefined;
69 }
70
71 // Parse timezone if present
72 let tzOffset = 0;
73
74 if (s.length >= 15) {
75 const tzChar = s.charAt(14);
76
77 if (tzChar === "Z") {
78 tzOffset = 0;
79 } else if (tzChar === "+" || tzChar === "-") {
80 const tzHour = parseInt(s.slice(15, 17), 10) || 0;
81 const tzMin = parseInt(s.slice(18, 20), 10) || 0;
82 tzOffset = (tzHour * 60 + tzMin) * (tzChar === "-" ? 1 : -1);
83 }
84 }
85
86 const date = new Date(Date.UTC(year, month, day, hour, minute, second));
87 date.setUTCMinutes(date.getUTCMinutes() + tzOffset);
88
89 return date;
90 } catch {
91 return undefined;
92 }
93}
94
95// ─────────────────────────────────────────────────────────────────────────────
96// Number Formatting

Callers 5

fromCatalogMethod · 0.90
file-spec.test.tsFile · 0.90
parseFileSpecFunction · 0.90
getCreationDateMethod · 0.90
getModificationDateMethod · 0.90

Calls 1

sliceMethod · 0.80

Tested by

no test coverage detected