sie*_*sta 0 time timezone datetime parsing go
在Go中,在尝试将字符串转换time.Time为时,使用时间包的Parse方法不会返回预期结果.似乎问题在于时区.我想更改为ISO 8601以及UTC中的日期和时间.
package main
import (
    "fmt"
    "time"
)
func main() {
    const longForm = "2013-05-13T18:41:34.848Z"
    //even this is not working
    //const longForm = "2013-05-13 18:41:34.848 -0700 PDT"
    t, _ := time.Parse(longForm, "2013-05-13 18:41:34.848 -0700 PDT")
    fmt.Println(t)
    //outputs 0001-01-01 00:00:00 +0000 UTC
}
提前致谢!
您的格式字符串longForm不正确.你会知道,如果你没有忽略返回的错误.引用文档:
这些是在Time.Format和Time.Parse中使用的预定义布局.布局中使用的参考时间是:
Mon Jan 2 15:04:05 MST 2006
这是Unix时间1136239445.由于MST是GMT-0700,参考时间可以被认为是
01/02 03:04:05PM '06 -0700
要定义自己的格式,请记下参考时间看起来像格式化的方式; 例如,查看ANSIC,StampMicro或Kitchen等常量的值.
package main
import (
        "fmt"
        "log"
        "time"
)
func main() {
        const longForm = "2006-01-02 15:04:05 -0700"
        t, err := time.Parse(longForm, "2013-05-13 18:41:34.848 -0700")
        if err != nil {
                log.Fatal(err)
        }
        fmt.Println(t)
}
输出:
2013-05-13 01:41:34.848 +0000 UTC