|
|
|
@ -1,24 +1,26 @@
|
|
|
|
|
package remind
|
|
|
|
|
package when
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"b612.me/startimer"
|
|
|
|
|
"errors"
|
|
|
|
|
"regexp"
|
|
|
|
|
"strconv"
|
|
|
|
|
"strings"
|
|
|
|
|
"time"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func matchPattern01(str string) (startimer.StarTimer, bool) {
|
|
|
|
|
func matchPeriodPattern01(base time.Time, str string) (startimer.StarTimer, error) {
|
|
|
|
|
str = transChn(str)
|
|
|
|
|
var base = time.Now()
|
|
|
|
|
var rpt startimer.Repeats
|
|
|
|
|
var duration time.Duration
|
|
|
|
|
reg := regexp.MustCompile(`(每隔|每)?(\d{1,4}年)?(\d{1,5}个?月)?(\d{1,4}[明后大]{0,4}[日号天])?([上中下午夜早凌清晨傍晚里]+)?(\d{1,4}个?[点小时:]+)?(\d{1,4}[分:])?(\d{1,10}秒?)?`)
|
|
|
|
|
reg := regexp.MustCompile(`(每隔|每)?(\d{1,4}年)?(\d{1,5}个?月)?(\d{1,4}[明后大]{0,4}[日号天])?([上中下午夜早凌清晨傍晚里]+)?(\d{1,4}个?[点小时钟:]+)?(\d{1,4}[分:])?(\d{1,10}秒?)?(后)?`)
|
|
|
|
|
if reg.MatchString(str) {
|
|
|
|
|
pts := reg.FindStringSubmatch(str)
|
|
|
|
|
var timeParse = pts[5]
|
|
|
|
|
count := 1
|
|
|
|
|
if pts[1] != "" {
|
|
|
|
|
rpt.Every = true
|
|
|
|
|
count = 0
|
|
|
|
|
}
|
|
|
|
|
if pts[2] != "" {
|
|
|
|
|
if !rpt.Every {
|
|
|
|
@ -47,7 +49,7 @@ func matchPattern01(str string) (startimer.StarTimer, bool) {
|
|
|
|
|
rpt.Repeat = append(rpt.Repeat, startimer.Repeat{Unit: startimer.STAR_DAY, Value: uint32(now.Add(time.Hour * 96).Day())})
|
|
|
|
|
default:
|
|
|
|
|
if !rpt.Every {
|
|
|
|
|
rpt.Repeat = append(rpt.Repeat, startimer.Repeat{Unit: startimer.STAR_MONTH, Value: uint32(getNumbers(pts[4]))})
|
|
|
|
|
rpt.Repeat = append(rpt.Repeat, startimer.Repeat{Unit: startimer.STAR_DAY, Value: uint32(getNumbers(pts[4]))})
|
|
|
|
|
} else {
|
|
|
|
|
duration += time.Hour * 24 * time.Duration(getNumbers(pts[4]))
|
|
|
|
|
}
|
|
|
|
@ -60,8 +62,9 @@ func matchPattern01(str string) (startimer.StarTimer, bool) {
|
|
|
|
|
setAsDate = true
|
|
|
|
|
base = time.Date(base.Year(), base.Month(), base.Day(), 0, 0, 0, 0, base.Location())
|
|
|
|
|
}
|
|
|
|
|
var hour uint32
|
|
|
|
|
if pts[6] != "" {
|
|
|
|
|
hour := uint32(getNumbers(pts[6]))
|
|
|
|
|
hour = uint32(getNumbers(pts[6]))
|
|
|
|
|
if timeParse == "下午" || strings.Contains(timeParse, "晚") || strings.Contains(timeParse, "夜") {
|
|
|
|
|
hour += 12
|
|
|
|
|
}
|
|
|
|
@ -73,7 +76,6 @@ func matchPattern01(str string) (startimer.StarTimer, bool) {
|
|
|
|
|
base.Add(time.Hour * time.Duration(hour))
|
|
|
|
|
}
|
|
|
|
|
} else if timeParse != "" {
|
|
|
|
|
var hour uint32
|
|
|
|
|
switch timeParse {
|
|
|
|
|
case "上午":
|
|
|
|
|
hour = 9
|
|
|
|
@ -104,8 +106,12 @@ func matchPattern01(str string) (startimer.StarTimer, bool) {
|
|
|
|
|
} else {
|
|
|
|
|
base.Add(time.Minute * time.Duration(uint32(getNumbers(pts[7]))))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} else if hour != 0 {
|
|
|
|
|
if !rpt.Every && !setAsDate {
|
|
|
|
|
rpt.Repeat = append(rpt.Repeat, startimer.Repeat{Unit: startimer.STAR_MINUTE, Value: 0})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if pts[8] != "" {
|
|
|
|
|
if !setAsDate && !rpt.Every {
|
|
|
|
|
rpt.Repeat = append(rpt.Repeat, startimer.Repeat{Unit: startimer.STAR_SECOND, Value: uint32(getNumbers(pts[8]))})
|
|
|
|
@ -114,40 +120,123 @@ func matchPattern01(str string) (startimer.StarTimer, bool) {
|
|
|
|
|
} else {
|
|
|
|
|
base.Add(time.Second * time.Duration(uint32(getNumbers(pts[8]))))
|
|
|
|
|
}
|
|
|
|
|
} else if hour != 0 {
|
|
|
|
|
if !rpt.Every && !setAsDate {
|
|
|
|
|
rpt.Repeat = append(rpt.Repeat, startimer.Repeat{Unit: startimer.STAR_SECOND, Value: 0})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if pts[9] != "" {
|
|
|
|
|
if rpt.Every {
|
|
|
|
|
return startimer.StarTimer{}, errors.New("Invalid Setences")
|
|
|
|
|
}
|
|
|
|
|
now := time.Now()
|
|
|
|
|
for _, sr := range rpt.Repeat {
|
|
|
|
|
switch sr.Unit {
|
|
|
|
|
case startimer.STAR_YEAR:
|
|
|
|
|
now = now.AddDate(int(sr.Value), 0, 0)
|
|
|
|
|
case startimer.STAR_MONTH:
|
|
|
|
|
now = now.AddDate(0, int(sr.Value), 0)
|
|
|
|
|
case startimer.STAR_DAY:
|
|
|
|
|
now = now.AddDate(0, 0, int(sr.Value))
|
|
|
|
|
case startimer.STAR_HOUR:
|
|
|
|
|
now = now.Add(time.Duration(sr.Value) * time.Hour)
|
|
|
|
|
case startimer.STAR_MINUTE:
|
|
|
|
|
now = now.Add(time.Duration(sr.Value) * time.Minute)
|
|
|
|
|
case startimer.STAR_SECOND:
|
|
|
|
|
now = now.Add(time.Duration(sr.Value) * time.Second)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return startimer.NewTimer(time.Now(), startimer.WithStaticDate(now), startimer.WithRunCountLimit(count)), nil
|
|
|
|
|
}
|
|
|
|
|
if duration.Seconds() > 0 {
|
|
|
|
|
rpt.Repeat = append(rpt.Repeat, startimer.Repeat{Unit: startimer.STAR_SECOND, Value: uint32(duration.Seconds())})
|
|
|
|
|
}
|
|
|
|
|
return startimer.NewTimer(base, startimer.WithRepeats(&rpt)), true
|
|
|
|
|
return startimer.NewTimer(base, startimer.WithRepeats(&rpt), startimer.WithRunCountLimit(count)), nil
|
|
|
|
|
}
|
|
|
|
|
return startimer.StarTimer{}, false
|
|
|
|
|
return startimer.StarTimer{}, errors.New("no Match")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
func matchPattern02(str string) (startimer.Repeats, bool) {
|
|
|
|
|
func matchPeroidPattern02(base time.Time, str string) (startimer.StarTimer, error) {
|
|
|
|
|
str = transChn(str)
|
|
|
|
|
var rpt startimer.Repeats
|
|
|
|
|
reg := regexp.MustCompile(`(每隔|每)?(周[周到1-6日]+)+([上中下午夜早凌清晨傍晚里]+)?(\d{1,4}[个点小时:]+)?(\d{1,4}[分:])?(\d{1,10}秒?)?`)
|
|
|
|
|
reg := regexp.MustCompile(`(每)?([周星期礼拜][周礼拜星期到1-6日天]+)+的?([上中下午夜早凌清晨傍晚里]+)?(\d{1,4}[个点时:]+)?(\d{1,4}[分:])?(\d{1,10}秒?)?`)
|
|
|
|
|
if reg.MatchString(str) {
|
|
|
|
|
pts := reg.FindStringSubmatch(str)
|
|
|
|
|
if pts[1] != "" {
|
|
|
|
|
rpt.Every = true
|
|
|
|
|
}
|
|
|
|
|
if pts[2] != "" {
|
|
|
|
|
if strings.Contains(pts[2], "到") {
|
|
|
|
|
se := strings.Split(pts[2], "到")
|
|
|
|
|
start := getNumbers(se[0])
|
|
|
|
|
end := getNumbers(se[1])
|
|
|
|
|
if end >= start {
|
|
|
|
|
for i := start; i <= end; i++ {
|
|
|
|
|
startimer.NextDayOfWeek()
|
|
|
|
|
wk := strings.ReplaceAll(pts[2], "周日", "周0")
|
|
|
|
|
wk = strings.ReplaceAll(wk, "礼拜天", "周0")
|
|
|
|
|
wk = strings.ReplaceAll(wk, "星期日", "周0")
|
|
|
|
|
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 {
|
|
|
|
|
endWk += 7
|
|
|
|
|
}
|
|
|
|
|
for i := startWk; i <= endWk; i++ {
|
|
|
|
|
num := i
|
|
|
|
|
if num > 6 {
|
|
|
|
|
num -= 7
|
|
|
|
|
}
|
|
|
|
|
rpt.Repeat = append(rpt.Repeat, startimer.Repeat{Unit: startimer.STAR_WEEK, Value: uint32(num)})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
timeParse := pts[3]
|
|
|
|
|
var hour uint32
|
|
|
|
|
if pts[4] != "" {
|
|
|
|
|
hour = uint32(getNumbers(pts[4]))
|
|
|
|
|
if timeParse == "下午" || strings.Contains(timeParse, "晚") || strings.Contains(timeParse, "夜") {
|
|
|
|
|
hour += 12
|
|
|
|
|
}
|
|
|
|
|
rpt.Repeat = append(rpt.Repeat, startimer.Repeat{Unit: startimer.STAR_HOUR, Value: hour})
|
|
|
|
|
} else if timeParse != "" {
|
|
|
|
|
switch timeParse {
|
|
|
|
|
case "上午":
|
|
|
|
|
hour = 9
|
|
|
|
|
case "中午":
|
|
|
|
|
hour = 8
|
|
|
|
|
case "下午":
|
|
|
|
|
hour = 15
|
|
|
|
|
case "傍晚":
|
|
|
|
|
hour = 18
|
|
|
|
|
case "夜里", "晚上", "晚", "夜", "夜晚":
|
|
|
|
|
hour = 21
|
|
|
|
|
case "凌晨":
|
|
|
|
|
hour = 0
|
|
|
|
|
case "清晨":
|
|
|
|
|
hour = 6
|
|
|
|
|
}
|
|
|
|
|
rpt.Repeat = append(rpt.Repeat, startimer.Repeat{Unit: startimer.STAR_HOUR, Value: hour})
|
|
|
|
|
}
|
|
|
|
|
if pts[5] != "" {
|
|
|
|
|
rpt.Repeat = append(rpt.Repeat, startimer.Repeat{Unit: startimer.STAR_MINUTE, Value: uint32(getNumbers(pts[5]))})
|
|
|
|
|
} else if hour != 0 {
|
|
|
|
|
rpt.Repeat = append(rpt.Repeat, startimer.Repeat{Unit: startimer.STAR_MINUTE, Value: 0})
|
|
|
|
|
}
|
|
|
|
|
if pts[6] != "" {
|
|
|
|
|
rpt.Repeat = append(rpt.Repeat, startimer.Repeat{Unit: startimer.STAR_SECOND, Value: uint32(getNumbers(pts[6]))})
|
|
|
|
|
} else if hour != 0 {
|
|
|
|
|
rpt.Repeat = append(rpt.Repeat, startimer.Repeat{Unit: startimer.STAR_SECOND, Value: 0})
|
|
|
|
|
}
|
|
|
|
|
count := 1
|
|
|
|
|
if pts[1] != "" {
|
|
|
|
|
count = 0
|
|
|
|
|
}
|
|
|
|
|
return startimer.NewTimer(base, startimer.WithRepeats(&rpt), startimer.WithRunCountLimit(count)), nil
|
|
|
|
|
}
|
|
|
|
|
return startimer.StarTimer{}, errors.New("no Match")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func WhenWithPeriod(str string) (startimer.StarTimer, error) {
|
|
|
|
|
if match, _ := regexp.MatchString(`(周|星期|礼拜)[一二三四五六天日]`, str); match {
|
|
|
|
|
return matchPeroidPattern02(time.Now(), str)
|
|
|
|
|
}
|
|
|
|
|
return matchPeriodPattern01(time.Now(), str)
|
|
|
|
|
}
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
func transChn(msg string) string {
|
|
|
|
|
var res []rune
|