-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: improve automatic format detection
Add more patterns to automatic format detector, pick a format that produces maximum date fixes #103
- Loading branch information
Showing
3 changed files
with
182 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
src/test/java/net/atomique/ksar/parser/DateFormatHelperTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package net.atomique.ksar.parser; | ||
|
||
import org.junit.Ignore; | ||
import org.junit.Test; | ||
|
||
import java.time.LocalDate; | ||
import java.time.format.DateTimeFormatter; | ||
import java.time.format.FormatStyle; | ||
import java.util.*; | ||
import java.util.function.Function; | ||
import java.util.function.Predicate; | ||
import java.util.regex.Pattern; | ||
|
||
public class DateFormatHelperTest { | ||
@Test | ||
@Ignore | ||
public void generateTests() throws Exception { | ||
Set<String> allFormats = new HashSet<>(); | ||
LocalDate date = LocalDate.of(2017, 10, 18); | ||
Predicate<String> nonPunctuation = Pattern.compile("[^ ./-\\:0-9]{3,}").asPredicate(); | ||
for (Locale locale : Locale.getAvailableLocales()) { | ||
for (FormatStyle style : EnumSet.of(FormatStyle.SHORT, FormatStyle.MEDIUM)) { | ||
DateTimeFormatter f = DateTimeFormatter.ofLocalizedDate(style).withLocale(locale); | ||
String str = f.format(date); | ||
if (nonPunctuation.test(str)) { | ||
continue; | ||
} | ||
String v = str.replaceAll("2017", "yyyy").replaceAll("17", "yy") | ||
.replaceAll("18", "dd").replaceAll("10", "MM"); | ||
allFormats.add(v); | ||
} | ||
} | ||
List<String> formats = new ArrayList<>(allFormats); | ||
formats.sort(Comparator.<String, String>comparing(v -> v.replaceAll("[^\\w]", "-")) | ||
.thenComparing(Function.identity())); | ||
|
||
for (String format : formats) { | ||
System.out.println('"' + format + "\","); | ||
} | ||
} | ||
|
||
} |
71 changes: 71 additions & 0 deletions
71
src/test/java/net/atomique/ksar/parser/DateFormatTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package net.atomique.ksar.parser; | ||
|
||
import net.atomique.ksar.AllParser; | ||
|
||
import org.junit.Assert; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.junit.runners.Parameterized; | ||
|
||
import java.time.LocalDate; | ||
import java.time.format.DateTimeFormatter; | ||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.Collection; | ||
|
||
@RunWith(Parameterized.class) | ||
public class DateFormatTest { | ||
private final String text; | ||
private final LocalDate date; | ||
private final String expected; | ||
|
||
public DateFormatTest(LocalDate date, String text, String expected) { | ||
this.text = text; | ||
this.date = date; | ||
this.expected = expected; | ||
} | ||
|
||
@Parameterized.Parameters(name = "{1} -> {2}") | ||
public static Iterable<Object[]> params() { | ||
Collection<Object[]> res = new ArrayList<>(); | ||
|
||
// See DateTest.generateFormats | ||
LocalDate date = LocalDate.of(2017, 5, 16); | ||
for (String format : Arrays.asList( | ||
"MM-dd-yy", | ||
"MM/dd/yy", | ||
"dd-MM-yy", | ||
"dd.MM.yy", | ||
"dd/MM/yy", | ||
"dd.MM.yy.", | ||
"dd-MM-yyyy", | ||
"dd.MM.yyyy", | ||
"dd/MM/yyyy", | ||
"yy. MM. dd", | ||
"yy-MM-dd", | ||
"yy.MM.dd", | ||
"yy/MM/dd", | ||
"yy年MM月dd日", | ||
"yy.dd.MM", | ||
"yyyy-MM-dd", | ||
"yyyy.MM.dd", | ||
"yyyy/MM/dd", | ||
"yyyy.MM.dd." | ||
)) { | ||
DateTimeFormatter df = DateTimeFormatter.ofPattern(format); | ||
res.add(new Object[]{date, df.format(date), format}); | ||
} | ||
// See https://github.com/vlsi/ksar/issues/103 | ||
LocalDate aug_04_2017 = LocalDate.of(2017, 8, 4); | ||
res.add(new Object[]{aug_04_2017, "04/08/17", "dd/MM/yy"}); | ||
return res; | ||
} | ||
|
||
@Test | ||
public void run() { | ||
DateTimeFormatter format = AllParser.determineDateFormat(text); | ||
LocalDate date = LocalDate.parse(text, format); | ||
Assert.assertEquals(text, this.date, date); | ||
} | ||
|
||
} |