You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
233 lines
6.6 KiB
Go
233 lines
6.6 KiB
Go
package remind
|
|
|
|
import (
|
|
"b612.me/startimer"
|
|
"regexp"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
func matchPattern01(str string) (startimer.StarTimer, bool) {
|
|
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}秒?)?`)
|
|
if reg.MatchString(str) {
|
|
pts := reg.FindStringSubmatch(str)
|
|
var timeParse = pts[5]
|
|
if pts[1] != "" {
|
|
rpt.Every = true
|
|
}
|
|
if pts[2] != "" {
|
|
if !rpt.Every {
|
|
rpt.Repeat = append(rpt.Repeat, startimer.Repeat{Unit: startimer.STAR_YEAR, Value: uint32(getNumbers(pts[2]))})
|
|
} else {
|
|
duration += time.Hour * 24 * 365 * time.Duration(getNumbers(pts[2]))
|
|
}
|
|
}
|
|
if pts[3] != "" {
|
|
if !rpt.Every {
|
|
rpt.Repeat = append(rpt.Repeat, startimer.Repeat{Unit: startimer.STAR_MONTH, Value: uint32(getNumbers(pts[3]))})
|
|
} else {
|
|
duration += time.Hour * 24 * 30 * time.Duration(getNumbers(pts[3]))
|
|
}
|
|
}
|
|
if pts[4] != "" {
|
|
now := time.Now()
|
|
switch pts[4] {
|
|
case "明天":
|
|
rpt.Repeat = append(rpt.Repeat, startimer.Repeat{Unit: startimer.STAR_DAY, Value: uint32(now.Add(time.Hour * 24).Day())})
|
|
case "后天":
|
|
rpt.Repeat = append(rpt.Repeat, startimer.Repeat{Unit: startimer.STAR_DAY, Value: uint32(now.Add(time.Hour * 48).Day())})
|
|
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())})
|
|
default:
|
|
if !rpt.Every {
|
|
rpt.Repeat = append(rpt.Repeat, startimer.Repeat{Unit: startimer.STAR_MONTH, Value: uint32(getNumbers(pts[4]))})
|
|
} else {
|
|
duration += time.Hour * 24 * time.Duration(getNumbers(pts[4]))
|
|
}
|
|
}
|
|
}
|
|
setAsDate := false
|
|
if rpt.Every && timeParse != "" {
|
|
setAsDate = true
|
|
} else if rpt.Every && !strings.Contains(pts[6], "小时") {
|
|
setAsDate = true
|
|
base = time.Date(base.Year(), base.Month(), base.Day(), 0, 0, 0, 0, base.Location())
|
|
}
|
|
if pts[6] != "" {
|
|
hour := uint32(getNumbers(pts[6]))
|
|
if timeParse == "下午" || strings.Contains(timeParse, "晚") || strings.Contains(timeParse, "夜") {
|
|
hour += 12
|
|
}
|
|
if !setAsDate && !rpt.Every {
|
|
rpt.Repeat = append(rpt.Repeat, startimer.Repeat{Unit: startimer.STAR_HOUR, Value: hour})
|
|
} else if rpt.Every && !setAsDate {
|
|
duration += time.Hour * time.Duration(hour)
|
|
} else {
|
|
base.Add(time.Hour * time.Duration(hour))
|
|
}
|
|
} else if timeParse != "" {
|
|
var hour uint32
|
|
switch timeParse {
|
|
case "上午":
|
|
hour = 9
|
|
case "中午":
|
|
hour = 8
|
|
case "下午":
|
|
hour = 15
|
|
case "傍晚":
|
|
hour = 18
|
|
case "夜里", "晚上", "晚", "夜", "夜晚":
|
|
hour = 21
|
|
case "凌晨":
|
|
hour = 0
|
|
case "清晨":
|
|
hour = 6
|
|
}
|
|
if !setAsDate {
|
|
rpt.Repeat = append(rpt.Repeat, startimer.Repeat{Unit: startimer.STAR_HOUR, Value: hour})
|
|
} else {
|
|
base.Add(time.Hour * time.Duration(hour))
|
|
}
|
|
}
|
|
if pts[7] != "" {
|
|
if !setAsDate && !rpt.Every {
|
|
rpt.Repeat = append(rpt.Repeat, startimer.Repeat{Unit: startimer.STAR_MINUTE, Value: uint32(getNumbers(pts[7]))})
|
|
} else if rpt.Every && !setAsDate {
|
|
duration += time.Minute * time.Duration(uint32(getNumbers(pts[7])))
|
|
} else {
|
|
base.Add(time.Minute * time.Duration(uint32(getNumbers(pts[7]))))
|
|
}
|
|
|
|
}
|
|
if pts[8] != "" {
|
|
if !setAsDate && !rpt.Every {
|
|
rpt.Repeat = append(rpt.Repeat, startimer.Repeat{Unit: startimer.STAR_SECOND, Value: uint32(getNumbers(pts[8]))})
|
|
} else if rpt.Every && !setAsDate {
|
|
duration += time.Second * time.Duration(uint32(getNumbers(pts[8])))
|
|
} else {
|
|
base.Add(time.Second * time.Duration(uint32(getNumbers(pts[8]))))
|
|
}
|
|
}
|
|
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.StarTimer{}, false
|
|
}
|
|
|
|
/*
|
|
func matchPattern02(str string) (startimer.Repeats, bool) {
|
|
str = transChn(str)
|
|
var rpt startimer.Repeats
|
|
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()
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
*/
|
|
|
|
func transChn(msg string) string {
|
|
var res []rune
|
|
var tmpMap []rune
|
|
keyMap := map[rune]int{
|
|
'零': 0, '一': 1, '二': 2, '三': 3, '四': 4, '五': 5, '六': 6, '七': 7, '八': 8, '九': 9, '十': 10, '百': 100, '千': 1000, '万': 10000, '亿': 100000000, '两': 2, '俩': 2,
|
|
}
|
|
for _, v := range []rune(msg) {
|
|
if _, ok := keyMap[v]; ok {
|
|
tmpMap = append(tmpMap, v)
|
|
} else {
|
|
if len(tmpMap) != 0 {
|
|
res = append(res, []rune(strconv.Itoa(transfer(string(tmpMap))))...)
|
|
tmpMap = []rune{}
|
|
}
|
|
res = append(res, v)
|
|
}
|
|
}
|
|
return string(res)
|
|
}
|
|
func transfer(msg string) int {
|
|
keyMap := map[rune]int{
|
|
'零': 0, '一': 1, '二': 2, '三': 3, '四': 4, '五': 5, '六': 6, '七': 7, '八': 8, '九': 9, '十': 10, '百': 100, '千': 1000, '万': 10000, '亿': 100000000, '两': 2, '俩': 2,
|
|
}
|
|
result := 0
|
|
secCache := 0
|
|
thrCache := 0
|
|
fKWord := map[rune]int{'百': 100, '千': 1000, '万': 10000, '亿': 100000000}
|
|
for _, num := range []rune(msg) {
|
|
if _, match := fKWord[num]; !match {
|
|
if num == '十' && thrCache != 0 {
|
|
thrCache *= keyMap[num]
|
|
} else {
|
|
thrCache += keyMap[num]
|
|
}
|
|
} else {
|
|
if fKWord[num] < 10000 {
|
|
secCache += thrCache * fKWord[num]
|
|
thrCache = 0
|
|
} else {
|
|
secCache += thrCache
|
|
thrCache = 0
|
|
if secCache == 0 {
|
|
result *= fKWord[num]
|
|
continue
|
|
}
|
|
result += secCache * fKWord[num]
|
|
secCache = 0
|
|
}
|
|
}
|
|
}
|
|
|
|
result += secCache + thrCache
|
|
return result
|
|
}
|
|
|
|
func getNumbers(s string) float64 {
|
|
res := ""
|
|
isNegative := false
|
|
|
|
for _, c := range s {
|
|
switch {
|
|
case c == '-' && len(res) == 0:
|
|
isNegative = true
|
|
case c >= '0' && c <= '9':
|
|
res += string(c)
|
|
case c == '.' && !strings.Contains(res, "."):
|
|
res += string(c)
|
|
}
|
|
}
|
|
|
|
if res == "" {
|
|
return 0
|
|
}
|
|
|
|
if isNegative {
|
|
res = "-" + res
|
|
}
|
|
|
|
num, _ := strconv.ParseFloat(res, 64)
|
|
return num
|
|
}
|