update new func

master
兔子 2 years ago
parent ce84ad4f01
commit d9af4978c6

@ -0,0 +1,8 @@
module b612.me/sdk/candy
go 1.20
require (
b612.me/stardb v1.1.2
b612.me/startimer v0.0.0-20230501080256-b0fd8947f0dc
)

@ -0,0 +1,4 @@
b612.me/stardb v1.1.2 h1:xr5Ovqd5kVSnV1W7eGJJwy+CyuqSwMmsFnPq/YxIWqg=
b612.me/stardb v1.1.2/go.mod h1:qtGEu+joEQxFESl3tse5xqiD767f6sAmHD284+Xoy48=
b612.me/startimer v0.0.0-20230501080256-b0fd8947f0dc h1:WO8jZiLVssi3myTWX5Oo/+/i9vk+Alo4ymJhHi4TXp0=
b612.me/startimer v0.0.0-20230501080256-b0fd8947f0dc/go.mod h1:8fw+OU7SnxzuLkNCYSbRKmvs4WtHs6SbCHuTf5F6s+U=

@ -1,15 +0,0 @@
package remind
import (
"fmt"
"testing"
)
func TestParse(t *testing.T) {
a, _ := matchPattern01("每两个小时")
fmt.Println(a.NextTimer(), a.BaseDate())
a, _ = matchPattern01("每五个月零二十五天三小时25分15秒")
fmt.Println(a.NextTimer(), a.BaseDate())
a, _ = matchPattern01("5月23日3点24分12秒")
fmt.Println(a.NextTimer(), a.BaseDate())
}

@ -0,0 +1,29 @@
package remind
import (
"b612.me/stardb"
"b612.me/startimer"
"sync"
)
type Remind struct {
db stardb.StarDB
tasks map[string]RemindTask
mu sync.Mutex
callBack func(remind RemindTask)
}
type RemindTask struct {
ID int `db:"id"`
Origin string `db:"text"`
timer startimer.StarTimer
Key string `db:"key"`
Msg []byte `db:"msg"`
}
func getCreateSql() []string {
return []string{
"CREATE TABLE IF NOT EXISTS remind(id INTEGER PRIMARY KEY AUTOINCREMENT,key VARCHAR(64),text TEXT,msg BLOB)",
"CREATE INDEX IF NOT EXISTS key_idx ON remind (key)",
}
}

@ -1,24 +1,26 @@
package remind package when
import ( import (
"b612.me/startimer" "b612.me/startimer"
"errors"
"regexp" "regexp"
"strconv" "strconv"
"strings" "strings"
"time" "time"
) )
func matchPattern01(str string) (startimer.StarTimer, bool) { func matchPeriodPattern01(base time.Time, str string) (startimer.StarTimer, error) {
str = transChn(str) str = transChn(str)
var base = time.Now()
var rpt startimer.Repeats var rpt startimer.Repeats
var duration time.Duration 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) { if reg.MatchString(str) {
pts := reg.FindStringSubmatch(str) pts := reg.FindStringSubmatch(str)
var timeParse = pts[5] var timeParse = pts[5]
count := 1
if pts[1] != "" { if pts[1] != "" {
rpt.Every = true rpt.Every = true
count = 0
} }
if pts[2] != "" { if pts[2] != "" {
if !rpt.Every { 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())}) rpt.Repeat = append(rpt.Repeat, startimer.Repeat{Unit: startimer.STAR_DAY, Value: uint32(now.Add(time.Hour * 96).Day())})
default: default:
if !rpt.Every { 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 { } else {
duration += time.Hour * 24 * time.Duration(getNumbers(pts[4])) duration += time.Hour * 24 * time.Duration(getNumbers(pts[4]))
} }
@ -60,8 +62,9 @@ func matchPattern01(str string) (startimer.StarTimer, bool) {
setAsDate = true setAsDate = true
base = time.Date(base.Year(), base.Month(), base.Day(), 0, 0, 0, 0, base.Location()) base = time.Date(base.Year(), base.Month(), base.Day(), 0, 0, 0, 0, base.Location())
} }
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 timeParse == "下午" || strings.Contains(timeParse, "晚") || strings.Contains(timeParse, "夜") {
hour += 12 hour += 12
} }
@ -73,7 +76,6 @@ func matchPattern01(str string) (startimer.StarTimer, bool) {
base.Add(time.Hour * time.Duration(hour)) base.Add(time.Hour * time.Duration(hour))
} }
} else if timeParse != "" { } else if timeParse != "" {
var hour uint32
switch timeParse { switch timeParse {
case "上午": case "上午":
hour = 9 hour = 9
@ -104,8 +106,12 @@ func matchPattern01(str string) (startimer.StarTimer, bool) {
} else { } else {
base.Add(time.Minute * time.Duration(uint32(getNumbers(pts[7])))) 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 pts[8] != "" {
if !setAsDate && !rpt.Every { if !setAsDate && !rpt.Every {
rpt.Repeat = append(rpt.Repeat, startimer.Repeat{Unit: startimer.STAR_SECOND, Value: uint32(getNumbers(pts[8]))}) 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 { } else {
base.Add(time.Second * time.Duration(uint32(getNumbers(pts[8])))) 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 { 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())})
} }
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 matchPeroidPattern02(base time.Time, str string) (startimer.StarTimer, error) {
func matchPattern02(str string) (startimer.Repeats, bool) {
str = transChn(str) str = transChn(str)
var rpt startimer.Repeats 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) { if reg.MatchString(str) {
pts := reg.FindStringSubmatch(str) pts := reg.FindStringSubmatch(str)
if pts[1] != "" {
rpt.Every = true
}
if pts[2] != "" { if pts[2] != "" {
if strings.Contains(pts[2], "到") { wk := strings.ReplaceAll(pts[2], "周日", "周0")
se := strings.Split(pts[2], "到") wk = strings.ReplaceAll(wk, "礼拜天", "周0")
start := getNumbers(se[0]) wk = strings.ReplaceAll(wk, "星期日", "周0")
end := getNumbers(se[1]) wk = strings.ReplaceAll(wk, "星期天", "周0")
if end >= start { wk = strings.ReplaceAll(wk, "周天", "周0")
for i := start; i <= end; i++ { if strings.Contains(wk, "到") {
startimer.NextDayOfWeek() 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 { func transChn(msg string) string {
var res []rune var res []rune

@ -0,0 +1,14 @@
package when
import (
"fmt"
"testing"
)
func TestParse(t *testing.T) {
for _, code := range []string{"一个小时后告诉我事情", "三个小时后爱我", "每两个小时提醒我吃饭", "每五个月零二十五天三小时25分15秒告诉我时间", "5月23日上午3点24分12秒打我", "周四上午11点提醒我"} {
a, _ := WhenWithPeriod(code)
//fmt.Println(a.Repeats()[0])
fmt.Println(a.NextTimer(), a.RunCountLimit(), code)
}
}
Loading…
Cancel
Save