1 回答

TA貢獻1712條經驗 獲得超3個贊
如果你想向下舍入時間,請使用time.Truncate:
t := time.Now()
fmt.Println(t.Truncate(5*time.Second))
將此應用到您的需求:
// returns in UTC timezone - regardless of input timezone
func getWindow(t time.Time, windowLength time.Duration, step int64) time.Time {
? ? return t.UTC().Truncate(windowLength).Add(
? ? ? ? time.Duration(step) * windowLength,
? ? )
}
// time.Now() may be in any timezone - but UTC will be returned (see above)
func GetWindow(windowLength time.Duration, step int64) time.Time {
? ? return getWindow(time.Now(), windowLength, step)
}
具有替代時區等的演示:https ://play.golang.org/p/gqsT4LvWdDi
添加到您的測試代碼:https://play.golang.org/p/UDPlsR6IGPw
添加回答
舉報