“Handy time stamps”在time包中的目的是什么?

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

What is the purpose of the "Handy time stamps" in the time package?

问题

我正在尝试弄清楚时间包中的"Handy Time Stamp"用于什么目的。

我可以使用其他常量(如RFC)很好地解析日期:

t, _ := time.Parse(time.RFC822, "02 Jan 06 15:04 MST")
fmt.Println(t.Unix())
输出 1136214240

t, _ := time.Parse(time.Stamp, "Jan _2 15:04:05")
fmt.Println(t.Unix())
输出:-62135596800

最后一个输出是错误的。我在这里漏掉了什么?这些时间戳有什么用处?

以下是时间常量的Godoc:

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"
)
英文:

Reference:

I am trying to figure out what the "Handy Time Stamp" are used for in the time package.

I can parse dates just fine using the other constants such as RFC

t, _ := time.Parse(time.RFC822, "02 Jan 06 15:04 MST")
fmt.Println(t.Unix())

> Output 1136214240

vs

t, _ := time.Parse(time.Stamp, "Jan _2 15:04:05")
fmt.Println(t.Unix())

> Output: -62135596800

The last output is wrong. What am I missing here? How are these timestamps useful?

Below is the Godoc for time constants:

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"
)

答案1

得分: 6

最后的输出是错误的。我在这里漏掉了什么?这些时间戳有什么用?

你明显漏掉了一个错误检查,让我们添加上吧。

t, err := time.Parse(time.Stamp, "Jan _2 15:04:05")
fmt.Println(err)
fmt.Println(t.Unix())

输出结果为:

parsing time "Jan _2 15:04:05" as "Jan _2 15:04:05": cannot parse "_2
15:04:05" as "_2"
-62135596800

正确的字符串应该是"Jan 2 15:04:05"(注意Jan和2之间有两个空格)。关于文档中的下划线:

在格式字符串中,下划线 _ 表示一个空格,如果后面的数字(一个日期)有两位数,则可以用一个数字替换它;这是为了与固定宽度的Unix时间格式兼容。

那么,为什么它的UNIX时间表示为负数,我们来检查一下:

t, err := time.Parse(time.Stamp, "Jan  2 15:04:05")
fmt.Println(err)
fmt.Println(t)

输出结果为:

<nil>
0000-01-02 15:04:05 +0000 UTC

所以它是负数是因为年份是0000年。

最后,它在哪些情况下有用呢?例如,用于测量耗时操作的持续时间。你可以将当前时间以其中一种Stamp格式输出到日志中,同时附带一些消息,比如"开始执行这个操作"、"完成执行那个操作"。然后,由于它是固定宽度格式且没有不必要的年份信息,很容易阅读日志,也很容易解析这样的日志。

这种格式实际上在*nix中的"syslog"中使用。

英文:

> The last output is wrong. What am I missing here? How are these timestamps useful?

You are definitely missing an error check here, let's add it

t, err := time.Parse(time.Stamp, &quot;Jan _2 15:04:05&quot;)
fmt.Println(err)
fmt.Println(t.Unix())

Output:
> parsing time "Jan _2 15:04:05" as "Jan _2 15:04:05": cannot parse "_2
> 15:04:05" as "_2"
> -62135596800

The correct string would be "Jan&nbsp;&nbsp;2 15:04:05" (note the double space between Jan and 2). About underscore from the docs:

> Within the format string, an underscore _ represents a space that may
> be replaced by a digit if the following number (a day) has two digits;
> for compatibility with fixed-width Unix time formats.

Then, why it's representations as UNIX time is negative, let's check:

t, err := time.Parse(time.Stamp, &quot;Jan  2 15:04:05&quot;)
fmt.Println(err)
fmt.Println(t)

Output:

&lt;nil&gt;
0000-01-02 15:04:05 +0000 UTC

So it's negative because the year is 0000.

And finally, where it can be useful? For example, to measure duration of time-consuming operations. You can output to logs current time in one of Stamp formats along with some messages like "Started doing this", "Finished doing that". Then, because it's fixed-width format and without unnecessary year information - it's easy to read the logs, easy to parse such logs.

This format is actually used in "syslog" in *nix.

huangapple
  • 本文由 发表于 2014年1月31日 02:51:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/21465082.html
匿名

发表评论

匿名网友

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

确定