Skip to content

Commit cea5481

Browse files
committed
Relax date validation for 1 digit seconds
1 parent 6bf0f53 commit cea5481

File tree

2 files changed

+30
-14
lines changed

2 files changed

+30
-14
lines changed

pkg/pdfcpu/types/date.go

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ func parseTimezoneHours(s string, o byte) (int, bool) {
7878
return 0, false
7979
}
8080

81-
// Opininated hack.
81+
// Opinionated hack.
8282
tzh = tzh % 24
8383

8484
if o == 'Z' && tzh != 0 {
@@ -106,15 +106,19 @@ func parseTimezoneMinutes(s string, o byte) (int, bool) {
106106
return tzm, true
107107
}
108108

109-
func validateTimezoneSeparator(c byte) bool {
109+
func timezoneSeparator(c byte) bool {
110110
return c == '+' || c == '-' || c == 'Z'
111111
}
112112

113-
func parseTimezone(s string, relaxed bool) (h, m int, ok bool) {
113+
// func validateTimezoneSeparator(c byte) bool {
114+
// return c == '+' || c == '-' || c == 'Z'
115+
// }
114116

115-
o := s[14]
117+
func parseTimezone(s string, off int, relaxed bool) (h, m int, ok bool) {
116118

117-
if !validateTimezoneSeparator(o) {
119+
o := s[off] // 14
120+
121+
if !timezoneSeparator(o) {
118122
// Ignore timezone on corrupt timezone separator if relaxed.
119123
return 0, 0, relaxed
120124
}
@@ -125,15 +129,17 @@ func parseTimezone(s string, relaxed bool) (h, m int, ok bool) {
125129
// "YYYYMMDDHHmmSSZ'"
126130
// "YYYYMMDDHHmmSSZ'0"
127131

132+
off++
133+
128134
if o == 'Z' {
129-
t := s[15:]
135+
t := s[off:]
130136
if t == "" || relaxed && (t == "'" || t == "'0") {
131137
return 0, 0, true
132138
}
133139
}
134140

135141
// HH'mm
136-
s = s[15:]
142+
s = s[off:]
137143
if s[0] == '-' {
138144
s = s[1:]
139145
}
@@ -291,24 +297,31 @@ func parseMinute(s string) (min int, finished, ok bool) {
291297
return min, false, true
292298
}
293299

294-
func parseSecond(s string) (sec int, finished, ok bool) {
300+
func parseSecond(s string) (sec int, finished bool, off int, ok bool) {
301+
302+
off = 14
303+
295304
second := s[12:14]
305+
if len(second) == 2 && timezoneSeparator(second[1]) {
306+
second = second[:1]
307+
off = 13
308+
}
296309

297310
sec, err := strconv.Atoi(second)
298311
if err != nil {
299-
return 0, false, false
312+
return 0, false, off, false
300313
}
301314

302315
if sec > 59 {
303-
return 0, false, false
316+
return 0, false, off, false
304317
}
305318

306319
// "YYYYMMDDHHmmSS"
307320
if len(s) == 14 {
308-
return sec, true, true
321+
return sec, true, off, true
309322
}
310323

311-
return sec, false, true
324+
return sec, false, off, true
312325
}
313326

314327
func digestPopularOutOfSpecDates(s string) (time.Time, bool) {
@@ -406,7 +419,7 @@ func DateTime(s string, relaxed bool) (time.Time, bool) {
406419
return d, true
407420
}
408421

409-
sec, finished, ok := parseSecond(s)
422+
sec, finished, off, ok := parseSecond(s)
410423
if !ok {
411424
return d, false
412425
}
@@ -417,7 +430,7 @@ func DateTime(s string, relaxed bool) (time.Time, bool) {
417430
}
418431

419432
// Process timezone
420-
tzh, tzm, ok := parseTimezone(s, relaxed)
433+
tzh, tzm, ok := parseTimezone(s, off, relaxed)
421434
if !ok {
422435
return d, false
423436
}

pkg/pdfcpu/types/date_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,9 @@ func TestParseDateTime(t *testing.T) {
104104
s = "D:20210515103719-02'00"
105105
doParseDateTimeOK(s, t)
106106

107+
s = "D:2025022513222+01'00'"
108+
doParseDateTimeOK(s, t)
109+
107110
s = "D:20170430155901+66'A9"
108111
doParseDateTimeFail(s, t)
109112

0 commit comments

Comments
 (0)