在Golang中解析时间

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

Parsing time in Golang

问题

我正在尝试根据名为dateAdded的输入创建一个Time结构体。我的代码如下:

dateAdded := "November 25, 2016"
layout := "September 9, 2016"
t, err := time.Parse(layout, dateAdded)
if err != nil {
	fmt.Println(err)
} else {
	fmt.Println(t)
}

我得到了以下错误:将时间“November 25, 2016”解析为“September 9, 2016”时出错:无法将“November 25, 2016”解析为“September 9,”。

我猜测Parse函数无法解析每种日期格式,但我想知道通常读取日期并将其解析为时间对象的方法是什么。

英文:

I'm trying to create a Time struct based on some input called dateAdded. My code is like this:

dateAdded := "November 25, 2016"
layout := "September 9, 2016"
t, err := time.Parse(layout, dateAdded)
if err != nil {
	fmt.Println(err)
} else {
	fmt.Println(t)
}

And I get the following error: parsing time "November 25, 2016" as "September 9, 2016": cannot parse "November 25, 2016" as "September 9, "

I assume the Parse function cannot parse every layout, but I'm curios what's the usual way of reading dates and parse them into time objects.

答案1

得分: 5

如果你没有使用时间模块预定义的常量布局之一,那么自定义的布局必须由精确的时间戳Mon Jan 2 15:04:05 -0700 MST 2006组成。请注意,每个元素都是唯一的,因此每个数字标识符都可以自动解析。基本上,它是1(月份),2(日期),3(小时),4(分钟),5(秒),6(年份),7(时区)等。

最好使用库中预定义的标准布局之一:

const (
        ANSIC       = "Mon Jan _2 15:04:05 2006"
        UnixDate    = "Mon Jan _2 15:04:05 MST 2006"
        RubyDate    = "Mon Jan 02 15:04:05 -0700 2006"
        RFC822      = "02 Jan 06 15:04 MST"
        RFC822Z     = "02 Jan 06 15:04 -0700" // 带有数字时区的RFC822
        RFC850      = "Monday, 02-Jan-06 15:04:05 MST"
        RFC1123     = "Mon, 02 Jan 2006 15:04:05 MST"
        RFC1123Z    = "Mon, 02 Jan 2006 15:04:05 -0700" // 带有数字时区的RFC1123
        RFC3339     = "2006-01-02T15:04:05Z07:00"
        RFC3339Nano = "2006-01-02T15:04:05.999999999Z07:00"
        Kitchen     = "3:04PM"
        // 方便的时间戳。
        Stamp      = "Jan _2 15:04:05"
        StampMilli = "Jan _2 15:04:05.000"
        StampMicro = "Jan _2 15:04:05.000000"
        StampNano  = "Jan _2 15:04:05.000000000"
)
英文:

The layout, if you're not using one of the pre-included constant layout that comes with the time module, must be formed from the exact timestamp Mon Jan 2 15:04:05 -0700 MST 2006. Notice that each element of it is unique, so each numeric identifier can be automatically parsed. it's basically 1 (month), 2 (day), 3 (hour), 4 (minute), 5 (second), 6 (year), 7 (time zone) etc.

It's better to use one of the pre-defined standard layouts that are included with the library:

const (
        ANSIC       = "Mon Jan _2 15:04:05 2006"
        UnixDate    = "Mon Jan _2 15:04:05 MST 2006"
        RubyDate    = "Mon Jan 02 15:04:05 -0700 2006"
        RFC822      = "02 Jan 06 15:04 MST"
        RFC822Z     = "02 Jan 06 15:04 -0700" // RFC822 with numeric zone
        RFC850      = "Monday, 02-Jan-06 15:04:05 MST"
        RFC1123     = "Mon, 02 Jan 2006 15:04:05 MST"
        RFC1123Z    = "Mon, 02 Jan 2006 15:04:05 -0700" // RFC1123 with numeric zone
        RFC3339     = "2006-01-02T15:04:05Z07:00"
        RFC3339Nano = "2006-01-02T15:04:05.999999999Z07:00"
        Kitchen     = "3:04PM"
        // Handy time stamps.
        Stamp      = "Jan _2 15:04:05"
        StampMilli = "Jan _2 15:04:05.000"
        StampMicro = "Jan _2 15:04:05.000000"
        StampNano  = "Jan _2 15:04:05.000000000"
)

答案2

得分: 1

你的日期格式是错误的。应该是"January 2, 2006"。正如规范所说:

该格式定义了参考时间的格式,参考时间被定义为Mon Jan 2 15:04:05 -0700 MST 2006,如果它是值的话,将如何解释。

英文:

Your layout date is wrong. Should be "January 2, 2006". As the specs say:

> The layout defines the format by showing how the reference time,
> defined to be Mon Jan 2 15:04:05 -0700 MST 2006 would be interpreted
> if it were the value

答案3

得分: 1

你应该将其视为你提供给time.Provide的示例。它应该是文档中描述的具体值。

Parse函数解析一个格式化的字符串并返回它表示的时间值。布局定义了格式,通过展示参考时间的形式来定义,参考时间被定义为

Mon Jan 2 15:04:05 -0700 MST 2006

这是一个带有正确变体的示例。

英文:

You should treat it as an example which you provide to time.Provide. And it should be of concrete value described in the documentation.

> Parse parses a formatted string and returns the time value it represents. The layout defines the format by showing how the reference time, defined to be
>
> Mon Jan 2 15:04:05 -0700 MST 2006

A playground with correct variant.

huangapple
  • 本文由 发表于 2016年11月28日 16:53:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/40840316.html
匿名

发表评论

匿名网友

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

确定