mirror of
https://github.com/araddon/dateparse.git
synced 2024-11-10 11:51:54 +08:00
提交代码
This commit is contained in:
parent
8aadafed4d
commit
b87b24a51f
37
parseany.go
37
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)
|
||||
}
|
||||
|
||||
|
@ -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"},
|
||||
|
Loading…
Reference in New Issue
Block a user