修复(#128): 兼容促销规则毫秒时间戳
Co-authored-by: multica-agent <github@multica.ai>
This commit is contained in:
@@ -17,11 +17,24 @@ const (
|
||||
subscribeCachePref = "promo:subscribe:"
|
||||
)
|
||||
|
||||
var (
|
||||
minPromoRuleTime = time.Date(1970, 1, 1, 0, 0, 0, 0, time.UTC)
|
||||
maxPromoRuleTime = time.Date(9999, 12, 31, 23, 59, 59, 0, time.UTC)
|
||||
)
|
||||
|
||||
func validateRuleInput(ruleType string, params map[string]interface{}, priority int64, startTime, endTime *int64) error {
|
||||
if priority < 0 {
|
||||
return errors.Wrapf(xerr.NewErrCode(xerr.InvalidParams), "priority must be greater than or equal to 0")
|
||||
}
|
||||
if startTime != nil && endTime != nil && *startTime >= *endTime {
|
||||
startAt, err := normalizeRuleTimestamp(startTime)
|
||||
if err != nil {
|
||||
return errors.Wrapf(xerr.NewErrCode(xerr.InvalidParams), "invalid start_time")
|
||||
}
|
||||
endAt, err := normalizeRuleTimestamp(endTime)
|
||||
if err != nil {
|
||||
return errors.Wrapf(xerr.NewErrCode(xerr.InvalidParams), "invalid end_time")
|
||||
}
|
||||
if startAt != nil && endAt != nil && !startAt.Before(*endAt) {
|
||||
return errors.Wrapf(xerr.NewErrCode(xerr.InvalidParams), "start_time must be less than end_time")
|
||||
}
|
||||
switch ruleType {
|
||||
@@ -93,12 +106,29 @@ func parseParams(data string) map[string]interface{} {
|
||||
return params
|
||||
}
|
||||
|
||||
func unixPtrToTimePtr(ts *int64) *time.Time {
|
||||
func normalizeRuleTimestamp(ts *int64) (*time.Time, error) {
|
||||
if ts == nil || *ts == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
value := *ts
|
||||
var t time.Time
|
||||
if value >= 1_000_000_000_000 || value <= -1_000_000_000_000 {
|
||||
t = time.UnixMilli(value)
|
||||
} else {
|
||||
t = time.Unix(value, 0)
|
||||
}
|
||||
if t.Before(minPromoRuleTime) || t.After(maxPromoRuleTime) {
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.InvalidParams), "timestamp out of range")
|
||||
}
|
||||
return &t, nil
|
||||
}
|
||||
|
||||
func unixPtrToTimePtr(ts *int64) *time.Time {
|
||||
t, err := normalizeRuleTimestamp(ts)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
t := time.Unix(*ts, 0)
|
||||
return &t
|
||||
return t
|
||||
}
|
||||
|
||||
func timePtrToUnixPtr(t *time.Time) *int64 {
|
||||
|
||||
Reference in New Issue
Block a user