gconv.Time方法增加对带毫秒时间字符串的支持,并完善示例程序

This commit is contained in:
john
2018-06-20 15:44:38 +08:00
parent f630b6bc9a
commit 55e026b712
2 changed files with 11 additions and 1 deletions

View File

@ -23,7 +23,7 @@ var (
)
func init() {
timeRegex, _ = regexp.Compile(`(\d{4}-\d{2}-\d{2})\s{0,1}(\d{2}:\d{2}:\d{2}){0,1}`)
timeRegex, _ = regexp.Compile(`(\d{4}-\d{2}-\d{2})\s{0,1}(\d{2}:\d{2}:\d{2}){0,1}\.{0,1}(\d{0,9})`)
}
// 将变量i转换为字符串指定的类型t
@ -74,6 +74,11 @@ func Time(i interface{}) time.Time {
} else {
// 标准日期时间格式
if match := timeRegex.FindStringSubmatch(s); len(match) > 0 {
if match[3] != "" {
if t, err := time.Parse("2006-01-02 15:04:05.999999999", s); err == nil {
return t
}
}
if match[2] != "" {
if t, err := time.Parse("2006-01-02 15:04:05", s); err == nil {
return t

View File

@ -8,6 +8,11 @@ import (
func main() {
fmt.Println(gconv.Time("2018-06-07").Date())
fmt.Println(gconv.Time("2018-06-07").Clock())
fmt.Println(gconv.Time("2018-06-07 13:01:02").Date())
fmt.Println(gconv.Time("2018-06-07 13:01:02").Clock())
fmt.Println(gconv.Time("2018-06-07 13:01:02.096").Date())
fmt.Println(gconv.Time("2018-06-07 13:01:02.096").Clock())
fmt.Println(gconv.Time("2018-06-07 13:01:02.096").Nanosecond())
}