master
兔子 1 year ago
parent 27897a51dc
commit 2a1cdecda7

@ -9,11 +9,27 @@ import (
"time" "time"
) )
func legalCheck(limitCode string, baseDate time.Time, opts ...startimer.TimerOptions) (startimer.StarTimer, error) {
if limitCode == "" {
return startimer.NewTimer(baseDate, opts...)
}
timer, err := startimer.NewTimer(baseDate, opts...)
if err != nil {
return timer, err
}
if strings.Contains(limitCode, "今") {
if timer.NextTimer().Day() != time.Now().Day() {
return timer, errors.New("无法添加,指定的提醒时刻不在未来。")
}
}
return timer, err
}
func matchPeriodPattern01(base time.Time, str string) (startimer.StarTimer, error) { func matchPeriodPattern01(base time.Time, str string) (startimer.StarTimer, error) {
str = transChn(str) str = transChn(str)
var rpt startimer.Repeats var rpt startimer.Repeats
var duration time.Duration var duration time.Duration
reg := regexp.MustCompile(`(每隔|每)?(\d{0,4}年)?(\d{0,5}个?月)?(\d{0,4}[明后大]{0,4}[日号天])?([上中下午夜早凌清晨傍晚里]+)?(\d{1,4}个?[点小时钟:]+)?(\d{0,4}半?[分::秒]?钟?)?(\d{0,10}半?秒?钟?)?(后)?`) reg := regexp.MustCompile(`(每隔|每)?(\d{0,4}年)?(\d{0,5}个?月)?(\d{0,4}[明后大]{0,4}[日号天])?([今天日上中下午夜早凌清晨傍晚里]+)?(\d{1,4}个?[点小时钟:]+)?(\d{0,4}半?[分::秒]?钟?)?(\d{0,10}半?秒?钟?)?(后)?`)
if reg.MatchString(str) { if reg.MatchString(str) {
pts := reg.FindStringSubmatch(str) pts := reg.FindStringSubmatch(str)
setAsDate := false setAsDate := false
@ -52,11 +68,11 @@ func matchPeriodPattern01(base time.Time, str string) (startimer.StarTimer, erro
if rpt.Every { if rpt.Every {
setAsDate = true setAsDate = true
} }
case "明天": case "明天", "明日":
rpt.Repeat = append(rpt.Repeat, startimer.Repeat{Unit: startimer.STAR_DAY, Value: uint32(now.Add(time.Hour * 24).Day())}) rpt.Repeat = append(rpt.Repeat, startimer.Repeat{Unit: startimer.STAR_DAY, Value: uint32(now.Add(time.Hour * 24).Day())})
case "后天": case "后天", "后日":
rpt.Repeat = append(rpt.Repeat, startimer.Repeat{Unit: startimer.STAR_DAY, Value: uint32(now.Add(time.Hour * 48).Day())}) rpt.Repeat = append(rpt.Repeat, startimer.Repeat{Unit: startimer.STAR_DAY, Value: uint32(now.Add(time.Hour * 48).Day())})
case "大后天": case "大后天", "大后日":
rpt.Repeat = append(rpt.Repeat, startimer.Repeat{Unit: startimer.STAR_DAY, Value: uint32(now.Add(time.Hour * 72).Day())}) rpt.Repeat = append(rpt.Repeat, startimer.Repeat{Unit: startimer.STAR_DAY, Value: uint32(now.Add(time.Hour * 72).Day())})
case "大大后天": case "大大后天":
rpt.Repeat = append(rpt.Repeat, startimer.Repeat{Unit: startimer.STAR_DAY, Value: uint32(now.Add(time.Hour * 96).Day())}) rpt.Repeat = append(rpt.Repeat, startimer.Repeat{Unit: startimer.STAR_DAY, Value: uint32(now.Add(time.Hour * 96).Day())})
@ -79,7 +95,7 @@ func matchPeriodPattern01(base time.Time, str string) (startimer.StarTimer, erro
var hour uint32 var hour uint32
if pts[6] != "" { if pts[6] != "" {
hour = uint32(getNumbers(pts[6])) hour = uint32(getNumbers(pts[6]))
if timeParse == "下午" || strings.Contains(timeParse, "晚") || strings.Contains(timeParse, "夜") { if hour < 12 && (timeParse == "下午" || strings.Contains(timeParse, "晚") || strings.Contains(timeParse, "夜")) {
hour += 12 hour += 12
} }
if rpt.Every == setAsDate { if rpt.Every == setAsDate {
@ -172,7 +188,7 @@ func matchPeriodPattern01(base time.Time, str string) (startimer.StarTimer, erro
now = now.Add(time.Duration(sr.Value) * time.Second) now = now.Add(time.Duration(sr.Value) * time.Second)
} }
} }
return startimer.NewTimer(time.Now(), startimer.WithStaticDate(now), startimer.WithRunCountLimit(count)) return legalCheck(timeParse, time.Now(), startimer.WithStaticDate(now), startimer.WithRunCountLimit(count))
} }
if duration.Seconds() > 0 { if duration.Seconds() > 0 {
rpt.Repeat = append(rpt.Repeat, startimer.Repeat{Unit: startimer.STAR_SECOND, Value: uint32(duration.Seconds())}) rpt.Repeat = append(rpt.Repeat, startimer.Repeat{Unit: startimer.STAR_SECOND, Value: uint32(duration.Seconds())})
@ -180,12 +196,17 @@ func matchPeriodPattern01(base time.Time, str string) (startimer.StarTimer, erro
if rpt.Every == setAsDate { if rpt.Every == setAsDate {
rpt.Every = false rpt.Every = false
} }
return startimer.NewTimer(base, startimer.WithRepeats(&rpt), startimer.WithRunCountLimit(count)) return legalCheck(timeParse, base, startimer.WithRepeats(&rpt), startimer.WithRunCountLimit(count))
} }
return startimer.StarTimer{}, errors.New("no Match") return startimer.StarTimer{}, errors.New("no Match")
} }
func matchPeroidPattern02(base time.Time, str string) (startimer.StarTimer, error) { func matchPeroidPattern02(base time.Time, str string) (startimer.StarTimer, error) {
preReg := "(周|礼拜|星期)([1-7一二三四五六七日天])([0-9一二三四五六七八九零])"
preExp := regexp.MustCompile(preReg)
if preExp.MatchString(str) {
str = preExp.ReplaceAllString(str, "${1}${2}的${3}")
}
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")

@ -10,16 +10,19 @@ func TestParse(t *testing.T) {
for k, code := range []string{"一个小时后告诉我事情", "三个小时后爱我", "每两个小时提醒我吃饭", for k, code := range []string{"一个小时后告诉我事情", "三个小时后爱我", "每两个小时提醒我吃饭",
"每五个月零二十五天三小时25分15秒告诉我时间", "5月23日上午3点24分12秒打我", "周五上午11点提醒我", "5时25分提醒我", "每五个月零二十五天三小时25分15秒告诉我时间", "5月23日上午3点24分12秒打我", "周五上午11点提醒我", "5时25分提醒我",
"每周一到周五上午8点提醒我吃饭", "每天晚上8点提醒我吃饭", "每月16号晚上8点提醒我吃饭", "晚上8:30提醒我", "每周一到周五上午8点提醒我吃饭", "每天晚上8点提醒我吃饭", "每月16号晚上8点提醒我吃饭", "晚上8:30提醒我",
"晚上八点半提醒我", "6分钟后提醒我", "凌晨0点半", "每20秒提醒我", "半小时后提醒我", "半分钟后提醒我", "一分半后提醒我", "00:00提醒我", "凌晨零点提醒我"} { "晚上八点半提醒我", "6分钟后提醒我", "凌晨0点半", "每20秒提醒我", "半小时后提醒我", "半分钟后提醒我", "一分半后提醒我", "00:00提醒我", "凌晨零点提醒我", "今晚九点二十提醒我", "周五九点二十提醒我"} {
a, _ := WhenWithPeriod(code) a, err := WhenWithPeriod(code)
//fmt.Println(a.Repeats()[0]) //fmt.Println(a.Repeats()[0])
if err != nil {
fmt.Println(err)
}
fmt.Println(a.NextTimer(), a.RunCountLimit(), code, k) fmt.Println(a.NextTimer(), a.RunCountLimit(), code, k)
fmt.Println(a.NextTimerAfterDate(time.Now().Add(time.Hour * 72))) fmt.Println(a.NextTimerAfterDate(time.Now().Add(time.Hour * 72)))
} }
} }
func TestSigParse(t *testing.T) { func TestSigParse(t *testing.T) {
for _, code := range []string{"每周一到周五11:25提醒大家干饭了"} { for _, code := range []string{"周五九点二十提醒我"} {
a, err := WhenWithPeriod(code) a, err := WhenWithPeriod(code)
fmt.Println(err) fmt.Println(err)
fmt.Println(a.NextTimer(), a.RunCountLimit(), code) fmt.Println(a.NextTimer(), a.RunCountLimit(), code)

Loading…
Cancel
Save