570144e |
Jakob Wakeling |
2023-12-15 23:28:06 |
0
|
// Copyright (C) 2023, Jakob Wakeling |
570144e |
Jakob Wakeling |
2023-12-15 23:28:06 |
1
|
// All rights reserved. |
570144e |
Jakob Wakeling |
2023-12-15 23:28:06 |
2
|
|
570144e |
Jakob Wakeling |
2023-12-15 23:28:06 |
3
|
package cron |
570144e |
Jakob Wakeling |
2023-12-15 23:28:06 |
4
|
|
570144e |
Jakob Wakeling |
2023-12-15 23:28:06 |
5
|
import ( |
594ec41 |
Jakob Wakeling |
2023-12-29 15:18:46 |
6
|
"fmt" |
570144e |
Jakob Wakeling |
2023-12-15 23:28:06 |
7
|
"time" |
594ec41 |
Jakob Wakeling |
2023-12-29 15:18:46 |
8
|
|
594ec41 |
Jakob Wakeling |
2023-12-29 15:18:46 |
9
|
"github.com/Jamozed/Goit/src/util" |
570144e |
Jakob Wakeling |
2023-12-15 23:28:06 |
10
|
) |
570144e |
Jakob Wakeling |
2023-12-15 23:28:06 |
11
|
|
570144e |
Jakob Wakeling |
2023-12-15 23:28:06 |
12
|
type Schedule struct{ Month, Day, Weekday, Hour, Minute, Second int64 } |
570144e |
Jakob Wakeling |
2023-12-15 23:28:06 |
13
|
|
2d1243e |
Jakob Wakeling |
2023-12-16 00:03:03 |
14
|
var ( |
2d1243e |
Jakob Wakeling |
2023-12-16 00:03:03 |
15
|
Immediate = Schedule{-1, -1, -1, -1, -1, -1} |
2d1243e |
Jakob Wakeling |
2023-12-16 00:03:03 |
16
|
Yearly = Schedule{1, 1, -1, 0, 0, 0} |
2d1243e |
Jakob Wakeling |
2023-12-16 00:03:03 |
17
|
Monthly = Schedule{-1, 1, -1, 0, 0, 0} |
2d1243e |
Jakob Wakeling |
2023-12-16 00:03:03 |
18
|
Weekly = Schedule{-1, -1, 1, 0, 0, 0} |
2d1243e |
Jakob Wakeling |
2023-12-16 00:03:03 |
19
|
Daily = Schedule{-1, -1, -1, 0, 0, 0} |
2d1243e |
Jakob Wakeling |
2023-12-16 00:03:03 |
20
|
Hourly = Schedule{-1, -1, -1, -1, 0, 0} |
2d1243e |
Jakob Wakeling |
2023-12-16 00:03:03 |
21
|
Minutely = Schedule{-1, -1, -1, -1, -1, 0} |
2d1243e |
Jakob Wakeling |
2023-12-16 00:03:03 |
22
|
) |
570144e |
Jakob Wakeling |
2023-12-15 23:28:06 |
23
|
|
570144e |
Jakob Wakeling |
2023-12-15 23:28:06 |
24
|
func (s Schedule) Next(t time.Time) time.Time { |
2d1243e |
Jakob Wakeling |
2023-12-16 00:03:03 |
25
|
t = t.Add(1 * time.Second) |
2d1243e |
Jakob Wakeling |
2023-12-16 00:03:03 |
26
|
|
570144e |
Jakob Wakeling |
2023-12-15 23:28:06 |
27
|
added := false |
570144e |
Jakob Wakeling |
2023-12-15 23:28:06 |
28
|
|
570144e |
Jakob Wakeling |
2023-12-15 23:28:06 |
29
|
wrap: |
570144e |
Jakob Wakeling |
2023-12-15 23:28:06 |
30
|
for s.Month != -1 && int64(t.Month()) != s.Month { |
570144e |
Jakob Wakeling |
2023-12-15 23:28:06 |
31
|
if !added { |
570144e |
Jakob Wakeling |
2023-12-15 23:28:06 |
32
|
t = time.Date(t.Year(), t.Month(), 1, 0, 0, 0, 0, t.Location()) |
570144e |
Jakob Wakeling |
2023-12-15 23:28:06 |
33
|
added = true |
570144e |
Jakob Wakeling |
2023-12-15 23:28:06 |
34
|
} |
570144e |
Jakob Wakeling |
2023-12-15 23:28:06 |
35
|
|
570144e |
Jakob Wakeling |
2023-12-15 23:28:06 |
36
|
t = t.AddDate(0, 1, 0) |
570144e |
Jakob Wakeling |
2023-12-15 23:28:06 |
37
|
|
570144e |
Jakob Wakeling |
2023-12-15 23:28:06 |
38
|
if t.Month() == time.January { |
570144e |
Jakob Wakeling |
2023-12-15 23:28:06 |
39
|
goto wrap |
570144e |
Jakob Wakeling |
2023-12-15 23:28:06 |
40
|
} |
570144e |
Jakob Wakeling |
2023-12-15 23:28:06 |
41
|
} |
570144e |
Jakob Wakeling |
2023-12-15 23:28:06 |
42
|
|
570144e |
Jakob Wakeling |
2023-12-15 23:28:06 |
43
|
for !((s.Day == -1 || int64(t.Day()) == s.Day) && (s.Weekday == -1 || int64(t.Weekday()) == s.Weekday)) { |
570144e |
Jakob Wakeling |
2023-12-15 23:28:06 |
44
|
if !added { |
570144e |
Jakob Wakeling |
2023-12-15 23:28:06 |
45
|
t = time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, t.Location()) |
570144e |
Jakob Wakeling |
2023-12-15 23:28:06 |
46
|
added = true |
570144e |
Jakob Wakeling |
2023-12-15 23:28:06 |
47
|
} |
570144e |
Jakob Wakeling |
2023-12-15 23:28:06 |
48
|
|
570144e |
Jakob Wakeling |
2023-12-15 23:28:06 |
49
|
t = t.AddDate(0, 0, 1) |
570144e |
Jakob Wakeling |
2023-12-15 23:28:06 |
50
|
|
570144e |
Jakob Wakeling |
2023-12-15 23:28:06 |
51
|
if t.Day() == 1 { |
570144e |
Jakob Wakeling |
2023-12-15 23:28:06 |
52
|
goto wrap |
570144e |
Jakob Wakeling |
2023-12-15 23:28:06 |
53
|
} |
570144e |
Jakob Wakeling |
2023-12-15 23:28:06 |
54
|
} |
570144e |
Jakob Wakeling |
2023-12-15 23:28:06 |
55
|
|
570144e |
Jakob Wakeling |
2023-12-15 23:28:06 |
56
|
for s.Hour != -1 && int64(t.Hour()) != s.Hour { |
570144e |
Jakob Wakeling |
2023-12-15 23:28:06 |
57
|
if !added { |
570144e |
Jakob Wakeling |
2023-12-15 23:28:06 |
58
|
t = time.Date(t.Year(), t.Month(), t.Day(), t.Hour(), 0, 0, 0, t.Location()) |
570144e |
Jakob Wakeling |
2023-12-15 23:28:06 |
59
|
added = true |
570144e |
Jakob Wakeling |
2023-12-15 23:28:06 |
60
|
} |
570144e |
Jakob Wakeling |
2023-12-15 23:28:06 |
61
|
|
570144e |
Jakob Wakeling |
2023-12-15 23:28:06 |
62
|
t = t.Add(1 * time.Hour) |
570144e |
Jakob Wakeling |
2023-12-15 23:28:06 |
63
|
|
570144e |
Jakob Wakeling |
2023-12-15 23:28:06 |
64
|
if t.Hour() == 0 { |
570144e |
Jakob Wakeling |
2023-12-15 23:28:06 |
65
|
goto wrap |
570144e |
Jakob Wakeling |
2023-12-15 23:28:06 |
66
|
} |
570144e |
Jakob Wakeling |
2023-12-15 23:28:06 |
67
|
} |
570144e |
Jakob Wakeling |
2023-12-15 23:28:06 |
68
|
|
570144e |
Jakob Wakeling |
2023-12-15 23:28:06 |
69
|
for s.Minute != -1 && int64(t.Minute()) != s.Minute { |
570144e |
Jakob Wakeling |
2023-12-15 23:28:06 |
70
|
if !added { |
570144e |
Jakob Wakeling |
2023-12-15 23:28:06 |
71
|
t = t.Truncate(time.Minute) |
570144e |
Jakob Wakeling |
2023-12-15 23:28:06 |
72
|
added = true |
570144e |
Jakob Wakeling |
2023-12-15 23:28:06 |
73
|
} |
570144e |
Jakob Wakeling |
2023-12-15 23:28:06 |
74
|
|
570144e |
Jakob Wakeling |
2023-12-15 23:28:06 |
75
|
t = t.Add(1 * time.Minute) |
570144e |
Jakob Wakeling |
2023-12-15 23:28:06 |
76
|
|
570144e |
Jakob Wakeling |
2023-12-15 23:28:06 |
77
|
if t.Minute() == 0 { |
570144e |
Jakob Wakeling |
2023-12-15 23:28:06 |
78
|
goto wrap |
570144e |
Jakob Wakeling |
2023-12-15 23:28:06 |
79
|
} |
570144e |
Jakob Wakeling |
2023-12-15 23:28:06 |
80
|
} |
570144e |
Jakob Wakeling |
2023-12-15 23:28:06 |
81
|
|
570144e |
Jakob Wakeling |
2023-12-15 23:28:06 |
82
|
for s.Second != -1 && int64(t.Second()) != s.Second { |
570144e |
Jakob Wakeling |
2023-12-15 23:28:06 |
83
|
if !added { |
570144e |
Jakob Wakeling |
2023-12-15 23:28:06 |
84
|
t = t.Truncate(time.Second) |
570144e |
Jakob Wakeling |
2023-12-15 23:28:06 |
85
|
added = true |
570144e |
Jakob Wakeling |
2023-12-15 23:28:06 |
86
|
} |
570144e |
Jakob Wakeling |
2023-12-15 23:28:06 |
87
|
|
570144e |
Jakob Wakeling |
2023-12-15 23:28:06 |
88
|
t = t.Add(1 * time.Second) |
570144e |
Jakob Wakeling |
2023-12-15 23:28:06 |
89
|
|
570144e |
Jakob Wakeling |
2023-12-15 23:28:06 |
90
|
if t.Second() == 0 { |
570144e |
Jakob Wakeling |
2023-12-15 23:28:06 |
91
|
goto wrap |
570144e |
Jakob Wakeling |
2023-12-15 23:28:06 |
92
|
} |
570144e |
Jakob Wakeling |
2023-12-15 23:28:06 |
93
|
} |
570144e |
Jakob Wakeling |
2023-12-15 23:28:06 |
94
|
|
570144e |
Jakob Wakeling |
2023-12-15 23:28:06 |
95
|
return t |
570144e |
Jakob Wakeling |
2023-12-15 23:28:06 |
96
|
} |
570144e |
Jakob Wakeling |
2023-12-15 23:28:06 |
97
|
|
570144e |
Jakob Wakeling |
2023-12-15 23:28:06 |
98
|
func (s Schedule) IsImmediate() bool { |
570144e |
Jakob Wakeling |
2023-12-15 23:28:06 |
99
|
return s == Immediate |
570144e |
Jakob Wakeling |
2023-12-15 23:28:06 |
100
|
} |
594ec41 |
Jakob Wakeling |
2023-12-29 15:18:46 |
101
|
|
594ec41 |
Jakob Wakeling |
2023-12-29 15:18:46 |
102
|
func (s Schedule) String() string { |
594ec41 |
Jakob Wakeling |
2023-12-29 15:18:46 |
103
|
if s.IsImmediate() { |
594ec41 |
Jakob Wakeling |
2023-12-29 15:18:46 |
104
|
return "immediate" |
594ec41 |
Jakob Wakeling |
2023-12-29 15:18:46 |
105
|
} |
594ec41 |
Jakob Wakeling |
2023-12-29 15:18:46 |
106
|
|
594ec41 |
Jakob Wakeling |
2023-12-29 15:18:46 |
107
|
return fmt.Sprintf( |
594ec41 |
Jakob Wakeling |
2023-12-29 15:18:46 |
108
|
"%s %s %s %s %s %s", |
594ec41 |
Jakob Wakeling |
2023-12-29 15:18:46 |
109
|
util.If(s.Month == -1, "*", fmt.Sprint(s.Month)), |
594ec41 |
Jakob Wakeling |
2023-12-29 15:18:46 |
110
|
util.If(s.Day == -1, "*", fmt.Sprint(s.Day)), |
594ec41 |
Jakob Wakeling |
2023-12-29 15:18:46 |
111
|
util.If(s.Weekday == -1, "*", fmt.Sprint(s.Weekday)), |
594ec41 |
Jakob Wakeling |
2023-12-29 15:18:46 |
112
|
util.If(s.Hour == -1, "*", fmt.Sprint(s.Hour)), |
594ec41 |
Jakob Wakeling |
2023-12-29 15:18:46 |
113
|
util.If(s.Minute == -1, "*", fmt.Sprint(s.Minute)), |
594ec41 |
Jakob Wakeling |
2023-12-29 15:18:46 |
114
|
util.If(s.Second == -1, "*", fmt.Sprint(s.Second)), |
594ec41 |
Jakob Wakeling |
2023-12-29 15:18:46 |
115
|
) |
594ec41 |
Jakob Wakeling |
2023-12-29 15:18:46 |
116
|
} |
|
|
|
117
|
|