master
兔子 2 years ago
parent 4ff7bd6ba7
commit 27897a51dc

@ -187,8 +187,13 @@ func matchPeriodPattern01(base time.Time, str string) (startimer.StarTimer, erro
func matchPeroidPattern02(base time.Time, str string) (startimer.StarTimer, error) { func matchPeroidPattern02(base time.Time, str string) (startimer.StarTimer, error) {
str = transChn(str) str = transChn(str)
str = strings.ReplaceAll(str, "周日", "周0")
str = strings.ReplaceAll(str, "礼拜天", "周0")
str = strings.ReplaceAll(str, "星期日", "周0")
str = strings.ReplaceAll(str, "星期天", "周0")
str = strings.ReplaceAll(str, "周天", "周0")
var rpt startimer.Repeats var rpt startimer.Repeats
reg := regexp.MustCompile(`(每)?([周星期礼拜][周礼拜星期到1-6日天]+)+的?([上中下午夜早凌清晨傍晚里]+)?(\d{1,4}[个点时:]+)?(\d{0,4}[分半:]?钟?)?(\d{1,10}秒?钟?)?`) reg := regexp.MustCompile(`(每)?([周星期礼拜日天]\d{0,1})到?([周礼拜星期日天]?\d{0,1})?的?([上中下午夜早凌清晨傍晚里]+)?(\d{1,4}[个点时:]+)?(\d{0,4}[分半:]?钟?)?(\d{1,10}秒?钟?)?`)
if reg.MatchString(str) { if reg.MatchString(str) {
pts := reg.FindStringSubmatch(str) pts := reg.FindStringSubmatch(str)
scs := len(strings.Split(pts[0], "周")) scs := len(strings.Split(pts[0], "周"))
@ -199,15 +204,9 @@ func matchPeroidPattern02(base time.Time, str string) (startimer.StarTimer, erro
return startimer.StarTimer{}, errors.New("Invalid stings") return startimer.StarTimer{}, errors.New("Invalid stings")
} }
if pts[2] != "" { if pts[2] != "" {
wk := strings.ReplaceAll(pts[2], "周日", "周0") if len(pts[3]) != 0 {
wk = strings.ReplaceAll(wk, "礼拜天", "周0") startWk := getNumbers(pts[2])
wk = strings.ReplaceAll(wk, "星期日", "周0") endWk := getNumbers(pts[3])
wk = strings.ReplaceAll(wk, "星期天", "周0")
wk = strings.ReplaceAll(wk, "周天", "周0")
if strings.Contains(wk, "到") {
pk := strings.Split(wk, "到")
startWk := getNumbers(pk[0])
endWk := getNumbers(pk[1])
if endWk < startWk { if endWk < startWk {
endWk += 7 endWk += 7
} }
@ -219,15 +218,15 @@ func matchPeroidPattern02(base time.Time, str string) (startimer.StarTimer, erro
rpt.Repeat = append(rpt.Repeat, startimer.Repeat{Unit: startimer.STAR_WEEK, Value: uint32(num)}) rpt.Repeat = append(rpt.Repeat, startimer.Repeat{Unit: startimer.STAR_WEEK, Value: uint32(num)})
} }
} else { } else {
wkNum := getNumbers(wk) wkNum := getNumbers(pts[2])
rpt.Repeat = append(rpt.Repeat, startimer.Repeat{Unit: startimer.STAR_WEEK, Value: uint32(wkNum)}) rpt.Repeat = append(rpt.Repeat, startimer.Repeat{Unit: startimer.STAR_WEEK, Value: uint32(wkNum)})
} }
} }
timeParse := pts[3] timeParse := pts[4]
var hour uint32 var hour uint32
if pts[4] != "" { if pts[5] != "" {
hour = uint32(getNumbers(pts[4])) hour = uint32(getNumbers(pts[5]))
if timeParse == "下午" || strings.Contains(timeParse, "晚") || strings.Contains(timeParse, "夜") { if timeParse == "下午" || strings.Contains(timeParse, "晚") || strings.Contains(timeParse, "夜") {
hour += 12 hour += 12
} }
@ -251,16 +250,16 @@ func matchPeroidPattern02(base time.Time, str string) (startimer.StarTimer, erro
} }
rpt.Repeat = append(rpt.Repeat, startimer.Repeat{Unit: startimer.STAR_HOUR, Value: hour}) rpt.Repeat = append(rpt.Repeat, startimer.Repeat{Unit: startimer.STAR_HOUR, Value: hour})
} }
if pts[5] != "" { if pts[6] != "" {
if pts[5] == "半" { if pts[6] == "半" {
pts[5] = "30" pts[6] = "30"
} }
rpt.Repeat = append(rpt.Repeat, startimer.Repeat{Unit: startimer.STAR_MINUTE, Value: uint32(getNumbers(pts[5]))}) rpt.Repeat = append(rpt.Repeat, startimer.Repeat{Unit: startimer.STAR_MINUTE, Value: uint32(getNumbers(pts[6]))})
} else if hour != 0 { } else if hour != 0 {
rpt.Repeat = append(rpt.Repeat, startimer.Repeat{Unit: startimer.STAR_MINUTE, Value: 0}) rpt.Repeat = append(rpt.Repeat, startimer.Repeat{Unit: startimer.STAR_MINUTE, Value: 0})
} }
if pts[6] != "" { if pts[7] != "" {
rpt.Repeat = append(rpt.Repeat, startimer.Repeat{Unit: startimer.STAR_SECOND, Value: uint32(getNumbers(pts[6]))}) rpt.Repeat = append(rpt.Repeat, startimer.Repeat{Unit: startimer.STAR_SECOND, Value: uint32(getNumbers(pts[7]))})
} else if hour != 0 { } else if hour != 0 {
rpt.Repeat = append(rpt.Repeat, startimer.Repeat{Unit: startimer.STAR_SECOND, Value: 0}) rpt.Repeat = append(rpt.Repeat, startimer.Repeat{Unit: startimer.STAR_SECOND, Value: 0})
} }

@ -19,10 +19,12 @@ func TestParse(t *testing.T) {
} }
func TestSigParse(t *testing.T) { func TestSigParse(t *testing.T) {
for _, code := range []string{"凌晨零点提醒我"} { for _, code := range []string{"每周一到周五11:25提醒大家干饭了"} {
a, _ := WhenWithPeriod(code) a, err := WhenWithPeriod(code)
//fmt.Println(a.Repeats()[0]) fmt.Println(err)
fmt.Println(a.NextTimer(), a.RunCountLimit(), code) fmt.Println(a.NextTimer(), a.RunCountLimit(), code)
pm := a.NextTimer()
fmt.Println(pm)
fmt.Println(a.NextTimerAfterDate(time.Now().Add(time.Hour * 72))) fmt.Println(a.NextTimerAfterDate(time.Now().Add(time.Hour * 72)))
} }
} }

Loading…
Cancel
Save