save trie year tree
This commit is contained in:
parent
ef1b483b5f
commit
8452db76d1
@ -3,18 +3,25 @@ package curl2info
|
|||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"log"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/davecgh/go-spew/spew"
|
"github.com/davecgh/go-spew/spew"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// type LRValue struct {
|
||||||
|
// left, right int
|
||||||
|
// }
|
||||||
|
|
||||||
type TimePointer struct {
|
type TimePointer struct {
|
||||||
left, right int
|
left, right int
|
||||||
leftlimit, rightlimit int
|
leftlimit, rightlimit int
|
||||||
per int
|
per int
|
||||||
|
isAll bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func (tp *TimePointer) String() string {
|
func (tp *TimePointer) String() string {
|
||||||
@ -29,6 +36,7 @@ type Crontab struct {
|
|||||||
Week []TimePointer
|
Week []TimePointer
|
||||||
|
|
||||||
logMissedTime bool
|
logMissedTime bool
|
||||||
|
plans []time.Time
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewCrontab(crontab string) *Crontab {
|
func NewCrontab(crontab string) *Crontab {
|
||||||
@ -41,7 +49,92 @@ func (cron *Crontab) SetMissedLog(missedlog bool) {
|
|||||||
cron.logMissedTime = missedlog
|
cron.logMissedTime = missedlog
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func countDays(year int, month int) (days int) {
|
||||||
|
if month != 2 {
|
||||||
|
if month == 4 || month == 6 || month == 9 || month == 11 {
|
||||||
|
days = 30
|
||||||
|
|
||||||
|
} else {
|
||||||
|
days = 31
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if ((year%4) == 0 && (year%100) != 0) || (year%400) == 0 {
|
||||||
|
days = 29
|
||||||
|
} else {
|
||||||
|
days = 28
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return days
|
||||||
|
}
|
||||||
|
|
||||||
|
func DayAccordWith(day int, now *time.Time, lm *TimePointer) {
|
||||||
|
// maxday := countDays(now.Year(), int(now.Month()))
|
||||||
|
|
||||||
|
// left := lm.left
|
||||||
|
// if left < 0 {
|
||||||
|
// left = maxday + left
|
||||||
|
// }
|
||||||
|
|
||||||
|
// right := lm.right
|
||||||
|
// if right < 0 {
|
||||||
|
// right = maxday + right
|
||||||
|
// }
|
||||||
|
|
||||||
|
// if lm.left <= lm.right {
|
||||||
|
// switch {
|
||||||
|
// case day < lm.left:
|
||||||
|
// return &LRValue{lm.left, lm.right}, nil
|
||||||
|
// case day > lm.right:
|
||||||
|
// return nil, nil
|
||||||
|
// case lm.left <= day && day <= lm.right:
|
||||||
|
// return &LRValue{day, lm.right}, nil
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|
||||||
|
// return nil, fmt.Errorf("day left > right? left: %d, right: %d", lm.left, lm.right)
|
||||||
|
}
|
||||||
|
|
||||||
|
func MonthAccordWith(month int, lm *TimePointer) {
|
||||||
|
|
||||||
|
// if lm.left <= lm.right {
|
||||||
|
// switch {
|
||||||
|
// case month < lm.left:
|
||||||
|
// return &LRValue{lm.left, lm.right}, nil
|
||||||
|
// case month > lm.right:
|
||||||
|
// return nil, nil
|
||||||
|
// case lm.left <= month && month <= lm.right:
|
||||||
|
// return &LRValue{month, lm.right}, nil
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|
||||||
|
//maxday := countDays(now.Year(), month)
|
||||||
|
|
||||||
|
// 1-12月
|
||||||
|
// left := lm.left
|
||||||
|
// if left < 0 {
|
||||||
|
// left = maxday + left
|
||||||
|
// }
|
||||||
|
|
||||||
|
// right := lm.right
|
||||||
|
// if right < 0 {
|
||||||
|
// right = maxday + right
|
||||||
|
// }
|
||||||
|
|
||||||
|
// if left <= right {
|
||||||
|
// switch {
|
||||||
|
// case day < lm.left:
|
||||||
|
// return &LRValue{lm.left, lm.right}, nil
|
||||||
|
// case month > lm.right:
|
||||||
|
// return nil, nil
|
||||||
|
// case lm.left <= month && month <= lm.right:
|
||||||
|
// return &LRValue{month, lm.right}, nil
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
func (cron *Crontab) TimeUp() bool {
|
func (cron *Crontab) TimeUp() bool {
|
||||||
|
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -100,6 +193,7 @@ func createTimePointer(min string, llimit, rlimit int) []TimePointer {
|
|||||||
|
|
||||||
if left == "*" {
|
if left == "*" {
|
||||||
tp.left = llimit
|
tp.left = llimit
|
||||||
|
tp.isAll = true
|
||||||
} else {
|
} else {
|
||||||
ileft, err := strconv.Atoi(strings.Replace(left, "^", "-", -1))
|
ileft, err := strconv.Atoi(strings.Replace(left, "^", "-", -1))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -110,6 +204,7 @@ func createTimePointer(min string, llimit, rlimit int) []TimePointer {
|
|||||||
|
|
||||||
if rigth == "*" {
|
if rigth == "*" {
|
||||||
tp.right = rlimit
|
tp.right = rlimit
|
||||||
|
tp.isAll = true
|
||||||
} else {
|
} else {
|
||||||
iright, err := strconv.Atoi(strings.Replace(rigth, "^", "-", -1))
|
iright, err := strconv.Atoi(strings.Replace(rigth, "^", "-", -1))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -136,4 +231,7 @@ func TestParseCrontab(t *testing.T) {
|
|||||||
crontab := "0-5/2,7-30/3,30,35,40-^1 * * * *" //(秒) 分 时 号(每月的多少号, 要注意月可可能性) 星期几(每个星期的) /每 ,列表 -范围
|
crontab := "0-5/2,7-30/3,30,35,40-^1 * * * *" //(秒) 分 时 号(每月的多少号, 要注意月可可能性) 星期几(每个星期的) /每 ,列表 -范围
|
||||||
|
|
||||||
t.Error(NewCrontab(crontab))
|
t.Error(NewCrontab(crontab))
|
||||||
|
|
||||||
|
ty := CreateTrieYear(2018)
|
||||||
|
log.Println(ty.Month[2].MaxDay)
|
||||||
}
|
}
|
||||||
|
73
trie_test.go
Normal file
73
trie_test.go
Normal file
@ -0,0 +1,73 @@
|
|||||||
|
package curl2info
|
||||||
|
|
||||||
|
import "time"
|
||||||
|
|
||||||
|
type SecondNode int
|
||||||
|
|
||||||
|
type MinuteNode struct {
|
||||||
|
IsOK bool
|
||||||
|
// Second [60]SecondNode
|
||||||
|
}
|
||||||
|
|
||||||
|
type HourNode struct {
|
||||||
|
IsOK bool
|
||||||
|
Minute [60]MinuteNode
|
||||||
|
}
|
||||||
|
|
||||||
|
type DayNode struct {
|
||||||
|
IsOK bool
|
||||||
|
|
||||||
|
Week time.Weekday
|
||||||
|
Hour [24]HourNode
|
||||||
|
}
|
||||||
|
|
||||||
|
type MonthNode struct {
|
||||||
|
IsOK bool
|
||||||
|
MaxDay int
|
||||||
|
Day []DayNode
|
||||||
|
}
|
||||||
|
|
||||||
|
type TrieYear struct {
|
||||||
|
Year int
|
||||||
|
Month [13]MonthNode
|
||||||
|
}
|
||||||
|
|
||||||
|
func CreateTrieYear(year int) *TrieYear {
|
||||||
|
|
||||||
|
ty := TrieYear{}
|
||||||
|
ty.Month[0].IsOK = false
|
||||||
|
|
||||||
|
for i := 1; i <= 12; i++ {
|
||||||
|
cur := time.Date(year, time.Month(i), 1, 0, 0, 0, 0, time.Local)
|
||||||
|
|
||||||
|
month := &ty.Month[i]
|
||||||
|
month.IsOK = false
|
||||||
|
|
||||||
|
last := cur.AddDate(0, 1, -1).Day()
|
||||||
|
month.MaxDay = last
|
||||||
|
|
||||||
|
for begin := 0; begin < last; begin++ {
|
||||||
|
month.Day = append(month.Day, DayNode{})
|
||||||
|
|
||||||
|
week := cur.AddDate(0, 0, begin).Weekday()
|
||||||
|
|
||||||
|
day := &month.Day[begin]
|
||||||
|
day.IsOK = false
|
||||||
|
day.Week = week
|
||||||
|
|
||||||
|
for j := 0; j < 24; j++ {
|
||||||
|
hour := &day.Hour[j]
|
||||||
|
hour.IsOK = false
|
||||||
|
|
||||||
|
for k := 0; k < 60; k++ {
|
||||||
|
min := &hour.Minute[k]
|
||||||
|
min.IsOK = false
|
||||||
|
// 还差一个秒
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
return &ty
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user