From b87b24a51fb97e1fafac8c3dec4030a20fe30ee1 Mon Sep 17 00:00:00 2001 From: liushaobo Date: Wed, 3 Feb 2021 18:10:39 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8F=90=E4=BA=A4=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- parseany.go | 37 +++++++++++++++++++++++++++++++++++++ parseany_test.go | 4 ++++ 2 files changed, 41 insertions(+) diff --git a/parseany.go b/parseany.go index 61a021e..ef74009 100644 --- a/parseany.go +++ b/parseany.go @@ -5,6 +5,7 @@ package dateparse import ( "fmt" + "regexp" "strconv" "strings" "time" @@ -49,6 +50,33 @@ var months = []string{ "december", } +var datePattern = []struct { + desc string + pattern string + goFmt string +}{ + { + desc: "nginx log datetime, 02/Jan/2006:15:04:05 -0700", + pattern: `\d{2}/\w+/\d{4}:\d{2}:\d{2}:\d{2} \+\d{4}`, + goFmt: "02/Jan/2006:15:04:05 -0700", + }, + { + desc: "redis log datetime, 14 May 2019 19:11:40.164", + pattern: `\d{2} \w+ \d{4} \d{2}:\d{2}:\d{2}.\d{3}`, + goFmt: "02 Jan 2006 15:04:05.000", + }, + { + desc: "redis log datetime, 14 May 19:11:40.164", + pattern: `\d{2} \w+ \d{2}:\d{2}:\d{2}.\d{3}`, + goFmt: "02 Jan 15:04:05.000", + }, + { + desc: "mysql, 171113 14:14:20", + pattern: `\d{6} \d{2}:\d{2}:\d{2}`, + goFmt: "060102 15:04:05", + }, +} + type dateState uint8 type timeState uint8 @@ -1790,6 +1818,15 @@ iterRunes: } + for _, d := range datePattern { + if match, err := regexp.MatchString(d.pattern, datestr); err != nil { + return nil, err + } else if match { + p.format = []byte(d.goFmt) + return p, nil + } + } + return nil, unknownErr(datestr) } diff --git a/parseany_test.go b/parseany_test.go index 35e212c..1faee76 100644 --- a/parseany_test.go +++ b/parseany_test.go @@ -508,6 +508,10 @@ var testParseFormat = []dateTest{ {in: "2009-15-12T22:15Z", err: true}, {in: "5,000-9,999", err: true}, // + {in: "02/Jan/2006:15:04:05 -0700", out: "01/Jan/2006:15:04:05 -0700"}, + {in: "14 May 2019 19:11:40.164", out: "02 Jan 2006 15:04:05.000"}, + {in: "171113 14:14:20", out: "060102 15:04:05"}, + {in: "oct 7, 1970", out: "Jan 2, 2006"}, {in: "sept. 7, 1970", out: "Jan. 2, 2006"}, {in: "May 05, 2015, 05:05:07", out: "Jan 02, 2006, 15:04:05"},