相关疑难解决方法(0)

为什么 Go 的 time.Parse() 不解析时区标识符?

考虑以下代码:

package main

import (
    "time"
    "fmt"
)

const (
    format = "2006 01 02 15:04 MST"

    date = "2018 08 01 12:00 EDT"
)

func main() {
    aloc, _ := time.LoadLocation("America/New_York")
    eloc, _ := time.LoadLocation("Europe/Berlin")
    tn, _ := time.Parse(format, date)
    tl, _ := time.ParseInLocation(format, date, aloc)

    fmt.Println(tn) // Says +0000 despite EDT being -0400
    fmt.Println(tn.In(eloc)) // Expect 18:00, but get 14:00
    fmt.Println(tl) // Correctly -0400
    fmt.Println(tl.In(eloc)) // Correctly 18:00
}
Run Code Online (Sandbox Code Playgroud)

您也可以在Go Playground上试用。

当我运行它时,我得到了这个结果(在我自己的系统上和通过 Playground):

2018-08-01 12:00:00 +0000 …
Run Code Online (Sandbox Code Playgroud)

go

7
推荐指数
1
解决办法
3066
查看次数

标签 统计

go ×1