duanchi5078 2013-12-18 08:08
浏览 155
已采纳

使用语言time.Parse()获取没有时区的时间戳

In Go I'm trying to use the time.Parse() function from the time package to convert a string timestamp into a Time object. I know Go has an uncommon way of representing the time format your timestamps are in by providing it with an example of how their reference time (Mon Jan 2 15:04:05 -0700 MST 2006) would be displayed in your format. I'm still having issues with errors however. Here is an example of one of my timestamps:

Tue Nov 27 09:09:29 UTC 2012

Here is what the call I'm making looks like:

    t, err := time.Parse("Mon Jan 02 22:04:05 UTC 2006", "Tue Nov 27 09:09:29 UTC 2012")

So basically what I've done here is try and match the formatting for day name/month name/day number, the hour/minute/second format, the string literal "UTC" and the year format. Note that I've increased the hours field of the Go reference format by 7 (from 15 to 22) to account for the fact that their timestamp is in a negative 7 timezone and all my timestamps are in a UTC timezone.

The error I get is:

parsing time "Tue Nov 27 09:09:29 UTC 2012" as "Mon Jan 02 22:04:05 UTC 2006": cannot parse ":09:29 UTC 2012" as "2"

What am I doing wrong here? Am I misinterpreting how to use time.Parse() or is my use case not supported for some reason?

  • 写回答

1条回答 默认 最新

  • dongxin5429 2013-12-18 08:12
    关注

    You're format string should be:

    Mon Jan 02 15:04:05 MST 2006
    

    playground

    That is, use MST for the timezone, and 15 for the hour, as documented in your linked Parse function.

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

报告相同问题?

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图