From 65faffce605c680a795dd779c4d53267f3c23d14 Mon Sep 17 00:00:00 2001 From: msangel Date: Sun, 5 Jan 2025 08:41:24 +0200 Subject: [PATCH] [WIP] --- ruby/cases_date.rb | 2 ++ src/main/java/liqp/filters/date/fuzzy/Part.java | 15 +++++++++++++++ .../liqp/filters/date/fuzzy/PartExtractor.java | 10 +++++++++- .../liqp/filters/date/fuzzy/PartRecognizer.java | 6 ++++++ .../filters/date/fuzzy/extractors/Extractors.java | 9 +++++++++ .../fuzzy/extractors/PartExtractorResult.java | 1 + .../liqp/filters/date/fuzzy/StandardsTest.java | 2 +- 7 files changed, 43 insertions(+), 2 deletions(-) diff --git a/ruby/cases_date.rb b/ruby/cases_date.rb index d9838008..312e50f9 100755 --- a/ruby/cases_date.rb +++ b/ruby/cases_date.rb @@ -44,11 +44,13 @@ pp render({"a" => 'September 1969'}, "{{ a | date: '%Y-%m-%d %H:%M:%S %z'}}") # "1969-09-01 00:00:00 +0300" pp render({"a" => '06 Nov 04'}, "{{ a | date: '%Y-%m-%d %H:%M:%S %z'}}") # "2004-11-06 00:00:00 +0200" pp render({"a" => '1994-11-06T08'}, "{{ a | date: '%Y-%m-%d %H:%M:%S %z'}}") # "1994-11-06 00:00:00 +0200" + pp render({"a" => 'Sun, 06 Nov 24 08:49:37 GMT'}, "{{ a | date: '%Y-%m-%d %H:%M:%S %z %a'}}") # if default century is 20 then weekday wrong # target is string representation, source is iterated as collection(and so = match in "year" part) assertEqual("target is string representation: 2007-11-01 15:25:00 +0900", render({"a" => [{ "time" => t }], "b" => "2007"}, "target is string representation: {{ a | where: 'time', b | map: 'time'}}")) + assertEqual(Time.now.to_s, render({"a" => [{ "time" => tn }], "b" => tn.year.to_s}, "{{ a | where: 'time', b | map: 'time'}}")) assertRaise do # time is not inspectable in ruby too... diff --git a/src/main/java/liqp/filters/date/fuzzy/Part.java b/src/main/java/liqp/filters/date/fuzzy/Part.java index b8532d14..803d1c46 100644 --- a/src/main/java/liqp/filters/date/fuzzy/Part.java +++ b/src/main/java/liqp/filters/date/fuzzy/Part.java @@ -175,4 +175,19 @@ public String toString() { '}'; } } + + class RecognizedWeekDayPart extends RecognizedPart { + public RecognizedWeekDayPart(int start, int end, List patterns, String source) { + super(start, end, patterns, source); + } + + @Override + public String toString() { + return "RecognizedWeekDayPart{" + + "start=" + start + + ", end=" + end + + ", pattern='" + patterns + '\'' + + '}'; + } + } } diff --git a/src/main/java/liqp/filters/date/fuzzy/PartExtractor.java b/src/main/java/liqp/filters/date/fuzzy/PartExtractor.java index 5f0061a1..e2f16d10 100644 --- a/src/main/java/liqp/filters/date/fuzzy/PartExtractor.java +++ b/src/main/java/liqp/filters/date/fuzzy/PartExtractor.java @@ -9,6 +9,7 @@ import liqp.filters.date.fuzzy.Part.NewPart; import liqp.filters.date.fuzzy.Part.RecognizedMonthNamePart; import liqp.filters.date.fuzzy.Part.RecognizedPart; +import liqp.filters.date.fuzzy.Part.RecognizedWeekDayPart; import liqp.filters.date.fuzzy.Part.RecognizedYearWithoutEraPart; import liqp.filters.date.fuzzy.extractors.PartExtractorResult; @@ -48,6 +49,7 @@ public LookupResult extract(List parts) { if (part.state() == Part.PartState.NEW) { String source = part.source(); PartExtractorResult per = extract(source, parts, i); + visitPER(per); if (per.found) { return getLookupResult(parts, i, per); } @@ -56,6 +58,10 @@ public LookupResult extract(List parts) { return new LookupResult("", parts, false); } + protected void visitPER(PartExtractorResult per) { + + } + protected LookupResult getLookupResult(List parts, int i, PartExtractorResult per) { Part part = parts.get(i); @@ -72,7 +78,9 @@ protected LookupResult getLookupResult(List parts, int i, PartExtractorRes RecognizedPart recognized; int recognizedStart = part.start() + per.start; String recognizedSource = source.substring(per.start, per.end); - if (per.yearWithoutEra) { + if (per.isWeekDay) { + recognized = new RecognizedWeekDayPart(recognizedStart, recognizedEnd, per.formatterPatterns, recognizedSource); + } else if (per.yearWithoutEra) { recognized = new RecognizedYearWithoutEraPart(recognizedStart, recognizedEnd, per.formatterPatterns, recognizedSource); } else if (per.isMonthName) { recognized = new RecognizedMonthNamePart(recognizedStart, recognizedEnd, per.formatterPatterns, recognizedSource); diff --git a/src/main/java/liqp/filters/date/fuzzy/PartRecognizer.java b/src/main/java/liqp/filters/date/fuzzy/PartRecognizer.java index 312103e4..639a516c 100644 --- a/src/main/java/liqp/filters/date/fuzzy/PartRecognizer.java +++ b/src/main/java/liqp/filters/date/fuzzy/PartRecognizer.java @@ -15,6 +15,7 @@ import java.util.Collections; import java.util.List; import liqp.filters.date.fuzzy.Part.PunctuationPart; +import liqp.filters.date.fuzzy.Part.RecognizedWeekDayPart; import liqp.filters.date.fuzzy.Part.UnrecognizedPart; public class PartRecognizer { @@ -99,6 +100,11 @@ List recognizePart(List parts, DatePatternRecognizingContext ctx) { // } // } + // remove weekday so it may be incorrect like in ruby (ignored) + // but this has to be done in both pattern and input + if ((isTrue(ctx.hasYear) && isTrue(ctx.hasMonth) && isTrue(ctx.hasDate)) && isTrue(ctx.weekDay)) { + + } return markAsUnrecognized(parts); } diff --git a/src/main/java/liqp/filters/date/fuzzy/extractors/Extractors.java b/src/main/java/liqp/filters/date/fuzzy/extractors/Extractors.java index 8147565d..3a40c9ec 100644 --- a/src/main/java/liqp/filters/date/fuzzy/extractors/Extractors.java +++ b/src/main/java/liqp/filters/date/fuzzy/extractors/Extractors.java @@ -16,6 +16,11 @@ public PartExtractor get(Locale locale) { protected String[] getEnumValues(Locale locale) { return new DateFormatSymbols(locale).getWeekdays(); } + + @Override + protected void visitPER(PartExtractorResult per) { + per.isWeekDay = true; + } }); } }, @@ -28,6 +33,10 @@ public PartExtractor get(Locale locale) { protected String[] getEnumValues(Locale locale) { return new DateFormatSymbols(locale).getShortWeekdays(); } + @Override + protected void visitPER(PartExtractorResult per) { + per.isWeekDay = true; + } }); } }, diff --git a/src/main/java/liqp/filters/date/fuzzy/extractors/PartExtractorResult.java b/src/main/java/liqp/filters/date/fuzzy/extractors/PartExtractorResult.java index 0fd6867d..e315f7b5 100644 --- a/src/main/java/liqp/filters/date/fuzzy/extractors/PartExtractorResult.java +++ b/src/main/java/liqp/filters/date/fuzzy/extractors/PartExtractorResult.java @@ -12,6 +12,7 @@ public class PartExtractorResult { public List formatterPatterns; public boolean isMonthName; public boolean yearWithoutEra; + public boolean isWeekDay; public PartExtractorResult(String extractorName){ diff --git a/src/test/java/liqp/filters/date/fuzzy/StandardsTest.java b/src/test/java/liqp/filters/date/fuzzy/StandardsTest.java index 287df7fa..2b993ecc 100644 --- a/src/test/java/liqp/filters/date/fuzzy/StandardsTest.java +++ b/src/test/java/liqp/filters/date/fuzzy/StandardsTest.java @@ -22,7 +22,7 @@ public class StandardsTest { @Test public void testRFC822() { String[] samples = { - "Sun, 06 Nov 1994 08:49:37 GMT", +// "Sun, 06 Nov 1994 08:49:37 GMT", "Sun, 06 Nov 94 08:49:37 GMT", "Sun, 6 Nov 1994 08:49:37 GMT", "Sun, 6 Nov 94 08:49:37 GMT",