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