|
|
|
@ -9,11 +9,27 @@ import (
|
|
|
|
|
"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) {
|
|
|
|
|
str = transChn(str)
|
|
|
|
|
var rpt startimer.Repeats
|
|
|
|
|
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) {
|
|
|
|
|
pts := reg.FindStringSubmatch(str)
|
|
|
|
|
setAsDate := false
|
|
|
|
@ -52,11 +68,11 @@ func matchPeriodPattern01(base time.Time, str string) (startimer.StarTimer, erro
|
|
|
|
|
if rpt.Every {
|
|
|
|
|
setAsDate = true
|
|
|
|
|
}
|
|
|
|
|
case "明天":
|
|
|
|
|
case "明天", "明日":
|
|
|
|
|
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())})
|
|
|
|
|
case "大后天":
|
|
|
|
|
case "大后天", "大后日":
|
|
|
|
|
rpt.Repeat = append(rpt.Repeat, startimer.Repeat{Unit: startimer.STAR_DAY, Value: uint32(now.Add(time.Hour * 72).Day())})
|
|
|
|
|
case "大大后天":
|
|
|
|
|
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
|
|
|
|
|
if 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
|
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
|
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 {
|
|
|
|
|
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")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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 = strings.ReplaceAll(str, "周日", "周0")
|
|
|
|
|
str = strings.ReplaceAll(str, "礼拜天", "周0")
|
|
|
|
|