英文:
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"
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论