From 8452db76d1dec602050f2187a8704ffcb7c54db1 Mon Sep 17 00:00:00 2001 From: huangsimin Date: Mon, 3 Dec 2018 19:22:43 +0800 Subject: [PATCH] save trie year tree --- crontab_test.go | 98 +++++++++++++++++++++++++++++++++++++++++++++++++ trie_test.go | 73 ++++++++++++++++++++++++++++++++++++ 2 files changed, 171 insertions(+) create mode 100644 trie_test.go diff --git a/crontab_test.go b/crontab_test.go index 2a37a93..bf6e2a5 100644 --- a/crontab_test.go +++ b/crontab_test.go @@ -3,18 +3,25 @@ package curl2info import ( "errors" "fmt" + "log" "regexp" "strconv" "strings" "testing" + "time" "github.com/davecgh/go-spew/spew" ) +// type LRValue struct { +// left, right int +// } + type TimePointer struct { left, right int leftlimit, rightlimit int per int + isAll bool } func (tp *TimePointer) String() string { @@ -29,6 +36,7 @@ type Crontab struct { Week []TimePointer logMissedTime bool + plans []time.Time } func NewCrontab(crontab string) *Crontab { @@ -41,7 +49,92 @@ func (cron *Crontab) SetMissedLog(missedlog bool) { 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 { + return false } @@ -100,6 +193,7 @@ func createTimePointer(min string, llimit, rlimit int) []TimePointer { if left == "*" { tp.left = llimit + tp.isAll = true } else { ileft, err := strconv.Atoi(strings.Replace(left, "^", "-", -1)) if err != nil { @@ -110,6 +204,7 @@ func createTimePointer(min string, llimit, rlimit int) []TimePointer { if rigth == "*" { tp.right = rlimit + tp.isAll = true } else { iright, err := strconv.Atoi(strings.Replace(rigth, "^", "-", -1)) if err != nil { @@ -136,4 +231,7 @@ func TestParseCrontab(t *testing.T) { crontab := "0-5/2,7-30/3,30,35,40-^1 * * * *" //(秒) 分 时 号(每月的多少号, 要注意月可可能性) 星期几(每个星期的) /每 ,列表 -范围 t.Error(NewCrontab(crontab)) + + ty := CreateTrieYear(2018) + log.Println(ty.Month[2].MaxDay) } diff --git a/trie_test.go b/trie_test.go new file mode 100644 index 0000000..5fa3438 --- /dev/null +++ b/trie_test.go @@ -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 +}