提交代码

This commit is contained in:
liushaobo 2021-02-03 18:10:39 +08:00
parent 8aadafed4d
commit b87b24a51f
2 changed files with 41 additions and 0 deletions

View File

@ -5,6 +5,7 @@ package dateparse
import ( import (
"fmt" "fmt"
"regexp"
"strconv" "strconv"
"strings" "strings"
"time" "time"
@ -49,6 +50,33 @@ var months = []string{
"december", "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 dateState uint8
type timeState 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) return nil, unknownErr(datestr)
} }

View File

@ -508,6 +508,10 @@ var testParseFormat = []dateTest{
{in: "2009-15-12T22:15Z", err: true}, {in: "2009-15-12T22:15Z", err: true},
{in: "5,000-9,999", 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: "oct 7, 1970", out: "Jan 2, 2006"},
{in: "sept. 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"}, {in: "May 05, 2015, 05:05:07", out: "Jan 02, 2006, 15:04:05"},