-
Notifications
You must be signed in to change notification settings - Fork 60
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: improve automatic format detection #106
Open
vlsi
wants to merge
1
commit into
master
Choose a base branch
from
date_format_detect
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,7 @@ | |
package net.atomique.ksar; | ||
|
||
import net.atomique.ksar.xml.OSConfig; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
|
@@ -14,26 +15,49 @@ | |
import java.time.LocalTime; | ||
import java.time.format.DateTimeFormatter; | ||
import java.time.format.DateTimeParseException; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.List; | ||
import java.util.Locale; | ||
import java.util.TreeSet; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.Stream; | ||
|
||
public abstract class AllParser { | ||
|
||
private static final Logger log = LoggerFactory.getLogger(AllParser.class); | ||
private static final Map<String, String> DATE_FORMAT_REGEXPS = new HashMap<String, String>() { | ||
{ | ||
put("^\\d{8}$", "yyyyMMdd"); | ||
put("^\\d{1,2}-\\d{1,2}-\\d{4}$", "dd-MM-yyyy"); | ||
put("^\\d{4}-\\d{1,2}-\\d{1,2}$", "yyyy-MM-dd"); | ||
put("^\\d{1,2}/\\d{1,2}/\\d{4}$", "MM/dd/yyyy"); | ||
put("^\\d{4}/\\d{1,2}/\\d{1,2}$", "yyyy/MM/dd"); | ||
put("^\\d{1,2}\\s[a-z]{3}\\s\\d{4}$", "dd MMM yyyy"); | ||
put("^\\d{1,2}\\s[a-z]{4,}\\s\\d{4}$", "dd MMMM yyyy"); | ||
put("^\\d{1,2}-\\d{1,2}-\\d{2}$", "dd-MM-yy"); | ||
put("^\\d{1,2}/\\d{1,2}/\\d{2}$", "MM/dd/yy"); | ||
} | ||
}; | ||
|
||
private static final List<DateTimeFormatter> DATE_FORMATS = Stream.of( | ||
"MM dd, yy", | ||
"MM-dd-yy", | ||
"MM/dd/yy", | ||
"MM-dd-yyyy", | ||
"MM/dd/yyyy", | ||
"dd-MM-yy", | ||
"dd.MM.yy", | ||
"dd/MM/yy", | ||
"dd.MM.yy.", | ||
"dd-MM-yyyy", | ||
"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", | ||
"yyyy.MM.dd.", | ||
"yyyy年MM月dd日", | ||
"yyyy.dd.MM", | ||
"yyyyMMdd", | ||
"dd MMM yyyy", | ||
"dd MMMM yyyy", | ||
"MMM dd yyyy", | ||
"MMMM dd yyyy" | ||
).map(p -> DateTimeFormatter.ofPattern(p, Locale.US)).collect(Collectors.toList()); | ||
|
||
public AllParser() { | ||
|
||
|
@@ -80,14 +104,7 @@ public boolean setDate(String s) { | |
} | ||
|
||
try { | ||
DateTimeFormatter formatter; | ||
if ("Automatic Detection".equals(dateFormat)) { | ||
formatter = DateTimeFormatter.ofPattern(determineDateFormat(s)); | ||
|
||
} else { | ||
formatter = DateTimeFormatter.ofPattern(dateFormat); | ||
} | ||
|
||
DateTimeFormatter formatter = getDateFormatter(s); | ||
currentDate = LocalDate.parse(s, formatter); | ||
|
||
parsedate = currentDate; | ||
|
@@ -109,6 +126,20 @@ public boolean setDate(String s) { | |
return true; | ||
} | ||
|
||
private DateTimeFormatter getDateFormatter(String s) { | ||
if (dateFormatter != null) { | ||
return dateFormatter; | ||
} | ||
DateTimeFormatter format = null; | ||
if ("Automatic Detection".equals(dateFormat)) { | ||
format = determineDateFormat(s); | ||
} else { | ||
format = DateTimeFormatter.ofPattern(dateFormat); | ||
} | ||
dateFormatter = format; | ||
return dateFormatter; | ||
} | ||
|
||
public String getDate() { | ||
if (sarStartDate.equals(sarEndDate)) { | ||
return sarStartDate; | ||
|
@@ -125,13 +156,21 @@ public String getCurrentStat() { | |
return currentStat; | ||
} | ||
|
||
public static String determineDateFormat(String dateString) { | ||
for (String regexp : DATE_FORMAT_REGEXPS.keySet()) { | ||
if (dateString.toLowerCase().matches(regexp)) { | ||
return DATE_FORMAT_REGEXPS.get(regexp); | ||
public static DateTimeFormatter determineDateFormat(String dateString) { | ||
DateTimeFormatter best = null; | ||
LocalDate bestDate = null; | ||
for (DateTimeFormatter format : DATE_FORMATS) { | ||
try { | ||
LocalDate nextDate = LocalDate.parse(dateString, format); | ||
if (bestDate == null || nextDate.compareTo(bestDate) >= 0) { | ||
bestDate = nextDate; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. also here .. log.trace "found better date" |
||
best = format; | ||
} | ||
} catch (DateTimeParseException e) { | ||
/* ignore */ | ||
} | ||
} | ||
return null; // Unknown format. | ||
return best; | ||
} | ||
|
||
protected String sarStartDate = null; | ||
|
@@ -159,4 +198,6 @@ public static String determineDateFormat(String dateString) { | |
protected String dateFormat = "MM/dd/yy"; | ||
protected String timeFormat = "HH:mm:ss"; | ||
protected int timeColumn = 1; | ||
|
||
private DateTimeFormatter dateFormatter; | ||
} |
53 changes: 53 additions & 0 deletions
53
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,53 @@ | ||
/* | ||
* Copyright 2017 The kSAR Project. All rights reserved. | ||
* See the LICENSE file in the project root for more information. | ||
*/ | ||
|
||
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.ArrayList; | ||
import java.util.Comparator; | ||
import java.util.EnumSet; | ||
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.Locale; | ||
import java.util.Set; | ||
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 + "\","); | ||
} | ||
} | ||
|
||
} |
76 changes: 76 additions & 0 deletions
76
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,76 @@ | ||
/* | ||
* Copyright 2017 The kSAR Project. All rights reserved. | ||
* See the LICENSE file in the project root for more information. | ||
*/ | ||
|
||
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); | ||
} | ||
|
||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
logging would be great .. log.Trace "found valid .."