diff --git a/pkg/filter/operator.go b/pkg/filter/operator.go index 5d81fe4c4c..c7356550c3 100644 --- a/pkg/filter/operator.go +++ b/pkg/filter/operator.go @@ -702,6 +702,11 @@ func (o DatetimeLessOp) ToMgo(field string, value interface{}) (map[string]inter return nil, errors.New("field is empty") } + if util.IsDate(value) { + return mapstr.MapStr{ + field: map[string]interface{}{common.BKDBLT: value}, + }, nil + } v, err := util.ConvToTime(value) if err != nil { return nil, fmt.Errorf("convert value to time failed, err: %v", err) @@ -744,6 +749,11 @@ func (o DatetimeLessOrEqualOp) ToMgo(field string, value interface{}) (map[strin return nil, errors.New("field is empty") } + if util.IsDate(value) { + return mapstr.MapStr{ + field: map[string]interface{}{common.BKDBLTE: value}, + }, nil + } v, err := util.ConvToTime(value) if err != nil { return nil, fmt.Errorf("convert value to time failed, err: %v", err) @@ -787,6 +797,11 @@ func (o DatetimeGreaterOp) ToMgo(field string, value interface{}) (map[string]in return nil, errors.New("field is empty") } + if util.IsDate(value) { + return mapstr.MapStr{ + field: map[string]interface{}{common.BKDBGT: value}, + }, nil + } v, err := util.ConvToTime(value) if err != nil { return nil, fmt.Errorf("convert value to time failed, err: %v", err) @@ -829,6 +844,11 @@ func (o DatetimeGreaterOrEqualOp) ToMgo(field string, value interface{}) (map[st return nil, errors.New("field is empty") } + if util.IsDate(value) { + return mapstr.MapStr{ + field: map[string]interface{}{common.BKDBGTE: value}, + }, nil + } v, err := util.ConvToTime(value) if err != nil { return nil, fmt.Errorf("convert value to time failed, err: %v", err) diff --git a/src/common/util/struti.go b/src/common/util/struti.go index d39d7b42cb..d01a4c0c4c 100644 --- a/src/common/util/struti.go +++ b/src/common/util/struti.go @@ -43,7 +43,6 @@ var ( charRegexp = regexp.MustCompile(charPattern) numCharRegexp = regexp.MustCompile(numCharPattern) // mailRegexp = regexp.MustCompile(mailPattern) - dateRegexp = regexp.MustCompile(datePattern) dateTimeRegexp = regexp.MustCompile(dateTimePattern) timeWithLocationRegexp = regexp.MustCompile(timeWithLocationPattern) timeZoneRegexp = regexp.MustCompile(timeZonePattern) @@ -75,7 +74,10 @@ func IsDate(sInput interface{}) bool { if len(val) == 0 { return false } - return dateRegexp.MatchString(val) + if _, err := time.Parse(time.DateOnly, val); err == nil { + return true + } + return false default: return false } diff --git a/src/common/valid/valid.go b/src/common/valid/valid.go index c9ebfab03c..7f0196be88 100644 --- a/src/common/valid/valid.go +++ b/src/common/valid/valid.go @@ -96,6 +96,10 @@ func ValidateDatetimeType(value interface{}) error { return nil } + if util.IsDate(value) { + return nil + } + // string type with time format is supported if _, ok := util.IsTime(value); ok { return nil diff --git a/src/common/valid/valid_test.go b/src/common/valid/valid_test.go index 3d08d930cc..75db8cac79 100644 --- a/src/common/valid/valid_test.go +++ b/src/common/valid/valid_test.go @@ -18,6 +18,7 @@ package valid import ( "testing" + "time" "configcenter/src/common" ) @@ -47,3 +48,28 @@ func TestIsInnerObject(t *testing.T) { }) } } + +func TestValidateDatetimeType(t *testing.T) { + type args struct { + timeStr any + } + now := time.Now() + tests := []struct { + name string + args args + wantErr bool + }{ + {"illegal date", args{"2024-00-00"}, true}, //should err + {"DateOnly", args{now.Format(time.DateOnly)}, false}, + {"DateTime", args{now.Format(time.DateTime)}, false}, + {"RFC3339", args{now.Format(time.RFC3339)}, false}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + err := ValidateDatetimeType(tt.args.timeStr) + if (err != nil) != tt.wantErr { + t.Errorf("ValidateDatetimeType() = %v:%v", tt.args, err) + } + }) + } +}