Skip to content

Commit

Permalink
[WIP]
Browse files Browse the repository at this point in the history
  • Loading branch information
vkhrystiuk-ks committed Dec 22, 2024
1 parent 7255d11 commit 37670fc
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 18 deletions.
26 changes: 26 additions & 0 deletions src/main/java/liqp/filters/date/BasicDateParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<String, String> toBeReplaced = new LinkedHashMap<String, String>() {{
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<String, String> entry : toBeReplaced.entrySet()) {
input = input.replaceAll("(?i)"+entry.getKey(), entry.getValue());
}
return input;
}


protected final List<String> cachedPatterns = new CopyOnWriteArrayList<>();

protected BasicDateParser() {
Expand Down
19 changes: 1 addition & 18 deletions src/main/java/liqp/filters/date/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,6 @@ public class Parser extends BasicDateParser {
*/
public static List<String> 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<String, String> toBeReplaced = new HashMap<String, String>() {{
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");
Expand Down Expand Up @@ -163,9 +148,7 @@ public Parser() {

public ZonedDateTime parse(String str, Locale locale, ZoneId defaultZone) {
String normalized = str.toLowerCase();
for(Map.Entry<String, String> kv : toBeReplaced.entrySet()) {
normalized = normalized.replace(kv.getKey(), kv.getValue());
}
normalized = removeSequentialSuffixes(normalized);
return parseUsingCachedPatterns(normalized, locale, defaultZone);
}

Expand Down
1 change: 1 addition & 0 deletions src/main/java/liqp/filters/date/fuzzy/FuzzyDateParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions src/test/java/liqp/filters/DateTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,7 @@ public void testSupportedDateStrings() {
"MARCH 2",
"MARCH 2nd",
"MARCH 3RD",
"MARCH 3rD",
"MARCH 4th",
"MARCH 5th",
"MARCH 10th",
Expand Down

0 comments on commit 37670fc

Please sign in to comment.