代码来自chatgpt
实现如果在在一个秒级时间戳上加上23h59m59s
先把时间戳转换成 time 对象,然后利用 Add 函数在该对象上添加23h59m59s,最后获取时间对象的时间戳
package main
import (
"fmt"
"time"
)
func main() {
// Suppose you have a timestamp in seconds
timestampSecs := int64(1627468293)
// Convert the timestamp to the time.Time
t := time.Unix(timestampSecs, 0)
// Add 23h59m59s
t = t.Add(23*time.Hour + 59*time.Minute + 59*time.Second)
// If you need to convert it back to a timestamp in seconds:
timestampSecsUpdated := t.Unix()
fmt.Printf("Updated timestamp in seconds: %v\n", timestampSecsUpdated)
}
注意
最好不要单纯的给 时间戳加上整数来修改时间戳,比如像下面这样,不仅看着费劲,也容易出bug
timestampSecs := int64(1627468293)
targetStamp := timestampSecs + time.Duration(3600 * time.Hour*23) + time.Duration(60 *time.Minute * 59) + time.Duration(60 * time.Second)