From 55e026b712e6f2a9cde001d0bb549446818ca3af Mon Sep 17 00:00:00 2001 From: john Date: Wed, 20 Jun 2018 15:44:38 +0800 Subject: [PATCH] =?UTF-8?q?gconv.Time=E6=96=B9=E6=B3=95=E5=A2=9E=E5=8A=A0?= =?UTF-8?q?=E5=AF=B9=E5=B8=A6=E6=AF=AB=E7=A7=92=E6=97=B6=E9=97=B4=E5=AD=97?= =?UTF-8?q?=E7=AC=A6=E4=B8=B2=E7=9A=84=E6=94=AF=E6=8C=81=EF=BC=8C=E5=B9=B6?= =?UTF-8?q?=E5=AE=8C=E5=96=84=E7=A4=BA=E4=BE=8B=E7=A8=8B=E5=BA=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- g/util/gconv/gconv.go | 7 ++++++- geg/util/gconv/time2.go | 5 +++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/g/util/gconv/gconv.go b/g/util/gconv/gconv.go index d41675818..e1bb09837 100644 --- a/g/util/gconv/gconv.go +++ b/g/util/gconv/gconv.go @@ -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 diff --git a/geg/util/gconv/time2.go b/geg/util/gconv/time2.go index ef60834c2..a02ac32a3 100644 --- a/geg/util/gconv/time2.go +++ b/geg/util/gconv/time2.go @@ -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()) } \ No newline at end of file