Parsing time string in Go

huangapple go评论97阅读模式
英文:

Parsing time string in Go

问题

Spark Rest API返回的时间格式是2016-10-28T16:56:50.497GMT

我正在尝试在Go中解析它,但没有成功。
我一直得到0001-01-01 00:00:00 +0000 UTC,但time.Parse函数没有抛出任何错误:

我的函数如下所示:

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
}

我已经尝试查阅文档并创建了自定义的布局,但我无法确定如何处理字符串中的GMT部分。

英文:

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

得分: 2

你试图将该时间解析为time.RFC3339Nano格式,但实际上它不是这种格式。

请使用格式"2006-01-02T15:04:05.999MST"

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

英文:

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

huangapple
  • 本文由 发表于 2016年10月31日 21:05:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/40342780.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定