Browse Source

MB-33600: Checking if datetimes are RFC3339 compatible

Check if datetimes provided within the datetime range query
are RFC3339 compatible only of the QueryDateTimeFormat is
RFC3339.
abhinavdangeti 2 months ago
parent
commit
73e96b4967
1 changed files with 15 additions and 6 deletions
  1. 15 6
      search/query/date_range.go

+ 15 - 6
search/query/date_range.go

@@ -41,12 +41,12 @@ type BleveQueryTime struct {
 	time.Time
 }
 
-var minValidTime time.Time
-var maxValidTime time.Time
+var MinRFC3339CompatibleTime time.Time
+var MaxRFC3339CompatibleTime time.Time
 
 func init() {
-	minValidTime, _ = time.Parse(time.RFC3339, "1677-12-01T00:00:00Z")
-	maxValidTime, _ = time.Parse(time.RFC3339, "2262-04-11T11:59:59Z")
+	MinRFC3339CompatibleTime, _ = time.Parse(time.RFC3339, "1677-12-01T00:00:00Z")
+	MaxRFC3339CompatibleTime, _ = time.Parse(time.RFC3339, "2262-04-11T11:59:59Z")
 }
 
 func queryTimeFromString(t string) (time.Time, error) {
@@ -151,7 +151,7 @@ func (q *DateRangeQuery) parseEndpoints() (*float64, *float64, error) {
 	min := math.Inf(-1)
 	max := math.Inf(1)
 	if !q.Start.IsZero() {
-		if q.Start.Before(minValidTime) || q.Start.After(maxValidTime) {
+		if !isDatetimeCompatible(q.Start) {
 			// overflow
 			return nil, nil, fmt.Errorf("invalid/unsupported date range, start: %v", q.Start)
 		}
@@ -159,7 +159,7 @@ func (q *DateRangeQuery) parseEndpoints() (*float64, *float64, error) {
 		min = numeric.Int64ToFloat64(startInt64)
 	}
 	if !q.End.IsZero() {
-		if q.End.Before(minValidTime) || q.End.After(maxValidTime) {
+		if !isDatetimeCompatible(q.End) {
 			// overflow
 			return nil, nil, fmt.Errorf("invalid/unsupported date range, end: %v", q.End)
 		}
@@ -180,3 +180,12 @@ func (q *DateRangeQuery) Validate() error {
 	}
 	return nil
 }
+
+func isDatetimeCompatible(t BleveQueryTime) bool {
+	if QueryDateTimeFormat == time.RFC3339 &&
+		(t.Before(MinRFC3339CompatibleTime) || t.After(MaxRFC3339CompatibleTime)) {
+		return false
+	}
+
+	return true
+}