Q:
將時間線區分為 3 個區塊,找出 now() 最接近區塊
僅往左側找,例: now() = 22,介於 13~23,應回傳 13 而不是 23
A:
##### pair programming result
package main
import (
"fmt"
"time"
)
func main() {
t, _ := time.Parse("2006-01-02 15:04:05", "2020-10-21 5:50:17")
h := t.Hour()
loop:
for {
switch h {
case 23, 13, 6:
fmt.Println(t)
break loop
}
t = t.Add(-1 * time.Hour)
h = t.Hour()
}
fmt.Println("done")
}