diff --git a/src/main/java/liqp/filters/date/BasicDateParser.java b/src/main/java/liqp/filters/date/BasicDateParser.java index 0174e76a..ae41f703 100644 --- a/src/main/java/liqp/filters/date/BasicDateParser.java +++ b/src/main/java/liqp/filters/date/BasicDateParser.java @@ -8,6 +8,7 @@ import java.time.temporal.TemporalField; import java.time.temporal.TemporalQueries; import java.util.HashMap; +import java.util.LinkedHashMap; import java.util.List; import java.util.Locale; import java.util.Map; @@ -18,6 +19,31 @@ public abstract class BasicDateParser { + // Since Liquid supports dates like `March 1st`, this list will + // hold strings that will be removed from the input string. + private static final Map toBeReplaced = new LinkedHashMap() {{ + this.put("11th", "11"); + this.put("12th", "12"); + this.put("13th", "13"); + this.put("1st", "1"); + this.put("2nd", "2"); + this.put("3rd", "3"); + this.put("4th", "4"); + this.put("5th", "5"); + this.put("6th", "6"); + this.put("7th", "7"); + this.put("8th", "8"); + this.put("9th", "9"); + this.put("0th", "0"); + }}; + protected String removeSequentialSuffixes(String input) { + for (Map.Entry entry : toBeReplaced.entrySet()) { + input = input.replaceAll("(?i)"+entry.getKey(), entry.getValue()); + } + return input; + } + + protected final List cachedPatterns = new CopyOnWriteArrayList<>(); protected BasicDateParser() { diff --git a/src/main/java/liqp/filters/date/Parser.java b/src/main/java/liqp/filters/date/Parser.java index 2f01cfbe..8bb44145 100644 --- a/src/main/java/liqp/filters/date/Parser.java +++ b/src/main/java/liqp/filters/date/Parser.java @@ -26,21 +26,6 @@ public class Parser extends BasicDateParser { */ public static List datePatterns = new ArrayList<>(); - // Since Liquid supports dates like `March 1st`, this list will - // hold strings that will be removed from the input string. - private static final Map toBeReplaced = new HashMap() {{ - this.put("1st", "1"); - this.put("2nd", "2"); - this.put("3rd", "3"); - this.put("4th", "4"); - this.put("5th", "5"); - this.put("6th", "6"); - this.put("7th", "7"); - this.put("8th", "8"); - this.put("9th", "9"); - this.put("0th", "0"); - }}; - static { datePatterns.add("EEE MMM d hh:mm:ss yyyy"); @@ -163,9 +148,7 @@ public Parser() { public ZonedDateTime parse(String str, Locale locale, ZoneId defaultZone) { String normalized = str.toLowerCase(); - for(Map.Entry kv : toBeReplaced.entrySet()) { - normalized = normalized.replace(kv.getKey(), kv.getValue()); - } + normalized = removeSequentialSuffixes(normalized); return parseUsingCachedPatterns(normalized, locale, defaultZone); } diff --git a/src/main/java/liqp/filters/date/fuzzy/FuzzyDateParser.java b/src/main/java/liqp/filters/date/fuzzy/FuzzyDateParser.java index de1ca343..1f49759e 100644 --- a/src/main/java/liqp/filters/date/fuzzy/FuzzyDateParser.java +++ b/src/main/java/liqp/filters/date/fuzzy/FuzzyDateParser.java @@ -56,6 +56,7 @@ public ZonedDateTime parse(String normalized, Locale locale, ZoneId defaultZone) if (defaultZone == null) { defaultZone = ZoneId.systemDefault(); } + normalized = removeSequentialSuffixes(normalized); ZonedDateTime date = parseUsingCachedPatterns(normalized, locale, defaultZone); if (date != null) { return date; diff --git a/src/test/java/liqp/filters/DateTest.java b/src/test/java/liqp/filters/DateTest.java index f9d052d3..7708ddc7 100644 --- a/src/test/java/liqp/filters/DateTest.java +++ b/src/test/java/liqp/filters/DateTest.java @@ -252,6 +252,7 @@ public void testSupportedDateStrings() { "MARCH 2", "MARCH 2nd", "MARCH 3RD", + "MARCH 3rD", "MARCH 4th", "MARCH 5th", "MARCH 10th",