[PATCH 1 of 8] Avoid guessing year 1950 for HQ50 etc

Wald Commits scm-commit at wald.intevation.org
Tue Apr 3 10:44:53 CEST 2018


# HG changeset patch
# User mschaefer
# Date 1522742066 -7200
# Node ID fe81eb39080c90a27db438159a7d30a13697d783
# Parent  66471a3b3db263bd082aecd1b42dcd1b9551e61f
Avoid guessing year 1950 for HQ50 etc.

diff -r 66471a3b3db2 -r fe81eb39080c backend/src/main/java/org/dive4elements/river/backend/utils/DateGuesser.java
--- a/backend/src/main/java/org/dive4elements/river/backend/utils/DateGuesser.java	Tue Apr 03 08:30:23 2018 +0200
+++ b/backend/src/main/java/org/dive4elements/river/backend/utils/DateGuesser.java	Tue Apr 03 09:54:26 2018 +0200
@@ -8,65 +8,64 @@
 
 package org.dive4elements.river.backend.utils;
 
+import java.util.Calendar;
 import java.util.Date;
-import java.util.Calendar;
-
+import java.util.regex.Matcher;
 import java.util.regex.Pattern;
-import java.util.regex.Matcher;
 
 public final class DateGuesser {
     public static final String [] MONTH = {
-        "jan", "feb", "mrz", "apr", "mai", "jun",
-        "jul", "aug", "sep", "okt", "nov", "dez"
+            "jan", "feb", "mrz", "apr", "mai", "jun",
+            "jul", "aug", "sep", "okt", "nov", "dez"
     };
 
-    public static final int guessMonth(String s) {
-        s = s.toLowerCase();
+    public static final int guessMonth(final String s) {
+        final String t = s.toLowerCase();
         for (int i = 0; i < MONTH.length; ++i)
-            if (MONTH[i].equals(s)) {
+            if (MONTH[i].equals(t)) {
                 return i;
             }
         return -1;
     }
 
     public static final Pattern YYYY_MM_DD =
-        Pattern.compile("^(\\d{4})-(\\d{2})-(\\d{2})$");
+            Pattern.compile("^(\\d{4})-(\\d{2})-(\\d{2})$");
 
     public static final Pattern DD_MM_YYYY =
-        Pattern.compile("^(\\d{1,2})\\.(\\d{1,2})\\.(\\d{2,4})$");
+            Pattern.compile("^(\\d{1,2})\\.(\\d{1,2})\\.(\\d{2,4})$");
 
     public static final Pattern MMM_YYYY =
-        Pattern.compile("^(\\d{0,2})\\.?(\\w{3})\\.?(\\d{2,4})$");
+            Pattern.compile("^((\\d{1,2})\\.)?(\\w{3})[\\. -]?((\\d\\d)|(\\d{4}))$");
 
     public static final Pattern GARBAGE_YYYY =
-        Pattern.compile("^\\D*(\\d{2,4})$");
+            Pattern.compile("^[^\\d=]*((\\d\\d)|([12]\\d{3}))$");
 
     public static final Pattern YYYY_MM_DDThh_mm =
-        Pattern.compile("^(\\d{4})-(\\d{2})-(\\d{2})T(\\d{2}):(\\d{2})$");
+            Pattern.compile("^(\\d{4})-(\\d{2})-(\\d{2})T(\\d{2}):(\\d{2})$");
 
     public static final Pattern YYYY_MM_DDThh_mm_ss =
-        Pattern.compile(
-            "^(\\d{4})-(\\d{2})-(\\d{2})T(\\d{2}):(\\d{2}):(\\d{2})$");
+            Pattern.compile(
+                    "^(\\d{4})-(\\d{2})-(\\d{2})T(\\d{2}):(\\d{2}):(\\d{2})$");
 
     public static final Pattern DD_MM_YYYYThh_mm =
-        Pattern.compile(
-            "^(\\d{1,2})\\.(\\d{1,2})\\.(\\d{2,4})T(\\d{1,2}):(\\d{2})$");
+            Pattern.compile(
+                    "^(\\d{1,2})\\.(\\d{1,2})\\.(\\d{2,4})T(\\d{1,2}):(\\d{2})$");
 
     public static final Pattern DD_MM_YYYYThh_mm_ss =
-        Pattern.compile("^(\\d{1,2})\\.(\\d{1,2})\\.(\\d{2,4})"
-            + "T(\\d{1,2}):(\\d{2}):(\\d{2})$");
+            Pattern.compile("^(\\d{1,2})\\.(\\d{1,2})\\.(\\d{2,4})"
+                    + "T(\\d{1,2}):(\\d{2}):(\\d{2})$");
 
     public static final Pattern DDMMYY =
-        Pattern.compile("^(\\d{2})(\\d{2})(\\d{2})$");
+            Pattern.compile("^(\\d{2})(\\d{2})(\\d{2})$");
 
     private DateGuesser() {
     }
 
-    public static final int calendarMonth(String month) {
+    public static final int calendarMonth(final String month) {
         return calendarMonth(Integer.parseInt(month));
     }
 
-    public static final int calendarMonth(int month) {
+    public static final int calendarMonth(final int month) {
         return Math.max(Math.min(month-1, 11), 0);
     }
 
@@ -76,156 +75,157 @@
      * @param s The date to be guessed (e.g. 11.02.2001).
      * @return the parsed Date.
      */
-    public static Date guessDate(String s) {
-        if (s == null || (s = s.trim()).length() == 0) {
+    public static Date guessDate(final String s) {
+        if ((s == null) || s.trim().isEmpty()) {
             throw new IllegalArgumentException();
         }
 
+        final String t = s.trim();
         Matcher m;
 
-        m = YYYY_MM_DD.matcher(s);
+        m = YYYY_MM_DD.matcher(t);
 
         if (m.matches()) {
-            Calendar cal = Calendar.getInstance();
-            String year  = m.group(1);
-            String month = m.group(2);
-            String day   = m.group(3);
+            final Calendar cal = Calendar.getInstance();
+            final String year  = m.group(1);
+            final String month = m.group(2);
+            final String day   = m.group(3);
             cal.clear();
             cal.set(
-                Integer.parseInt(year),
-                calendarMonth(month),
-                Integer.parseInt(day),
-                12, 0, 0);
+                    Integer.parseInt(year),
+                    calendarMonth(month),
+                    Integer.parseInt(day),
+                    12, 0, 0);
             return cal.getTime();
         }
 
-        m = DD_MM_YYYY.matcher(s);
+        m = DD_MM_YYYY.matcher(t);
 
         if (m.matches()) {
-            Calendar cal = Calendar.getInstance();
-            String year  = m.group(3);
-            String month = m.group(2);
-            String day   = m.group(1);
+            final Calendar cal = Calendar.getInstance();
+            final String year  = m.group(3);
+            final String month = m.group(2);
+            final String day   = m.group(1);
             cal.clear();
             cal.set(
-                Integer.parseInt(year) + (year.length() == 2 ? 1900 : 0),
-                calendarMonth(month),
-                Integer.parseInt(day),
-                12, 0, 0);
+                    Integer.parseInt(year) + (year.length() == 2 ? 1900 : 0),
+                    calendarMonth(month),
+                    Integer.parseInt(day),
+                    12, 0, 0);
             return cal.getTime();
         }
 
-        m = MMM_YYYY.matcher(s);
+        m = MMM_YYYY.matcher(t);
 
         if (m.matches()) {
-            int month = guessMonth(m.group(2));
+            final int month = guessMonth(m.group(3));
             if (month >= 0) {
-                Calendar cal = Calendar.getInstance();
-                String year = m.group(3);
-                String day  = m.group(1);
+                final Calendar cal = Calendar.getInstance();
+                final String year = m.group(4);
+                final String day = m.group(1);
                 cal.clear();
                 cal.set(
-                    Integer.parseInt(year) + (year.length() == 2 ? 1900 : 0),
-                    month,
-                    day.length() == 0 ? 15 : Integer.parseInt(day),
-                    12, 0, 0);
+                        Integer.parseInt(year) + (year.length() == 2 ? 1900 : 0),
+                        month,
+                        ((day == null) || day.isEmpty()) ? 15 : Integer.parseInt(day),
+                                12, 0, 0);
                 return cal.getTime();
             }
         }
 
-        m = YYYY_MM_DDThh_mm.matcher(s);
+        m = YYYY_MM_DDThh_mm.matcher(t);
 
         if (m.matches()) {
-            Calendar cal = Calendar.getInstance();
-            String year = m.group(1);
-            String month = m.group(2);
-            String day = m.group(3);
-            String hour = m.group(4);
-            String minute = m.group(5);
+            final Calendar cal = Calendar.getInstance();
+            final String year = m.group(1);
+            final String month = m.group(2);
+            final String day = m.group(3);
+            final String hour = m.group(4);
+            final String minute = m.group(5);
             cal.clear();
             cal.set(
-                Integer.parseInt(year),
-                calendarMonth(month),
-                Integer.parseInt(day),
-                Integer.parseInt(hour),
-                Integer.parseInt(minute),
-                0
-            );
+                    Integer.parseInt(year),
+                    calendarMonth(month),
+                    Integer.parseInt(day),
+                    Integer.parseInt(hour),
+                    Integer.parseInt(minute),
+                    0
+                    );
             return cal.getTime();
         }
 
-        m = YYYY_MM_DDThh_mm_ss.matcher(s);
+        m = YYYY_MM_DDThh_mm_ss.matcher(t);
 
         if (m.matches()) {
-            Calendar cal = Calendar.getInstance();
-            String year = m.group(1);
-            String month = m.group(2);
-            String day = m.group(3);
-            String hour = m.group(4);
-            String minute = m.group(5);
-            String second = m.group(6);
+            final Calendar cal = Calendar.getInstance();
+            final String year = m.group(1);
+            final String month = m.group(2);
+            final String day = m.group(3);
+            final String hour = m.group(4);
+            final String minute = m.group(5);
+            final String second = m.group(6);
             cal.clear();
             cal.set(
-                Integer.parseInt(year),
-                calendarMonth(month),
-                Integer.parseInt(day),
-                Integer.parseInt(hour),
-                Integer.parseInt(minute),
-                Integer.parseInt(second)
-            );
+                    Integer.parseInt(year),
+                    calendarMonth(month),
+                    Integer.parseInt(day),
+                    Integer.parseInt(hour),
+                    Integer.parseInt(minute),
+                    Integer.parseInt(second)
+                    );
             return cal.getTime();
         }
 
-        m = DD_MM_YYYYThh_mm.matcher(s);
+        m = DD_MM_YYYYThh_mm.matcher(t);
 
         if (m.matches()) {
-            Calendar cal = Calendar.getInstance();
-            String year = m.group(3);
-            String month = m.group(2);
-            String day = m.group(1);
-            String hour = m.group(4);
-            String minute = m.group(5);
+            final Calendar cal = Calendar.getInstance();
+            final String year = m.group(3);
+            final String month = m.group(2);
+            final String day = m.group(1);
+            final String hour = m.group(4);
+            final String minute = m.group(5);
             cal.clear();
             cal.set(
-                Integer.parseInt(year) + (year.length() == 2 ? 1900 : 0),
-                calendarMonth(month),
-                Integer.parseInt(day),
-                Integer.parseInt(hour),
-                Integer.parseInt(minute),
-                0
-            );
+                    Integer.parseInt(year) + (year.length() == 2 ? 1900 : 0),
+                    calendarMonth(month),
+                    Integer.parseInt(day),
+                    Integer.parseInt(hour),
+                    Integer.parseInt(minute),
+                    0
+                    );
             return cal.getTime();
         }
 
-        m = DD_MM_YYYYThh_mm_ss.matcher(s);
+        m = DD_MM_YYYYThh_mm_ss.matcher(t);
 
         if (m.matches()) {
-            Calendar cal = Calendar.getInstance();
-            String year = m.group(3);
-            String month = m.group(2);
-            String day = m.group(1);
-            String hour = m.group(4);
-            String minute = m.group(5);
-            String second = m.group(6);
+            final Calendar cal = Calendar.getInstance();
+            final String year = m.group(3);
+            final String month = m.group(2);
+            final String day = m.group(1);
+            final String hour = m.group(4);
+            final String minute = m.group(5);
+            final String second = m.group(6);
             cal.clear();
             cal.set(
-                Integer.parseInt(year) + (year.length() == 2 ? 1900 : 0),
-                calendarMonth(month),
-                Integer.parseInt(day),
-                Integer.parseInt(hour),
-                Integer.parseInt(minute),
-                Integer.parseInt(second)
-            );
+                    Integer.parseInt(year) + (year.length() == 2 ? 1900 : 0),
+                    calendarMonth(month),
+                    Integer.parseInt(day),
+                    Integer.parseInt(hour),
+                    Integer.parseInt(minute),
+                    Integer.parseInt(second)
+                    );
             return cal.getTime();
         }
 
-        m = DDMMYY.matcher(s);
+        m = DDMMYY.matcher(t);
 
         if (m.matches()) {
-            Calendar cal = Calendar.getInstance();
-            String day   = m.group(1);
-            String month = m.group(2);
-            String yearS = m.group(3);
+            final Calendar cal = Calendar.getInstance();
+            final String day   = m.group(1);
+            final String month = m.group(2);
+            final String yearS = m.group(3);
             int year = Integer.parseInt(yearS);
 
             if (year <= cal.get(Calendar.YEAR) % 100) {
@@ -234,33 +234,34 @@
             else {
                 year += 1900;
             }
+            // TODO Warning: two digit year
             cal.clear();
             cal.set(
-                year,
-                Integer.parseInt(month),  // month
-                Integer.parseInt(day), // day
-                12, 0, 0);
+                    year,
+                    Integer.parseInt(month),  // month
+                    Integer.parseInt(day), // day
+                    12, 0, 0);
             return cal.getTime();
         }
 
-        m = GARBAGE_YYYY.matcher(s);
+        m = GARBAGE_YYYY.matcher(t);
 
-        if (m.matches()) {
-            Calendar cal = Calendar.getInstance();
-            String year = m.group(1);
+        if (m.matches() && !t.contains("HQ") && !t.contains("HW")) {
+            final Calendar cal = Calendar.getInstance();
+            final String year = m.group(1);
             cal.clear();
             cal.set(
-                Integer.parseInt(year) + (year.length() == 2 ? 1900 : 0),
-                5,  // month
-                15, // day
-                12, 0, 0);
+                    Integer.parseInt(year) + (year.length() == 2 ? 1900 : 0),
+                    5,  // month
+                    15, // day
+                    12, 0, 0);
             return cal.getTime();
         }
 
         throw new IllegalArgumentException();
     }
 
-    public static void main(String [] args) {
+    public static void main(final String [] args) {
         for (int i = 0; i < args.length; ++i) {
             System.out.println(args[i] + ": " + guessDate(args[i]));
         }


More information about the Dive4Elements-commits mailing list