mirror of
https://gitee.com/johng/gf
synced 2026-06-07 10:22:11 +08:00
gconv.Time方法增加对带毫秒时间字符串的支持,并完善示例程序
This commit is contained in:
@ -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
|
||||
|
||||
@ -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())
|
||||
}
|
||||
Reference in New Issue
Block a user