mirror of
https://github.com/araddon/dateparse.git
synced 2024-11-10 11:51:54 +08:00
扩充时间格式
This commit is contained in:
parent
b87b24a51f
commit
a7e3619d47
67
parseany.go
67
parseany.go
@ -5,7 +5,6 @@ package dateparse
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
@ -50,39 +49,13 @@ 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
|
||||
|
||||
const (
|
||||
dateStart dateState = iota // 0
|
||||
dateDigit
|
||||
dateDigitSt
|
||||
dateYearDash
|
||||
dateYearDashAlphaDash
|
||||
dateYearDashDash
|
||||
@ -391,9 +364,13 @@ iterRunes:
|
||||
// 02 Jan 2018 23:59:34
|
||||
// 12 Feb 2006, 19:17
|
||||
// 12 Feb 2006, 19:17:22
|
||||
p.stateDate = dateDigitWs
|
||||
p.dayi = 0
|
||||
p.daylen = i
|
||||
if i == 6 {
|
||||
p.stateDate = dateDigitSt
|
||||
} else {
|
||||
p.stateDate = dateDigitWs
|
||||
p.dayi = 0
|
||||
p.daylen = i
|
||||
}
|
||||
case '年':
|
||||
// Chinese Year
|
||||
p.stateDate = dateDigitChineseYear
|
||||
@ -404,6 +381,11 @@ iterRunes:
|
||||
}
|
||||
p.part1Len = i
|
||||
|
||||
case dateDigitSt:
|
||||
p.set(0, "060102")
|
||||
i = i -1
|
||||
p.stateTime = timeStart
|
||||
break iterRunes
|
||||
case dateYearDash:
|
||||
// dateYearDashDashT
|
||||
// 2006-01-02T15:04:05Z07:00
|
||||
@ -526,6 +508,16 @@ iterRunes:
|
||||
p.setDay()
|
||||
}
|
||||
break iterRunes
|
||||
case ':':
|
||||
p.stateTime = timeStart
|
||||
if p.yearlen == 0 {
|
||||
p.yearlen = i - p.yeari
|
||||
p.setYear()
|
||||
} else if p.daylen == 0 {
|
||||
p.daylen = i - p.dayi
|
||||
p.setDay()
|
||||
}
|
||||
break iterRunes
|
||||
case '/':
|
||||
if p.yearlen > 0 {
|
||||
// 2014/07/10 06:55:38.156283
|
||||
@ -1666,6 +1658,9 @@ iterRunes:
|
||||
p.t = &t
|
||||
return p, nil
|
||||
}
|
||||
case dateDigitSt:
|
||||
// 171113 14:14:20
|
||||
return p, nil
|
||||
|
||||
case dateYearDash:
|
||||
// 2006-01
|
||||
@ -1818,15 +1813,6 @@ 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)
|
||||
}
|
||||
|
||||
@ -2039,6 +2025,7 @@ func (p *parser) parse() (time.Time, error) {
|
||||
p.format = p.format[p.skip:]
|
||||
p.datestr = p.datestr[p.skip:]
|
||||
}
|
||||
|
||||
//gou.Debugf("parse %q AS %q", p.datestr, string(p.format))
|
||||
if p.loc == nil {
|
||||
return time.Parse(string(p.format), p.datestr)
|
||||
|
@ -508,7 +508,7 @@ 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: "06/Jan/2008: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"},
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user