您的当前位置:首页正文

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

2024-11-29 来源:个人技术集锦

例子

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
}

显示全文