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

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

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?