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

使用语言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?

图片转代码服务由CSDN问答提供 功能建议

在Go语言中,我尝试使用 time 包中的 time.Parse() 函数可将字符串时间戳转换为 Time 对象。 我知道Go提供了一个示例,说明参考时间( Mon Jan 2 15:04:05 -0700 MST 2006 )将是一种不常见的方式来表示时间戳的时间格式 以您的格式显示。 我仍然有错误的问题。 这是我的一个时间戳示例:

 星期二11月27日09:09:29 UTC 2012 
   
 
 

这就是我正在拨打的电话:

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

所以我在这里所做的基本上是尝试匹配日期名称/月份名称/ 日期,小时/分钟/秒格式,字符串文字“ UTC”和年份格式。 请注意,我已经将Go参考格式的小时字段增加了 7 (从 15 22 ) 考虑到它们的时间戳位于负7时区,而我所有的时间戳均位于UTC时区。

我得到的错误是: < pre> 将时间“ Tue Nov 27 09:09:29 UTC 2012”解析为“ Mon Jan 02 22:04:05 UTC 2006”:无法将“:09:29 UTC 2012”解析为“ 2” < / code>

我在这里做错了什么? 我是在误解如何使用 time.Parse()还是出于某些原因不支持我的用例?

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

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.

    打赏 评论

相关推荐 更多相似问题