简体   繁体   English

为什么 golang time.Parse 支持小数秒,即使 formatString 没有小数秒?

[英]Why golang time.Parse supports fractional seconds even the formatString does not have them?

this code:这个代码:

fmt.Println(time.Parse("2006-01-02T15:04:05", "2020-01-26T08:02:29.9"))

( https://go.dev/play/p/w1eZNdVgse4 ) ( https://go.dev/play/p/w1eZNdVgse4 )

prints: 2020-01-26 08:02:29.9 +0000 UTC打印:2020-01-26 08:02:29.9 +0000 UTC

It does not have fractional seconds in the format but they are still going though.它的格式中没有小数秒,但它们仍在继续。 Anyone any idea is there some reason to this.任何人有任何想法是有原因的。 the source: https://cs.opensource.google/go/go/+/refs/tags/go1.18:src/time/format.go;l=1075;drc=refs%2Ftags%2Fgo1.18来源: https://cs.opensource.google/go/go/+/refs/tags/go1.18:src/time/format.go;l=1075;drc=refs%2Ftags%2Fgo1.18

According to the documentation for time.Parse:根据 time.Parse 的文档:

When parsing, if the seconds value is followed by a decimal point and some digits, that is taken as a fraction of a second even if the layout string does not represent the fractional second.解析时,如果秒值后跟小数点和一些数字,即使布局字符串不表示小数秒,也将其视为秒的小数部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM