]> git.aero2k.de Git - tmp/jakarta-migration.git/commitdiff
HM-5 Added support for Mon/Year-Month/Year range
authorcesarvh <cesarv.h@berkeley.edu>
Wed, 17 Oct 2018 23:46:30 +0000 (16:46 -0700)
committercesarvh <cesarv.h@berkeley.edu>
Tue, 19 Mar 2019 19:40:02 +0000 (12:40 -0700)
services/structureddate/structureddate/src/main/antlr4/org/collectionspace/services/structureddate/antlr/StructuredDate.g4
services/structureddate/structureddate/src/main/java/org/collectionspace/services/structureddate/antlr/ANTLRStructuredDateEvaluator.java
services/structureddate/structureddate/src/test/resources/test-dates.yaml

index 7a07930556175a8ab90789dc2ebcaf912993a5c2..d1cb9a49f9905806443632929e7b7c2d67f1ef55 100644 (file)
@@ -35,6 +35,7 @@ hyphenatedRange:       singleInterval ( HYPHEN | DASH ) singleInterval
 |                      quarterInYearRange
 |                      strDayInMonthRange
 |                      numDayInMonthRange
+// |                      numMonthInYearsRange
 ;
 
 singleInterval:        yearSpanningWinter
@@ -103,8 +104,7 @@ strDayInMonthRange:    strMonth numDayOfMonth ( HYPHEN | DASH ) numDayOfMonth CO
 monthInYearRange:      strMonth ( HYPHEN | DASH ) strMonth COMMA? numYear era ;
 nthQuarterInYearRange: nthQuarter ( HYPHEN | DASH ) nthQuarter COMMA? numYear era ;
 strSeasonInYearRange:  strSeason ( HYPHEN | DASH ) strSeason COMMA? numYear era ;
-numDayInMonthRange:    numMonth SLASH numDayOfMonth ( HYPHEN | DASH ) numDayOfMonth SLASH numYear era 
-|                      numMonth SLASH numYear ( HYPHEN | DASH ) numMonth SLASH numYear era ;
+numDayInMonthRange:    numMonth SLASH num ( HYPHEN | DASH ) num SLASH numYear era ;
 numDate:               num SLASH num SLASH num era
 |                      num HYPHEN num HYPHEN num era ;
 monthYear:             strMonth COMMA? numYear era ;
index ad7eb3645dd2197d25d2ac343a0e0ea260c6e056..b0de8cc52ca80f5c285f38a48c1c285789e85a0a 100644 (file)
@@ -369,16 +369,42 @@ public class ANTLRStructuredDateEvaluator extends StructuredDateBaseListener imp
        public void exitNumDayInMonthRange(NumDayInMonthRangeContext ctx) {
                if (ctx.exception != null) return;
 
-               Era era = (ctx.era() == null) ? null : (Era) stack.pop();
-               Integer year = (Integer) stack.pop();
-               Integer dayOfMonthEnd = (Integer) stack.pop();
-               Integer dayOfMonthStart = (Integer) stack.pop();
-               Integer numMonth = (Integer) stack.pop();
-
-               stack.push(new Date(year, numMonth, dayOfMonthStart, era));
-               stack.push(new Date(year, numMonth, dayOfMonthStart, era));
-               stack.push(new Date(year, numMonth, dayOfMonthEnd, era));
-               stack.push(new Date(year, numMonth, dayOfMonthEnd, era));
+               Era era = (Era) stack.pop();
+               Integer num1 = (Integer) stack.pop();
+               Integer num2 = (Integer) stack.pop();
+               Integer num3 = (Integer) stack.pop();
+               Integer num4 = (Integer) stack.pop();
+
+               /* We can distinguish whether it is M/D-D/Y (Case 1) or M/Y-M/Y (Case 2) by checking if
+                       The num1, num4, num2 and num1, num4, num3 are valid dates, since this would equate to
+                       a set of two ranges. For examples: 04/13-19/1995 would be 04/13/1995-04/19/1995. If both these
+                       dates are valid, we know that it shouldn't be interpreted as 04/01/13 - 19/31/1995 since these arent valid dates!
+               */
+
+
+               Integer lateYear = num1;
+               Integer earlyMonth = num4;
+               Integer dayOfMonthEnd = num2;
+               Integer dayOfMonthStart = num3;
+
+               if (DateUtils.isValidDate(num1, num4, num2, era) && DateUtils.isValidDate(num1, num4, num3, era)) {
+                       // No need to alter the arguments, so just push to the stack
+                       stack.push(new Date(lateYear, earlyMonth, dayOfMonthStart, era));
+                       stack.push(new Date(lateYear, earlyMonth, dayOfMonthStart, era));
+                       stack.push(new Date(lateYear, earlyMonth, dayOfMonthEnd, era));
+                       stack.push(new Date(lateYear, earlyMonth, dayOfMonthEnd, era));
+
+               } else {
+                       // Separated these by case, since it makes the code more legible
+                       Integer latestMonth = num2;
+                       Integer earliestYear = num3;
+
+                       stack.push(new Date(earliestYear, earlyMonth, 1, era)); // Earliest Early Date
+                       stack.push(new Date(earliestYear, earlyMonth, DateUtils.getDaysInMonth(earlyMonth, earliestYear, era), era)); // Latest Early Date
+                       stack.push(new Date(lateYear, latestMonth, 1, era)); // Earliest Latest Date
+                       stack.push(new Date(lateYear, latestMonth, DateUtils.getDaysInMonth(latestMonth, lateYear, era), era)); // Latest Late Date
+                       
+               }
        }
 
        @Override
index f3f0b07b1e99a9b5d71964a463092e23168759db..8d66b693881128f59673f047372833d8f047866f 100644 (file)
   "after april 13 1995":
                                          earliestSingleDate: [1995,  4, 13, CE]
                                          latestDate:         [2018,  10, 17, CE]
-
+  
+  "10/2005-12/2006":
+                                         earliestSingleDate: [2005,  10, 1, CE]
+                                         latestDate:         [2006,  12, 31, CE]
+
+  "04/1995-04/2018":
+                                         earliestSingleDate: [1995,  4, 1, CE]
+                                         latestDate:         [2018,  4, 30, CE] 
+  # "unknown":
+  #                                        earliestSingleDate: []
 
 # -------------------------------------------------------------------------------------------------------
 # Invalid dates