doujionggan9570 2015-01-22 11:18
浏览 99
已采纳

Go的time.Format(布局字符串)参考时间的意义是什么?

What is the significance of Go's time.Format(layout string) reference time, ie:

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

This specific time couldn't have been chosen completely randomly, right?

Source: http://golang.org/pkg/time/#Time.Format

  • 写回答

2条回答 默认 最新

  • duanliexi1052 2015-01-22 11:25
    关注

    Each part of the date is used as an index:

    Jan        -> 1      -> Month
    2          -> 2      -> Day-of-Month
    15 = 3PM   -> 15/3   -> hour
    04         -> 4      -> minute
    05         -> 5      -> second
    2006       -> 6      -> year
    -0700      -> 7      -> time-zone
    

    So according to the doc:

    Since MST is GMT-0700, the reference time can be thought of as 01/02 03:04:05PM '06 -0700

    This makes it easy for the time.Format method to parse human-readable date format specifications that are visually identical to the desired result.

    Compare this to for example the strftime C function that uses hard-to-remember format strings such as "%a, %d %b %y %T %z" which represents a RFC 822-compliant date format.

    The Go equivalent is: "Mon, 02 Jan 06 15:04 MST".

    The time.Format will tokenize this string and analyze each word.

    • Mon is recognized litteraly as monday so this is the week day's name
    • the comma is left untouched
    • 02 is recognized as the integer value 2, representing a day-of-month in the index
    • Jan is the known english abbreviation for the january month, so this is used for the month part
    • 06 is 6 so this the year part
    • 15 is equivalent to 3 and represent the hour
    • the ':' character is left untouched
    • 04 is 4, therefore the minute
    • MST is interpreted litteraly

    See http://golang.org/src/time/format.go?s=12714:12756#L117 for the exact algorithm.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 前端echarts坐标轴问题
  • ¥15 CMFCPropertyPage
  • ¥15 ad5933的I2C
  • ¥15 请问RTX4060的笔记本电脑可以训练yolov5模型吗?
  • ¥15 数学建模求思路及代码
  • ¥50 silvaco GaN HEMT有栅极场板的击穿电压仿真问题
  • ¥15 谁会P4语言啊,我想请教一下
  • ¥15 这个怎么改成直流激励源给加热电阻提供5a电流呀
  • ¥50 求解vmware的网络模式问题 别拿AI回答
  • ¥24 EFS加密后,在同一台电脑解密出错,证书界面找不到对应指纹的证书,未备份证书,求在原电脑解密的方法,可行即采纳