go的定时日期之循环日期设置处理,并转换为时间戳

例子

package main
import (
	"fmt"
	"time"
)
func main() {
        var tt int64 = 1512230400
        ts := time.Unix(tt, 0)
        tst := ts.AddDate(0, 1, 0) //年、月、日(可以为正、负)
        fmt.Println(ts)
	fmt.Println(tst)
}

实例

package main

import (
	"fmt"
	"github.com/gogf/gf/v2/os/gtime"
	"log"
	"strconv"
	"strings"
)
import "time"

func main() {
	//简单周期任务测试
	//go heartBeat()
	//time.Sleep(time.Second * 3)

	//AnalysisCycleTime("1,13,17:27") //参数说明:选项,每月几号,时:分
	//AnalysisCycleTime("2,3,17:27") //参数说明:选项,周几,时:分
	AnalysisCycleTime("3,3,17:27") //参数说明:选项,隔几天,时:分
}

func heartBeat() {
	for range time.Tick(time.Second * 1) {
		fmt.Println("Foo")
	}
}

func AnalysisCycleTime(planTime string) int {
	//当前时间
	currentTime := gtime.Now()
	log.Println("currentTime: ", currentTime)
	//当前时间戳
	currentTimestamp := currentTime.Unix()
	//当前的时、分
	currentHour := currentTime.Hour()
	currentMinute := currentTime.Minute()
	currentTempMinute := currentHour*60 + currentMinute
	//处理计划中的周期类型、间隔时间、时:分
	planTimeList := strings.Split(planTime, ",")
	if len(planTimeList) != 3 {
		log.Println("周期运行时间格式错误: ", planTime)
		return 0
	}
	signStr, dayStr, timeStr := planTimeList[0], planTimeList[1], planTimeList[2]
	log.Println("signStr, dayUnit8, timeStr:", signStr, dayStr, timeStr)
	sign, err := strconv.Atoi(signStr)
	if err != nil {
		log.Println("strconv method -- str to int err: ", err)
		return 0
	}
	//处理计划中的时:分
	timeList := strings.Split(timeStr, ":")
	if len(timeList) != 2 {
		log.Println("周期运行时间格式错误: ", planTime)
		return 0
	}
	//获取计划中的时:分
	hour, minute := timeList[0], timeList[1]
	nextHour, err := strconv.Atoi(hour)
	if err != nil {
		log.Println("strconv method -- str to int err: ", err)
		return 0
	}
	nextMinute, err := strconv.Atoi(minute)
	if err != nil {
		log.Println("strconv method -- str to int err: ", err)
		return 0
	}
	nextTempMinute := nextHour*60 + nextMinute
	differenceMinute := nextTempMinute - currentTempMinute
	month := 0
	nextTimestamp := int(currentTimestamp)
	if sign == 1 {
		nextDay, _ := strconv.Atoi(dayStr)
		currentDay := currentTime.Day()
		differenceDay := nextDay - currentDay
		if differenceDay >= 0 {
			if differenceMinute >= 0 {
				month = 0
			} else {
				month = 1
			}
		} else {
			month = 1
		}
		tempTimestamp := currentTime.AddDate(0, month, differenceDay).Unix()
		differenceSecond := differenceMinute * 60
		nextTimestamp = int(tempTimestamp) + differenceSecond
	} else if sign == 2 {
		nextWeekday, _ := strconv.Atoi(dayStr)
		currentWeekdayStr := currentTime.Weekday().String()
		currentWeekday := WeekNumber(currentWeekdayStr)
		weekday := 0
		if nextWeekday >= currentWeekday {
			if differenceMinute >= 0 {
				weekday = nextWeekday - currentWeekday
			} else {
				weekday = nextWeekday + 7 - currentWeekday
			}

		} else {
			weekday = nextWeekday + 7 - currentWeekday
		}
		tempTimestamp := currentTime.AddDate(0, month, weekday).Unix()
		differenceSecond := differenceMinute * 60
		nextTimestamp = int(tempTimestamp) + differenceSecond
	} else if sign == 3 {
		perDay := 0
		if differenceMinute < 0 {
			perDay, _ = strconv.Atoi(dayStr)
		}
		tempTimestamp := currentTime.AddDate(0, month, perDay).Unix()
		differenceSecond := differenceMinute * 60
		nextTimestamp = int(tempTimestamp) + differenceSecond
	}
	log.Println("下次执行的时间戳: ", nextTimestamp)
	log.Println("当前时间: ", time.Now().String())
	return nextTimestamp
}

func WeekNumber(k string) int {
	enum := map[string]int{
		"Monday":    1,
		"Tuesday":   2,
		"Wednesday": 3,
		"Thursday":  4,
		"Friday":    5,
		"Saturday":  6,
		"Sunday":    7,
	}
	value, ok := enum[k]
	if ok {
		return value
	}
	return 0
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值