Go language time.Parse() for timestamps with no timezone

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

Go language time.Parse() for timestamps with no timezone

问题

在Go语言中,我正在尝试使用time包中的time.Parse()函数将字符串时间戳转换为Time对象。我知道Go语言使用一种不常见的方式来表示时间格式,即通过提供一个示例来显示它们的参考时间(Mon Jan 2 15:04:05 -0700 MST 2006)在你的格式中的显示方式。然而,我仍然遇到了错误。以下是一个时间戳的示例:

Tue Nov 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时区的问题。

我得到的错误是:

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"

我在这里做错了什么?我是否误解了如何使用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

得分: 8

你的格式字符串应该是:

Mon Jan 02 15:04:05 MST 2006

playground

也就是说,使用MST表示时区,使用15表示小时,正如你链接中的Parse函数所记录的那样。

英文:

Your 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.

答案2

得分: 0

在这种情况下,你可以使用time.UnixDate

package main

import (
   "fmt"
   "time"
)

func main() {
   t, e := time.Parse(time.UnixDate, "Tue Nov 27 09:09:29 UTC 2012")
   if e != nil {
      panic(e)
   }
   fmt.Println(t)
}

https://golang.org/pkg/time#UnixDate

英文:

In this case, you can use time.UnixDate:

package main

import (
   "fmt"
   "time"
)

func main() {
   t, e := time.Parse(time.UnixDate, "Tue Nov 27 09:09:29 UTC 2012")
   if e != nil {
      panic(e)
   }
   fmt.Println(t)
}

https://golang.org/pkg/time#UnixDate

huangapple
  • 本文由 发表于 2013年12月18日 16:08:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/20652747.html
匿名

发表评论

匿名网友

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

确定