douhu2525 2014-01-30 18:51
浏览 91
已采纳

时标中“便捷时间戳”的目的是什么?

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条回答 默认 最新

  • duansha6410 2014-01-30 19:42
    关注

    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, "Jan _2 15:04:05")
    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  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, "Jan  2 15:04:05")
    fmt.Println(err)
    fmt.Println(t)
    

    Output:

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 linux驱动,linux应用,多线程