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