]> git.ipfire.org Git - thirdparty/paperless-ngx.git/commitdiff
Tweak: improve 2-digit year parsing (#12044)
authorshamoon <4887959+shamoon@users.noreply.github.com>
Mon, 9 Feb 2026 07:00:00 +0000 (23:00 -0800)
committerGitHub <noreply@github.com>
Mon, 9 Feb 2026 07:00:00 +0000 (23:00 -0800)
src-ui/src/app/utils/ngb-date-parser-formatter.spec.ts
src-ui/src/app/utils/ngb-date-parser-formatter.ts

index 3bf12e1f636b7b5237e2c48e198141e82b69ac6e..1ede0215ff600074dc041c801254809570406a23 100644 (file)
@@ -70,4 +70,26 @@ describe('LocalizedDateParserFormatter', () => {
     dateStr = dateParserFormatter.format(dateStruct)
     expect(dateStr).toEqual('04.05.2023')
   })
+
+  it('should handle years when current year % 100 < 50', () => {
+    jest.useFakeTimers()
+    jest.setSystemTime(new Date(2026, 5, 15))
+    let val = dateParserFormatter.parse('5/4/26')
+    expect(val).toEqual({ day: 4, month: 5, year: 2026 })
+
+    val = dateParserFormatter.parse('5/4/75')
+    expect(val).toEqual({ day: 4, month: 5, year: 2075 })
+
+    val = dateParserFormatter.parse('5/4/99')
+    expect(val).toEqual({ day: 4, month: 5, year: 1999 })
+    jest.useRealTimers()
+  })
+
+  it('should handle years when current year % 100 >= 50', () => {
+    jest.useFakeTimers()
+    jest.setSystemTime(new Date(2076, 5, 15))
+    const val = dateParserFormatter.parse('5/4/00')
+    expect(val).toEqual({ day: 4, month: 5, year: 2100 })
+    jest.useRealTimers()
+  })
 })
index 99be6d88ad6c0dc729c849d9763a9d416622d6d4..48806a80a56d77a00754cae66ece0e9a0c6fef2c 100644 (file)
@@ -106,15 +106,25 @@ export class LocalizedDateParserFormatter extends NgbDateParserFormatter {
     value = this.preformatDateInput(value)
     let match = this.getDateParseRegex().exec(value)
     if (match) {
+      const currentYear = new Date().getFullYear()
+      const currentCentury = currentYear - (currentYear % 100)
+
+      let year = +match.groups.year
+      if (year < 100) {
+        let fourDigitYear = currentCentury + year
+        // Mimic python-dateutil: keep result within -50/+49 years of current year
+        if (fourDigitYear > currentYear + 49) {
+          fourDigitYear -= 100
+        } else if (fourDigitYear <= currentYear - 50) {
+          fourDigitYear += 100
+        }
+        year = fourDigitYear
+      }
+
       let dateStruct = {
         day: +match.groups.day,
         month: +match.groups.month,
-        year: +match.groups.year,
-      }
-      if (dateStruct.year <= new Date().getFullYear() - 2000) {
-        dateStruct.year += 2000
-      } else if (dateStruct.year < 100) {
-        dateStruct.year += 1900
+        year,
       }
       return dateStruct
     } else {