为什么解析时间字符串格式是这样?
来源:2-5 编码实战:实现多个文件查询信息接口

拧壶冲
2020-10-16
我看到我们的代码有这样使用parse的format
const baseFormat = "2006-01-02 15:04:05"
老师说这是golang中的特殊时间点,为什么呢?
我看到golang的文档上是这样写的
// Some valid layouts are invalid time values, due to format specifiers
// such as _ for space padding and Z for zone information.
// For example the RFC3339 layout 2006-01-02T15:04:05Z07:00
// contains both Z and a time zone offset in order to handle both valid options:
// 2006-01-02T15:04:05Z
// 2006-01-02T15:04:05+07:00
t, _ = time.Parse(time.RFC3339, "2006-01-02T15:04:05Z")
这个时间有什么特殊的意义吗?所有的时间format都不想用这个时间来作为format吗?
很多语言用“yyyy-mm-d HH:MM:SS”来作为format。
写回答
1回答
-
同学你好,这个时间格式化的方式,初衷应该是作者为了方便大家记忆吧。可以看下这段解释:
// 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 // There are some wrinkles illustrated below.
简单就是1月2号3点4分5秒2006年-7时区,这种1234567的方式对于熟悉英语写法的人来说会比较容易理解和记忆吧。而对于使用惯了yyyy-mm-dd或YYYY-MM-dd这种方式来做日期格式化的开发者来说,的确是有点不太好接受。
022020-10-20
相似问题