Understanding golang date formatting for time package

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

Understanding golang date formatting for time package

问题

所以我的函数表现良好。

func Today()(result string){
    current_time := time.Now().Local()
    result =  current_time.Format("01/02/2006")
    return
}

打印出 MM/DD/YYYY,我认为如果在日期位置上有一个大于12的值,以明确表示是MM/DD/YYYY,会更易读,所以我将其更改为以下内容:

func Today()(result string){
    current_time := time.Now().Local()
    result =  current_time.Format("01/23/2004")
    return
}

令我失望的是,这导致了错误的结果。打印出 MM/DDHH/DD0MM

意识到我的错误后,我发现格式是由参考时间定义的...

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

我想知道是否还有其他情况下使用这个时间作为日期时间格式参考的实例,以及这个参考时间是否有一个昵称(比如null island)?

英文:

So I have the function performing well.

func Today()(result string){
    current_time := time.Now().Local()
    result =  current_time.Format("01/02/2006")
    return
}

Prints MM/DD/YYYY And I thought that it would be more readable if I had a value greater than 12 in the days position to make it clear that it was MM/DD/YYYY so I changed the to following

func Today()(result string){
    current_time := time.Now().Local()
    result =  current_time.Format("01/23/2004")
    return
}

Which to my chagrin caused bad results. Prints MM/DDHH/DD0MM

Realizing my mistake I see that the format is defined by the reference time...

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

I'm wondering if there is any other instances this moment being used as a formatting reference for date times, and if this reference moment has a nickname (like null island)?

答案1

得分: 2

日期字符串中的值并不是任意的。你不能只是将02改为03,然后期望它能正常工作。日期格式化程序会寻找特定的值,并知道1表示月份,2表示日期等等。

01/02/2006改为01/23/2004就像将一个可读的表单从First Name: ______ Last Name: ______改为First Name: ______ Ice Cream: ______。你不能期望任何人知道Ice Cream应该表示Last Name

名称

对于这个名称,唯一提供的是“参考时间”,在这里

> Parse解析一个格式化的字符串并返回它表示的时间值。布局定义了格式,它展示了参考时间的形式,定义如下:
>
> Mon Jan 2 15:04:05 -0700 MST 2006

以及在这里

> 这些是用于Time.Format和Time.Parse的预定义布局。布局中使用的参考时间是特定的时间:
>
> Mon Jan 2 15:04:05 MST 2006
>
> 它对应的Unix时间是1136239445。由于MST是GMT-0700,参考时间可以被认为是
>
> 01/02 03:04:05PM '06 -0700
>
> 要定义自己的格式,可以写下以你的方式格式化的参考时间是什么样的;可以参考ANSIC、StampMicro或Kitchen等常量的值来进行示例。模型是展示参考时间的样子,以便Format和Parse方法可以对一般的时间值应用相同的转换。

要指定你正在谈论Go的参考时间,我会说“Go的参考时间”。或者更明显一些,说“Go的time.Parse参考时间”。


顺便说一下,你的函数可以大大简化:

func Today() string {
    return time.Now().Local().Format("01/02/2006")
}
英文:

The values in a date string are not arbitrary. You can't just change 02 to 03 and expect it to work. The date formatter looks for those specific values, and knows that 1 means month, 2 means day of month, etc.

Changing 01/02/2006 to 01/23/2004 is like changing a human-readable form that says First Name: ______ Last Name: ______ to one that says First Name: ______ Ice Cream: ______. You can't expect anyone to know that Ice Cream should mean Last Name.

The name

The only name provided for this is "reference time", here:

> 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

and here:

> These are predefined layouts for use in Time.Format and Time.Parse. The reference time used in the layouts is the specific time:
>
> Mon Jan 2 15:04:05 MST 2006
>
> which is Unix time 1136239445. Since MST is GMT-0700, the reference time can be thought of as
>
> 01/02 03:04:05PM '06 -0700
>
> To define your own format, write down what the reference time would look like formatted your way; see the values of constants like ANSIC, StampMicro or Kitchen for examples. The model is to demonstrate what the reference time looks like so that the Format and Parse methods can apply the same transformation to a general time value.

To specify that you're talking about Go's reference time, I'd say "Go's reference time." Or to be blatantly obvious, "Go's time.Parse reference time."


As an aside, your function can be greatly shortened:

func Today() string {
    return time.Now().Local().Format("01/02/2006")
}

huangapple
  • 本文由 发表于 2017年9月12日 11:22:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/46167391.html
匿名

发表评论

匿名网友

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

确定