From d9af4978c67ddeddefa64727070f879daae17b1f Mon Sep 17 00:00:00 2001 From: starainrt Date: Tue, 16 May 2023 16:56:50 +0800 Subject: [PATCH] update new func --- go.mod | 8 +++ go.sum | 4 ++ remind/parse_test.go | 15 ----- remind/remind.go | 29 ++++++++ {remind => when}/parse.go | 137 +++++++++++++++++++++++++++++++------- when/parse_test.go | 14 ++++ 6 files changed, 168 insertions(+), 39 deletions(-) create mode 100644 go.mod create mode 100644 go.sum delete mode 100644 remind/parse_test.go create mode 100644 remind/remind.go rename {remind => when}/parse.go (58%) create mode 100644 when/parse_test.go diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..281b430 --- /dev/null +++ b/go.mod @@ -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 +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..a6b7d4f --- /dev/null +++ b/go.sum @@ -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= diff --git a/remind/parse_test.go b/remind/parse_test.go deleted file mode 100644 index cbde165..0000000 --- a/remind/parse_test.go +++ /dev/null @@ -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()) -} diff --git a/remind/remind.go b/remind/remind.go new file mode 100644 index 0000000..f77d48a --- /dev/null +++ b/remind/remind.go @@ -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)", + } +} diff --git a/remind/parse.go b/when/parse.go similarity index 58% rename from remind/parse.go rename to when/parse.go index fa604a0..b746aa2 100644 --- a/remind/parse.go +++ b/when/parse.go @@ -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 diff --git a/when/parse_test.go b/when/parse_test.go new file mode 100644 index 0000000..9031767 --- /dev/null +++ b/when/parse_test.go @@ -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) + } +}