英文:
time.Unix(t, offset) gives same time?
问题
尝试使用以下Go代码获取连续的时间戳:
package main
import "fmt"
import "time"
func main() {
ts := int64(1500000000) // 起始时间戳
for i := int64(0); i<1e6; i = i + 1000 { // 逐渐增加1e3
t := time.Unix(ts, i) // 获取起始时间戳加上i的时间
fmt.Printf("%d %02d:%02d\n", ts+i, t.Hour(), t.Minute())
}
}
输出结果如下:
....
1500995000 04:40
1500996000 04:40
1500997000 04:40
1500998000 04:40
1500999000 04:40
请问,这里有什么问题?为什么小时和分钟不变?(都是04:40)
go version go1.15.6 linux/amd64
英文:
Trying to obtain successive timestamps with below Go code
package main
import "fmt"
import "time"
func main () {
ts := int64(1500000000) // start
for i := int64(0); i<1e6; i = i + 1000 { // successively add 1e3
t := time.Unix(ts, i) // Get start + i
fmt.Printf("%d %02d:%02d\n", ts+i, t.Hour(), t.Minute())
}
}
, output remains like
....
1500995000 04:40
1500996000 04:40
1500997000 04:40
1500998000 04:40
1500999000 04:40
Please, what is the problem here ? Why the hour:minute doesn't vary ? (04:40)
go version go1.15.6 linux/amd64
答案1
得分: 3
你正在更改纳秒并打印分钟...
func Unix(sec int64, nsec int64) Time
请查看这个链接:
https://play.golang.org/p/_Ywn9S5Khch
package main
import "fmt"
import "time"
func main() {
ts := int64(1500000000) // 开始时间
for i := int64(0); i<1e6; i = i + 1000 { // 逐渐增加1e3
t := time.Unix(ts, i) // 获取开始时间 + i
fmt.Printf("%d %02d:%02d ---- %2d\n", ts+i, t.Hour(), t.Minute(), t.UnixNano())
}
}
英文:
You are changing the nanoseconds and printing minutes...
https://pkg.go.dev/time@go1.16.7#Unix
func Unix(sec int64, nsec int64) Time
Check this:
https://play.golang.org/p/_Ywn9S5Khch
package main
import "fmt"
import "time"
func main () {
ts := int64(1500000000) // start
for i := int64(0); i<1e6; i = i + 1000 { // successively add 1e3
t := time.Unix(ts, i) // Get start + i
fmt.Printf("%d %02d:%02d ---- %2d\n", ts+i, t.Hour(), t.Minute(), t.UnixNano())
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论