隐藏无关的函数
This commit is contained in:
parent
a4b4afdffd
commit
60f5aaad3f
29
crontab.go
29
crontab.go
@ -25,16 +25,16 @@ func (tp *timePointer) String() string {
|
||||
|
||||
// Crontab 的string解析
|
||||
type Crontab struct {
|
||||
Min []timePointer
|
||||
Hour []timePointer
|
||||
Day []timePointer
|
||||
Month []timePointer
|
||||
Week []timePointer
|
||||
min []timePointer
|
||||
hour []timePointer
|
||||
day []timePointer
|
||||
month []timePointer
|
||||
week []timePointer
|
||||
|
||||
WillPlans []time.Time
|
||||
SkipPlans []time.Time
|
||||
|
||||
YearPlan *TrieYear
|
||||
YearPlan *trieYear
|
||||
}
|
||||
|
||||
// NewCrontab create 一个crontab
|
||||
@ -45,7 +45,7 @@ func NewCrontab(crontab string) *Crontab {
|
||||
}
|
||||
|
||||
func (cron *Crontab) String() string {
|
||||
return fmt.Sprintf("min:%s\nhour:%s\nday:%s\nmonth:%s\nweek:%s\n", spew.Sdump(cron.Min), spew.Sdump(cron.Hour), spew.Sdump(cron.Day), spew.Sdump(cron.Month), spew.Sdump(cron.Week))
|
||||
return fmt.Sprintf("min:%s\nhour:%s\nday:%s\nmonth:%s\nweek:%s\n", spew.Sdump(cron.min), spew.Sdump(cron.hour), spew.Sdump(cron.day), spew.Sdump(cron.month), spew.Sdump(cron.week))
|
||||
}
|
||||
|
||||
// FromString 解析crontab 的 表达式
|
||||
@ -55,12 +55,14 @@ func (cron *Crontab) FromString(crontab string) error {
|
||||
matches := regexp.MustCompile("[^ ]+").FindAllString(crontab, -1)
|
||||
mlen := len(matches)
|
||||
switch mlen {
|
||||
case 1:
|
||||
|
||||
case 5:
|
||||
cron.Min = createTimePointer(matches[0], 0, 59, true)
|
||||
cron.Hour = createTimePointer(matches[1], 0, 23, true)
|
||||
cron.Day = createTimePointer(matches[2], 1, 31, false)
|
||||
cron.Month = createTimePointer(matches[3], 1, 12, true)
|
||||
cron.Week = createTimePointer(matches[4], 0, 6, true)
|
||||
cron.min = createTimePointer(matches[0], 0, 59, true)
|
||||
cron.hour = createTimePointer(matches[1], 0, 23, true)
|
||||
cron.day = createTimePointer(matches[2], 1, 31, false)
|
||||
cron.month = createTimePointer(matches[3], 1, 12, true)
|
||||
cron.week = createTimePointer(matches[4], 0, 6, true)
|
||||
|
||||
cron.CreateYearPlan()
|
||||
default:
|
||||
@ -77,8 +79,9 @@ func (cron *Crontab) CreateYearPlan() {
|
||||
}
|
||||
|
||||
// TimeUp 是否时间快到
|
||||
func (cron *Crontab) TimeUp(now time.Time) bool {
|
||||
func (cron *Crontab) TimeUp() bool {
|
||||
|
||||
now := time.Now()
|
||||
maxlen := 1000
|
||||
createlen := 500
|
||||
|
||||
|
@ -26,7 +26,7 @@ func TestParseCrontab(t *testing.T) {
|
||||
}
|
||||
|
||||
cron.CreateYearPlan()
|
||||
if !cron.TimeUp(time.Now()) {
|
||||
if !cron.TimeUp() {
|
||||
t.Error("timeup error")
|
||||
}
|
||||
|
||||
|
78
trie_year.go
78
trie_year.go
@ -4,43 +4,39 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
var defaultTime = time.Now()
|
||||
|
||||
type SecondNode int
|
||||
|
||||
type MinuteNode struct {
|
||||
type minuteNode struct {
|
||||
}
|
||||
|
||||
func NewMinuteNode() *MinuteNode {
|
||||
return &MinuteNode{}
|
||||
func newMinuteNode() *minuteNode {
|
||||
return &minuteNode{}
|
||||
}
|
||||
|
||||
type HourNode struct {
|
||||
Minute [60]*MinuteNode
|
||||
type hourNode struct {
|
||||
Minute [60]*minuteNode
|
||||
}
|
||||
|
||||
func (hour *HourNode) CreateMinute(nminute int) {
|
||||
min := &MinuteNode{}
|
||||
func (hour *hourNode) CreateMinute(nminute int) {
|
||||
min := &minuteNode{}
|
||||
hour.Minute[nminute] = min
|
||||
}
|
||||
|
||||
func newHourNode() *HourNode {
|
||||
return &HourNode{}
|
||||
func newHourNode() *hourNode {
|
||||
return &hourNode{}
|
||||
}
|
||||
|
||||
type DayNode struct {
|
||||
type dayNode struct {
|
||||
IsClear bool
|
||||
Week time.Weekday
|
||||
Hour [24]*HourNode
|
||||
Hour [24]*hourNode
|
||||
}
|
||||
|
||||
func (day *DayNode) CreateHour(nhour int) {
|
||||
hour := &HourNode{}
|
||||
func (day *dayNode) CreateHour(nhour int) {
|
||||
hour := &hourNode{}
|
||||
day.Hour[nhour] = hour
|
||||
}
|
||||
|
||||
func newDayNode(curday *time.Time) *DayNode {
|
||||
day := &DayNode{}
|
||||
func newDayNode(curday *time.Time) *dayNode {
|
||||
day := &dayNode{}
|
||||
|
||||
week := curday.Weekday()
|
||||
day.Week = week
|
||||
@ -49,37 +45,37 @@ func newDayNode(curday *time.Time) *DayNode {
|
||||
return day
|
||||
}
|
||||
|
||||
type MonthNode struct {
|
||||
type monthNode struct {
|
||||
MaxDay int
|
||||
First *time.Time
|
||||
Day [32]*DayNode
|
||||
Day [32]*dayNode
|
||||
}
|
||||
|
||||
func (month *MonthNode) CreateDay(nday int) {
|
||||
func (month *monthNode) CreateDay(nday int) {
|
||||
day := month.First.AddDate(0, 0, nday-1)
|
||||
month.Day[nday] = newDayNode(&day)
|
||||
}
|
||||
|
||||
func newMonthNode(year, month int) *MonthNode {
|
||||
Month := &MonthNode{}
|
||||
func newMonthNode(year, month int) *monthNode {
|
||||
Month := &monthNode{}
|
||||
First := time.Date(year, time.Month(month), 1, 0, 0, 0, 0, time.Local)
|
||||
Month.First = &First
|
||||
Month.MaxDay = Month.First.AddDate(0, 1, -1).Day()
|
||||
return Month
|
||||
}
|
||||
|
||||
type TrieYear struct {
|
||||
type trieYear struct {
|
||||
Year int
|
||||
Month [13]*MonthNode
|
||||
Month [13]*monthNode
|
||||
}
|
||||
|
||||
// CheckYear 跨年判断
|
||||
func (ty *TrieYear) CheckYear() bool {
|
||||
func (ty *trieYear) CheckYear() bool {
|
||||
year := time.Now().Year()
|
||||
return ty.Year == year
|
||||
}
|
||||
|
||||
func (ty *TrieYear) clearHour() {
|
||||
func (ty *trieYear) clearHour() {
|
||||
|
||||
for _, month := range ty.Month {
|
||||
if month != nil {
|
||||
@ -98,14 +94,14 @@ func (ty *TrieYear) clearHour() {
|
||||
|
||||
}
|
||||
|
||||
func newTrieYear() *TrieYear {
|
||||
ty := TrieYear{}
|
||||
func newTrieYear() *trieYear {
|
||||
ty := trieYear{}
|
||||
ty.Year = time.Now().Year()
|
||||
return &ty
|
||||
}
|
||||
|
||||
// GetPlanTime 获取计划表
|
||||
func (ty *TrieYear) GetPlanTime(cron *Crontab, aftertime time.Time, count uint) []time.Time {
|
||||
func (ty *trieYear) GetPlanTime(cron *Crontab, aftertime time.Time, count uint) []time.Time {
|
||||
|
||||
now := aftertime
|
||||
nmonth := int(now.Month())
|
||||
@ -184,9 +180,9 @@ func (ty *TrieYear) GetPlanTime(cron *Crontab, aftertime time.Time, count uint)
|
||||
}
|
||||
|
||||
// FromCrontab 从Crontab生成树
|
||||
func (ty *TrieYear) FromCrontab(cron *Crontab) {
|
||||
func (ty *trieYear) FromCrontab(cron *Crontab) {
|
||||
// 月的填充
|
||||
for _, month := range cron.Month {
|
||||
for _, month := range cron.month {
|
||||
|
||||
left := month.left
|
||||
right := month.right
|
||||
@ -200,9 +196,9 @@ func (ty *TrieYear) FromCrontab(cron *Crontab) {
|
||||
}
|
||||
}
|
||||
|
||||
func filterDay(cron *Crontab, curday *DayNode) bool {
|
||||
func filterDay(cron *Crontab, curday *dayNode) bool {
|
||||
|
||||
for _, w := range cron.Week {
|
||||
for _, w := range cron.week {
|
||||
if w.isAll {
|
||||
return false
|
||||
}
|
||||
@ -216,8 +212,8 @@ func filterDay(cron *Crontab, curday *DayNode) bool {
|
||||
return true
|
||||
}
|
||||
|
||||
func insertDay(cron *Crontab, curMonth *MonthNode) {
|
||||
for _, day := range cron.Day {
|
||||
func insertDay(cron *Crontab, curMonth *monthNode) {
|
||||
for _, day := range cron.day {
|
||||
|
||||
left := day.left
|
||||
if left < 0 {
|
||||
@ -243,10 +239,10 @@ func insertDay(cron *Crontab, curMonth *MonthNode) {
|
||||
}
|
||||
}
|
||||
|
||||
func insertHour(cron *Crontab, curDay *DayNode) {
|
||||
func insertHour(cron *Crontab, curDay *dayNode) {
|
||||
curDay.IsClear = false
|
||||
// 时的填充
|
||||
for _, hour := range cron.Hour {
|
||||
for _, hour := range cron.hour {
|
||||
|
||||
left := hour.left
|
||||
right := hour.right
|
||||
@ -259,8 +255,8 @@ func insertHour(cron *Crontab, curDay *DayNode) {
|
||||
}
|
||||
}
|
||||
|
||||
func insertMinute(cron *Crontab, curHour *HourNode) {
|
||||
for _, min := range cron.Min {
|
||||
func insertMinute(cron *Crontab, curHour *hourNode) {
|
||||
for _, min := range cron.min {
|
||||
|
||||
left := min.left
|
||||
right := min.right
|
||||
|
Loading…
x
Reference in New Issue
Block a user