ai-week/tools/checktime.go

30 lines
728 B
Go
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package tools
import (
"time"
)
// 计算时间差值
func RemainingTimeUntilNextFriday17() time.Duration {
// 获取当前时间
now := time.Now()
// 计算下一个周五的时间
nextFriday := now.AddDate(0, 0, (int(time.Friday)-int(now.Weekday())+7)%7)
// 如果今天是周五则直接返回距离今天17点的时间
if now.Weekday() == time.Friday {
today17 := time.Date(now.Year(), now.Month(), now.Day(), 17, 0, 0, 0, now.Location())
return today17.Sub(now)
}
// 构造下一个周五 17 点的时间
nextFriday17 := time.Date(nextFriday.Year(), nextFriday.Month(), nextFriday.Day(), 17, 0, 0, 0, nextFriday.Location())
// 计算时间差
duration := nextFriday17.Sub(now)
return duration
}