例子
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() {
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
}