diff --git a/src/datetime.js b/src/datetime.js index 01217658..1d9b2d92 100644 --- a/src/datetime.js +++ b/src/datetime.js @@ -612,7 +612,7 @@ const dateTime = (function () { def.integerFormat.mandatoryDigits = def.width.min; } } - if ('YMD'.indexOf(def.component) !== -1) { + if (def.component === 'Y') { // ยง9.8.4.4 def.n = -1; if (def.width && def.width.max !== undefined) { @@ -625,6 +625,11 @@ const dateTime = (function () { } } } + // if the previous part is also an integer with no intervening markup, then its width for parsing must be precisely defined + const previousPart = spec[spec.length - 1]; + if (previousPart && previousPart.integerFormat) { + previousPart.integerFormat.parseWidth = previousPart.integerFormat.mandatoryDigits; + } } if (def.component === 'Z' || def.component === 'z') { def.integerFormat = analyseIntegerPicture(def.presentation1); @@ -990,7 +995,6 @@ const dateTime = (function () { return offsetHours * 60 + offsetMinutes; }; } else if (part.integerFormat) { - part.integerFormat.n = part.n; res = generateRegex(part.integerFormat); } else { // must be a month or day name @@ -1035,16 +1039,6 @@ const dateTime = (function () { } else { // type === 'integer' matcher.type = 'integer'; const isUpper = formatSpec.case === tcase.UPPER; - let occurrences; - if(formatSpec.n && formatSpec.n > 0){ - if(formatSpec.optionalDigits === 0){ - occurrences = `{${formatSpec.n}}`; - } else { - occurrences = `{${formatSpec.n - formatSpec.optionalDigits},${formatSpec.n}}`; - } - } else { - occurrences = '+'; - } switch (formatSpec.primary) { case formats.LETTERS: @@ -1066,7 +1060,12 @@ const dateTime = (function () { }; break; case formats.DECIMAL: - matcher.regex = `[0-9]${occurrences}`; + matcher.regex = '[0-9]'; + if (formatSpec.parseWidth) { + matcher.regex += `{${formatSpec.parseWidth}}`; + } else { + matcher.regex += '+'; + } if (formatSpec.ordinal) { // ordinals matcher.regex += '(?:th|st|nd|rd)'; diff --git a/test/test-suite/groups/function-tomillis/case013.json b/test/test-suite/groups/function-tomillis/case013.json new file mode 100644 index 00000000..0e085d10 --- /dev/null +++ b/test/test-suite/groups/function-tomillis/case013.json @@ -0,0 +1,6 @@ +{ + "expr": "$toMillis('2024-01-01T12:38:49Z') = $toMillis('20240101123849', '[Y0000][M00][D00][H00][m00][s00]')", + "data": null, + "bindings": {}, + "result": true +}