這是一個建立於 的文章,其中的資訊可能已經有所發展或是發生改變。
func (s *SpecSchedule) Next(t time.Time) time.Time {// General approach:// For Month, Day, Hour, Minute, Second:// Check if the time value matches. If yes, continue to the next field.// If the field doesn't match the schedule, then increment the field until it matches.// While incrementing the field, a wrap-around brings it back to the beginning// of the field list (since it is necessary to re-verify previous field// values)// 下一次每次取最近1s的時間t = t.Add(1*time.Second - time.Duration(t.Nanosecond())*time.Nanosecond)// This flag indicates whether a field has been incremented.added := false// If no time is found within five years, return zero.yearLimit := t.Year() + 5WRAP:if t.Year() > yearLimit {return time.Time{}}// Find the first applicable month.// If it's this month, then do nothing. //通過指數法逼近值,並通過從sec到min到hour到day到mon到year的不斷逼近for 1<<uint(t.Month())&s.Month == 0 {// If we have to add a month, reset the other parts to 0.if !added {added = true// Otherwise, set the date at the beginning (since the current time is irrelevant).t = time.Date(t.Year(), t.Month(), 1, 0, 0, 0, 0, t.Location())}t = t.AddDate(0, 1, 0)// Wrapped around.if t.Month() == time.January {goto WRAP}}// Now get a day in that month.for !dayMatches(s, t) {if !added {added = truet = time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, t.Location())}t = t.AddDate(0, 0, 1)if t.Day() == 1 {goto WRAP}}for 1<<uint(t.Hour())&s.Hour == 0 {if !added {added = truet = time.Date(t.Year(), t.Month(), t.Day(), t.Hour(), 0, 0, 0, t.Location())}t = t.Add(1 * time.Hour)if t.Hour() == 0 {goto WRAP}}for 1<<uint(t.Minute())&s.Minute == 0 {if !added {added = truet = t.Truncate(time.Minute)}t = t.Add(1 * time.Minute)if t.Minute() == 0 {goto WRAP}}for 1<<uint(t.Second())&s.Second == 0 {if !added {added = truet = t.Truncate(time.Second)}t = t.Add(1 * time.Second)if t.Second() == 0 {goto WRAP}}return t}
代碼裡面最經典的還是這種位元運算:
1<<uint(t.Month())&s.Month//轉化為計算公式為: 2^t.Month & s.Month //比如t.Month = 3, s.Month = 4 2^t.Month = 8 > s.Month 並且此時與操作為0//說明此時t.Month與s.Month差距不多,當t.Month=1的時候 2^t.Month = 2 此時與操作結果不為0//當t.Month=2的時候 也是如此//總結就是 2^t.Month > s.Month的時候操作結果為0,需要輪轉到比它小很多的位置,//然後Month的更新再依賴於下面的day的更新