- update cron

master
李光春 1 year ago
parent 35fe97d37b
commit c21b6948cc

@ -1,89 +1,123 @@
package rocron
import "fmt"
import (
"fmt"
)
var (
EarlyInTheMorningSpec = "0 0 0 * * *"
EarlyInTheMorningExplain = "每天0点执行一次"
YearlySpec = "0 0 0 1 1 *"
YearlyExplain = "每年一次1 月 1 日午夜执行一次"
AnnuallySpec = "0 0 0 1 1 *"
AnnuallyExplain = "每年一次1 月 1 日午夜执行一次"
MonthlySpec = "0 0 0 1 * *"
MonthlyExplain = "每月执行,午夜,月初执行一次"
WeeklySpec = "0 0 0 * * 0"
WeeklyExplain = "每周执行,周六和周日之间的午夜执行一次"
DailySpec = "0 0 0 * * *"
DailyExplain = "每天午夜执行一次"
MidnightSpec = "0 0 0 * * *"
MidnightExplain = "每天午夜执行一次"
HourlySpec = "0 0 * * * *"
HourlyExplain = "每小时运行,每小时开始执行一次"
)
// GetSecondsSpec 秒
func GetSecondsSpec(seconds int) string {
if seconds <= 0 {
seconds = 0
}
if seconds > 60 {
seconds = 60
}
return fmt.Sprintf("*/%d * * * * *", seconds)
/*
*
second (0 - 60)
min (0 - 59)
hour (0 - 23)
day of month (1 - 31)
month (1 - 12)
day of week (0 - 6) (0 to 6 are Sunday to
Saturday)
* * * * * *
*/
const (
secondsSpec = "*/%d * * * * *"
minutesSpec = "%d */%d * * * *"
hoursSpec = "%d %d */%d * * *"
daySpec = "%d %d %d */%d * *"
daysSpec = "%d %d %d %s * *"
)
// GetSecondSpec 每隔秒
func GetSecondSpec(seconds int) string {
seconds = filterSeconds(seconds)
return fmt.Sprintf(secondsSpec, seconds)
}
// GetSecondsExplain 秒
func GetSecondsExplain(seconds int) string {
if seconds <= 0 {
seconds = 0
}
if seconds > 60 {
seconds = 60
}
// GetSecondExplain 每隔秒
func GetSecondExplain(seconds int) string {
seconds = filterSeconds(seconds)
return fmt.Sprintf("每隔%d秒执行一次", seconds)
}
// GetMinutesSpec 分
func GetMinutesSpec(minutes, seconds int) string {
if minutes <= 0 {
minutes = 0
}
if seconds > 59 {
seconds = 59
}
return fmt.Sprintf("%d */%d * * * *", seconds, minutes)
// GetMinuteSpec 每隔分钟
func GetMinuteSpec(minutes, seconds int) string {
minutes = filterMinutes(minutes)
seconds = filterSeconds(seconds)
return fmt.Sprintf(minutesSpec, seconds, minutes)
}
// GetMinutesExplain 分
func GetMinutesExplain(minutes, seconds int) string {
if minutes <= 0 {
minutes = 0
}
// GetMinuteExplain 每隔分钟
func GetMinuteExplain(minutes, seconds int) string {
minutes = filterMinutes(minutes)
seconds = filterSeconds(seconds)
if seconds <= 0 {
return fmt.Sprintf("每隔%d分钟执行一次", minutes)
}
if seconds > 59 {
seconds = 59
}
return fmt.Sprintf("每隔%d分钟%d秒执行一次", minutes, seconds)
}
// GetHoursSpec 小时
func GetHoursSpec(hours, minutes, seconds int) string {
if hours <= 0 {
hours = 0
}
if minutes > 59 {
minutes = 59
}
if seconds > 59 {
seconds = 59
}
return fmt.Sprintf("%d %d */%d * * *", seconds, minutes, hours)
// GetHourSpec 每隔小时
func GetHourSpec(hours, minutes, seconds int) string {
hours = filterHours(hours)
minutes = filterMinutes(minutes)
seconds = filterSeconds(seconds)
return fmt.Sprintf(hoursSpec, seconds, minutes, hours)
}
// GetHoursExplain 小时
func GetHoursExplain(hours, minutes, seconds int) string {
if hours <= 0 {
hours = 0
}
// GetHourExplain 每隔小时
func GetHourExplain(hours, minutes, seconds int) string {
hours = filterHours(hours)
minutes = filterMinutes(minutes)
seconds = filterSeconds(seconds)
if seconds <= 0 {
if minutes <= 0 {
return fmt.Sprintf("每隔%d小时执行一次", hours)
}
if minutes > 59 {
minutes = 59
}
return fmt.Sprintf("每隔%d小时%d分执行一次", hours, minutes)
}
if seconds > 59 {
seconds = 59
}
return fmt.Sprintf("每隔%d小时%d分%d秒执行一次", hours, minutes, seconds)
}
// GetDaySpec 天
func GetDaySpec(day, hours, minutes, seconds int) string {
day = filterDays(day)
hours = filterHours(hours)
minutes = filterMinutes(minutes)
seconds = filterSeconds(seconds)
return fmt.Sprintf(daySpec, seconds, minutes, hours, day)
//days := concatenateStrings(day)
//return fmt.Sprintf(daysSpec, seconds, minutes, hours, days)
}
// GetDayExplain 天
func GetDayExplain(days, hours, minutes, seconds int) string {
days = filterDays(days)
hours = filterHours(hours)
minutes = filterMinutes(minutes)
seconds = filterSeconds(seconds)
if seconds <= 0 {
if minutes <= 0 {
if hours <= 0 {
return fmt.Sprintf("每隔%d天执行一次", days)
}
return fmt.Sprintf("每隔%d天%d时执行一次", days, hours)
}
return fmt.Sprintf("每隔%d天%d时%d分执行一次", days, hours, minutes)
}
return fmt.Sprintf("每隔%d天%d时%d分%d秒执行一次", days, hours, minutes, seconds)
}

@ -0,0 +1,41 @@
package rocron
func filterSeconds(seconds int) int {
if seconds <= 0 {
return 0
}
if seconds > 60 {
return 60
}
return seconds
}
func filterMinutes(minutes int) int {
if minutes <= 0 {
return 0
}
if minutes > 59 {
return 59
}
return minutes
}
func filterHours(hours int) int {
if hours <= 0 {
return 0
}
if hours > 23 {
return 23
}
return hours
}
func filterDays(days int) int {
if days <= 0 {
return 1
}
if days > 31 {
return 31
}
return days
}

@ -0,0 +1,32 @@
package rocron
import "fmt"
func concatenateStrings(day int) string {
var result string
if day > 1 {
result += "1"
}
for i := 1; i <= 31; i++ {
if isDivisibleBy(i, day) {
if len(result) > 0 {
result += ","
}
result += fmt.Sprintf("%d", i)
}
}
return result
}
func isDivisibleBy(num, divisor int) bool {
return num%divisor == 0
}
func removeFirstTwoChars(str string) string {
if len(str) > 2 {
return str[2:]
}
return ""
}
Loading…
Cancel
Save