Skip to content

Commit

Permalink
[WIP]
Browse files Browse the repository at this point in the history
  • Loading branch information
msangel committed Jan 5, 2025
1 parent 0931d5f commit 65faffc
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 2 deletions.
2 changes: 2 additions & 0 deletions ruby/cases_date.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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...
Expand Down
15 changes: 15 additions & 0 deletions src/main/java/liqp/filters/date/fuzzy/Part.java
Original file line number Diff line number Diff line change
Expand Up @@ -175,4 +175,19 @@ public String toString() {
'}';
}
}

class RecognizedWeekDayPart extends RecognizedPart {
public RecognizedWeekDayPart(int start, int end, List<String> patterns, String source) {
super(start, end, patterns, source);
}

@Override
public String toString() {
return "RecognizedWeekDayPart{" +
"start=" + start +
", end=" + end +
", pattern='" + patterns + '\'' +
'}';
}
}
}
10 changes: 9 additions & 1 deletion src/main/java/liqp/filters/date/fuzzy/PartExtractor.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -48,6 +49,7 @@ public LookupResult extract(List<Part> 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);
}
Expand All @@ -56,6 +58,10 @@ public LookupResult extract(List<Part> parts) {
return new LookupResult("<none>", parts, false);
}

protected void visitPER(PartExtractorResult per) {

}

protected LookupResult getLookupResult(List<Part> parts, int i, PartExtractorResult per) {

Part part = parts.get(i);
Expand All @@ -72,7 +78,9 @@ protected LookupResult getLookupResult(List<Part> 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);
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/liqp/filters/date/fuzzy/PartRecognizer.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -99,6 +100,11 @@ List<Part> recognizePart(List<Part> 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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
});
}
},
Expand All @@ -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;
}
});
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public class PartExtractorResult {
public List<String> formatterPatterns;
public boolean isMonthName;
public boolean yearWithoutEra;
public boolean isWeekDay;


public PartExtractorResult(String extractorName){
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/liqp/filters/date/fuzzy/StandardsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 65faffc

Please sign in to comment.