dongxieli3839 2016-10-31 13:05
浏览 104
已采纳

在Go中解析时间字符串

The Spark Rest API returns time in this format: 2016-10-28T16:56:50.497GMT.

I'm trying to parse that in Go, without any luck. I keep getting 0001-01-01 00:00:00 +0000 UTC, but the time.Parse function doesn't throw any errors:

My function looks like this:

func getTime(timeStamp string) (t time.Time, e error) {
    t, e = time.Parse(
        time.RFC3339Nano,
        timeStamp)
    if e != nil {
        fmt.Errorf("Error parsing timestring", e)
    }
    return t, e
}

I've tried going through the documentation and created my custom layout, but I can't figure out how I should treat that GMT part of the string

  • 写回答

1条回答 默认 最新

  • dpo60833 2016-10-31 13:11
    关注

    You're trying to parse that time as time.RFC3339Nano, which it is not.

    Use the format "2006-01-02T15:04:05.999MST"

    https://play.golang.org/p/TYHnvclxEn

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?