go_rain_dtu/time_utils.go
2025-04-24 14:44:30 +08:00

48 lines
1.1 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package main
import (
"time"
)
// 获取5分钟为间隔的下一个查询时间
func getNextQueryTime() time.Time {
now := time.Now()
minute := now.Minute()
nextMinute := ((minute / 5) + 1) * 5
var nextHour int
if nextMinute >= 60 {
nextMinute = 0
nextHour = now.Hour() + 1
if nextHour >= 24 {
nextHour = 0
}
} else {
nextHour = now.Hour()
}
return time.Date(now.Year(), now.Month(), now.Day(), nextHour, nextMinute, 0, 0, now.Location())
}
// 获取下一个整点时间
func getNextHourTime() time.Time {
now := time.Now()
nextHour := now.Hour() + 1
if nextHour >= 24 {
nextHour = 0
// 如果是23点则下一小时是次日的0点
if now.Hour() == 23 {
tomorrow := now.AddDate(0, 0, 1)
return time.Date(tomorrow.Year(), tomorrow.Month(), tomorrow.Day(), 0, 0, 0, 0, now.Location())
}
}
return time.Date(now.Year(), now.Month(), now.Day(), nextHour, 0, 0, 0, now.Location())
}
// 判断是否接近整点前后2分钟内
func isNearHour() bool {
now := time.Now()
minute := now.Minute()
return minute >= 58 || minute <= 2
}