Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion pkg/filter/operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -706,7 +706,12 @@ func (o DatetimeLessOp) ToMgo(field string, value interface{}) (map[string]inter
if err != nil {
return nil, fmt.Errorf("convert value to time failed, err: %v", err)
}

timeType, _ := util.IsTime(value)
if timeType == util.DateOnlyType {
return mapstr.MapStr{
field: map[string]interface{}{common.BKDBLTE: value},
}, nil
}
return mapstr.MapStr{
field: map[string]interface{}{common.BKDBLT: v},
}, nil
Expand Down Expand Up @@ -748,6 +753,12 @@ func (o DatetimeLessOrEqualOp) ToMgo(field string, value interface{}) (map[strin
if err != nil {
return nil, fmt.Errorf("convert value to time failed, err: %v", err)
}
timeType, _ := util.IsTime(value)
if timeType == util.DateOnlyType {
return mapstr.MapStr{
field: map[string]interface{}{common.BKDBLTE: value},
}, nil
}

return mapstr.MapStr{
field: map[string]interface{}{common.BKDBLTE: v},
Expand Down Expand Up @@ -791,6 +802,12 @@ func (o DatetimeGreaterOp) ToMgo(field string, value interface{}) (map[string]in
if err != nil {
return nil, fmt.Errorf("convert value to time failed, err: %v", err)
}
timeType, _ := util.IsTime(value)
if timeType == util.DateOnlyType {
return mapstr.MapStr{
field: map[string]interface{}{common.BKDBGT: value},
}, nil
}

return mapstr.MapStr{
field: map[string]interface{}{common.BKDBGT: v},
Expand Down Expand Up @@ -833,6 +850,12 @@ func (o DatetimeGreaterOrEqualOp) ToMgo(field string, value interface{}) (map[st
if err != nil {
return nil, fmt.Errorf("convert value to time failed, err: %v", err)
}
timeType, _ := util.IsTime(value)
if timeType == util.DateOnlyType {
return mapstr.MapStr{
field: map[string]interface{}{common.BKDBGTE: value},
}, nil
}

return mapstr.MapStr{
field: map[string]interface{}{common.BKDBGTE: v},
Expand Down
7 changes: 7 additions & 0 deletions src/common/util/struti.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ const (
// timeWithLocationType the date time type compatible for values from db which is marshaled with time zone
timeWithLocationType DateTimeFieldType = "time_with_location"
invalidDateTimeType DateTimeFieldType = "invalid"

DateOnlyType DateTimeFieldType = "date_only"
)

// IsTime 是否是时间类型
Expand All @@ -102,6 +104,9 @@ func IsTime(sInput interface{}) (DateTimeFieldType, bool) {
if timeWithLocationRegexp.MatchString(val) {
return timeWithLocationType, true
}
if dateRegexp.MatchString(val) {
return DateOnlyType, true
}
return invalidDateTimeType, false
default:
return invalidDateTimeType, false
Expand Down Expand Up @@ -134,6 +139,8 @@ func Str2Time(timeStr string, timeType DateTimeFieldType) time.Time {
layout = "2006-01-02 15:04:05"
case timeWithLocationType:
layout = "2006-01-02T15:04:05Z07:00"
case DateOnlyType:
layout = time.DateOnly
default:
return time.Time{}
}
Expand Down
4 changes: 4 additions & 0 deletions src/common/valid/valid.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
24 changes: 24 additions & 0 deletions src/common/valid/valid_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package valid

import (
"testing"
"time"

"configcenter/src/common"
)
Expand Down Expand Up @@ -47,3 +48,26 @@ func TestIsInnerObject(t *testing.T) {
})
}
}

func TestValidateDatetimeType(t *testing.T) {
type args struct {
objID any
}
now := time.Now()
tests := []struct {
name string
args args
}{
{"", args{"2024-00-00"}}, //TODO 不符合日期格式,但能通过正则
{"", args{now.Format(time.DateOnly)}},
{"", args{now.Format(time.DateTime)}},
{"", args{now.Format(time.RFC3339)}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := ValidateDatetimeType(tt.args.objID); err != nil {
t.Errorf("ValidateDatetimeType() = %v:%v", tt.args, err)
}
})
}
}