修复定时器当启动时间在周五时,计算下个周五的bug
parent
7937facbe3
commit
19a6453dd4
|
@ -11,7 +11,13 @@ func RemainingTimeUntilNextFriday17() time.Duration {
|
|||
now := time.Now()
|
||||
|
||||
// 计算下一个周五的时间
|
||||
nextFriday := GetNextWeekday(now, time.Friday)
|
||||
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())
|
||||
|
@ -21,13 +27,3 @@ func RemainingTimeUntilNextFriday17() time.Duration {
|
|||
|
||||
return duration
|
||||
}
|
||||
|
||||
// 获取下一个指定星期几的时间
|
||||
|
||||
func GetNextWeekday(t time.Time, weekday time.Weekday) time.Time {
|
||||
daysUntil := (int(weekday) - int(t.Weekday()) + 7) % 7
|
||||
if daysUntil == 0 {
|
||||
daysUntil = 7
|
||||
}
|
||||
return t.AddDate(0, 0, daysUntil)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue