我正在尝试比较不同时区的两个时间,看看一个时间是否在另一个时间之前。我将如何在 Go 中执行此操作?
注意:基本上我想要sfTime.Before(nyTime) == true
,但我下面的例子会有sfTime.Before(nyTime) ==错误
。关于如何实现这一目标的建议会很棒。
例如,在这段代码中...
layout := "2006-01-02 15:04 MST"
sfTime, _ := time.Parse(layout, "2017-03-01 12:00 PDT")
nyTime, _ := time.Parse(layout, "2017-03-01 12:00 EDT")
fmt.Printf("Are these times equal? %v\n", sfTime.Equal(nyTime))
这打印:
Are these times equal? true
不直观的是,即使您将它们设置为同一时区,这也只会更改时区,而不会更改 HH:mm
值。
layout := "2006-01-02 15:04 MST"
sfTime, _ := time.Parse(layout, "2017-03-01 12:00 PDT")
nyTime, _ := time.Parse(layout, "2017-03-01 12:00 EDT")
// Set timezone to UTC
utcLocation, _ := time.LoadLocation("UTC")
sfTime = sfTime.In(utcLocation)
nyTime = nyTime.In(utcLocation)
// Timezones should not be equal, but they are
fmt.Printf("Are these times still equal? %v\n", sfTime.Equal(nyTime))
fmt.Printf("The New York Time: %v\n", nyTime)
打印
Are these times still equal? true
The New York Time: 2017-03-01 12:00:00 +0000 UTC
最佳答案
不要使用围棋 Playground 计算时间。它在带有假时间的沙箱中运行:
The Go Playground is a web service that runs on golang.org's servers. The service receives a Go program, compiles, links, and runs the program inside a sandbox, then returns the output.
There are limitations to the programs that can be run in the playground.
In the playground the time begins at 2009-11-10 23:00:00 UTC (determining the significance of this date is an exercise for the reader). This makes it easier to cache programs by giving them deterministic output.
此外,Go Playground 中的所有时间都使用 UTC 时区。 Go Playground 不使用 IANA Time Zone Database .
例如,对于这个程序,
package main
import (
"fmt"
"time"
)
func main() {
layout := "2006-01-02 15:04 MST"
sfTime, err := time.Parse(layout, "2017-03-01 12:00 PDT")
if err != nil {
fmt.Println(err)
}
fmt.Println(sfTime, sfTime.UTC())
nyTime, err := time.Parse(layout, "2017-03-01 12:00 EDT")
if err != nil {
fmt.Println(err)
}
fmt.Println(nyTime, nyTime.UTC())
fmt.Printf("Are these times equal? %v\n", sfTime.Equal(nyTime))
}
Go Playground 的输出是:
2017-03-01 12:00:00 +0000 PDT 2017-03-01 12:00:00 +0000 UTC
2017-03-01 12:00:00 +0000 EDT 2017-03-01 12:00:00 +0000 UTC
Are these times equal? true
要获得正确的输出,请使用 Go gc 或 gccgo 编译器运行程序:
$ go run equal.go
2017-03-01 12:00:00 +0000 PDT 2017-03-01 12:00:00 +0000 UTC
2017-03-01 11:00:00 -0500 EST 2017-03-01 16:00:00 +0000 UTC
Are these times equal? false
然后使用 Go gc 或 gccgo 编译器sfTime.Before(nyTime) == true
:
package main
import (
"fmt"
"time"
)
func main() {
layout := "2006-01-02 15:04 MST"
sfTime, err := time.Parse(layout, "2017-03-01 12:00 PDT")
if err != nil {
fmt.Println(err)
}
fmt.Println(sfTime, sfTime.UTC())
nyTime, err := time.Parse(layout, "2017-03-01 12:00 EDT")
if err != nil {
fmt.Println(err)
}
fmt.Println(nyTime, nyTime.UTC())
fmt.Printf("Is the SF time before the NY time? %v\n", sfTime.Before(nyTime))
}
输出:
$ go run before.go
2017-03-01 12:00:00 +0000 PDT 2017-03-01 12:00:00 +0000 UTC
2017-03-01 11:00:00 -0500 EST 2017-03-01 16:00:00 +0000 UTC
Is the SF time before the NY time? true
Go time
包比较方法(Equal
、Before
和 After
)比较 UTC 值。
关于go - 如何在另一个时区获得等效时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42940065/