Go: time.Format: how to understand meaning of '2006-01-02' layout?

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

Go: time.Format: how to understand meaning of '2006-01-02' layout?

问题

给定一个时间变量,我想打印年、月和日。
根据文档,似乎可以使用任何布局。例如,我看不出布局2006-01-02、2006-10-10、1999-02-02之间的区别。

然而,只有布局2006-01-02返回我期望的结果。
我在这里尝试了不同的布局:go playground: testing layouts

英文:

Given a time variable, I want to print year, month, and day.
From the documentation, it seems that any layout can be used. For example, I don't see difference between layouts 2006-01-02, 2006-10-10, 1999-02-02.

However, only layout 2006-01-02 returns what I expect.
Where can I find documentation on the meanings of '2006', '01', '02' in the layout?

I played here with different layouts: go playground: testing layouts

答案1

得分: 23

为了跟进Jack的信息,请查看详细的示例

// Parse函数和Format方法使用的布局字符串
// 通过示例展示了参考时间的表示方式。
// 我们强调必须展示参考时间的格式,
// 而不是用户选择的时间。因此,每个布局字符串都是
// 时间戳的表示,
// Jan 2 15:04:05 2006 MST
// 记住这个值的简单方法是,按照上面的元素排列,
// 它包含以下值:
//	  1 2  3  4  5    6  -7

这个参考时间可以帮助我们澄清Go语言是否应该将01-02-17解析为2017年1月2日还是2月1日。

英文:

to follow up on Jack's info, see the detailed examples:

// The layout string used by the Parse function and Format method
// shows by example how the reference time should be represented.
// We stress that one must show how the reference time is formatted,
// not a time of the user's choosing. Thus each layout string is a
// representation of the time stamp,
//	Jan 2 15:04:05 2006 MST
// An easy way to remember this value is that it holds, when presented
// in this order, the values (lined up with the elements above):
//	  1 2  3  4  5    6  -7

this reference time allows us to clarify whether go should parse 01-02-17 as jan 2 2017 or feb 1

答案2

得分: 21

Go的时间格式与其他语言不同且独特。与其使用传统的日期格式进行打印不同,Go使用参考日期20060102150405,这看起来毫无意义,但实际上有其原因,因为它对应于Posix日期命令中的1 2 3 4 5 6

Mon Jan 2 15:04:05 -0700 MST 2006
0   1   2  3  4  5              6

在Go中,布局字符串如下:

Jan 2 15:04:05 2006 MST
1   2  3  4  5    6  -7
英文:

Go’s time formatting unique and different than what you would do in other languages. Instead of having a conventional format to print the date, Go uses the reference date 20060102150405 which seems meaningless but actually has a reason, as it’s 1 2 3 4 5 6 in the Posix date command:

Mon Jan 2 15:04:05 -0700 MST 2006
0   1   2  3  4  5              6

In Go layout string is:

Jan 2 15:04:05 2006 MST
1   2  3  4  5    6  -7

答案3

得分: 15

Mon Jan 2 15:04:05 -0700 MST 2006 是参考时间,这意味着布局需要使用该确切日期。这里有更多信息,但基本上通过为日期时间的每个部分使用唯一值,它能够自动确定每个部分(年、月等)的位置。

已更正的 Go Playground

英文:

Mon Jan 2 15:04:05 -0700 MST 2006 is the reference time, which means the layout needs to use that exact date. There's more information here, but basically by using unique values for each part of a datetime it's able to tell where each part (year, month, etc) actually is automatically.

Corrected go playground

huangapple
  • 本文由 发表于 2017年2月14日 11:21:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/42217308.html
匿名

发表评论

匿名网友

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

确定