From a42fa3d61b3e44bbb3802b7d94cfc2e447353edc Mon Sep 17 00:00:00 2001 From: Jinzhu Date: Thu, 18 Nov 2021 17:34:16 +0800 Subject: [PATCH] Fix hasTimeRegexp, close #51 --- now.go | 4 ++-- now_test.go | 3 +++ 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/now.go b/now.go index 869c4b2..83ce6b5 100644 --- a/now.go +++ b/now.go @@ -149,8 +149,8 @@ func (now *Now) parseWithFormat(str string, location *time.Location) (t time.Tim return } -var hasTimeRegexp = regexp.MustCompile(`(\s+|^\s*)\d{1,2}((:\d{1,2})*|((:\d{1,2}){2}\.(\d{3}|\d{6}|\d{9})))\s*$`) // match 15:04:05, 15:04:05.000, 15:04:05.000000 15, 2017-01-01 15:04, etc -var onlyTimeRegexp = regexp.MustCompile(`^\s*\d{1,2}((:\d{1,2})*|((:\d{1,2}){2}\.(\d{3}|\d{6}|\d{9})))\s*$`) // match 15:04:05, 15, 15:04:05.000, 15:04:05.000000, etc +var hasTimeRegexp = regexp.MustCompile(`(\s+|^\s*|T)\d{1,2}((:\d{1,2})*|((:\d{1,2}){2}\.(\d{3}|\d{6}|\d{9})))(\s*$|Z)`) // match 15:04:05, 15:04:05.000, 15:04:05.000000 15, 2017-01-01 15:04, 2021-07-20T00:59:10Z etc +var onlyTimeRegexp = regexp.MustCompile(`^\s*\d{1,2}((:\d{1,2})*|((:\d{1,2}){2}\.(\d{3}|\d{6}|\d{9})))\s*$`) // match 15:04:05, 15, 15:04:05.000, 15:04:05.000000, etc // Parse parse string to time func (now *Now) Parse(strs ...string) (t time.Time, err error) { diff --git a/now_test.go b/now_test.go index 965580a..49388ee 100644 --- a/now_test.go +++ b/now_test.go @@ -276,6 +276,9 @@ func TestParse(t *testing.T) { assert(With(n).MustParse("2011-01-01", "18"), "2011-01-01 18:00:00", "Parse two strings 2011-01-01, 18") + assert(With(n).MustParse("2002-10-12T00:14:56Z"), "2002-10-12 00:14:56", "Parse 2002-10-12T00:14:56Z") + assert(With(n).MustParse("2002-10-12T00:00:56Z"), "2002-10-12 00:00:56", "Parse 2002-10-12T00:00:56Z") + TimeFormats = append(TimeFormats, "02 Jan 15:04") assert(With(n).MustParse("04 Feb 12:09"), "2013-02-04 12:09:00", "Parse 04 Feb 12:09 with specified format")