dqm4977 2017-03-21 23:25
浏览 621
已采纳

Golang:获取其他时区的等效时间

I'm trying to compare two times from different timezones, and see whether one is before the other. How would I do this in golang?

Note: Basically I would like sfTime.Before(nyTime) == true, but my example below would have sfTime.Before(nyTime) == false. Suggestions on how to make this happen would be great.


For example, in this code...

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
", sfTime.Equal(nyTime))

This prints:

Are these times equal? true

Playground link here.

Unintuitively, even if you set them to be the same timezone, this only changes the timezone, but not the HH:mm value.

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
", sfTime.Equal(nyTime))
fmt.Printf("The New York Time: %v
", nyTime)

Prints

Are these times still equal? true

The New York Time: 2017-03-01 12:00:00 +0000 UTC

Playground link.

  • 写回答

3条回答 默认 最新

  • doufen5175 2017-03-22 01:47
    关注

    Don't use the Go Playground for time calculations. It runs in a sandbox with a fake time:

    About the 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.

    Also, all times in the Go Playground use the UTC time zone. The Go Playground doesn't use the IANA Time Zone Database.

    For example, for this program,

    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
    ", sfTime.Equal(nyTime))
    }
    

    Output from the Go Playground is:

    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
    

    For the correct output, run the program using the Go gc or gccgo compiler:

    $ 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
    

    Using the Go gc or gccgo compiler then 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
    ", sfTime.Before(nyTime))
    }
    

    Output:

    $ 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
    

    The Go time package comparison methods (Equal, Before, and After) compare UTC values.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?