mirror of
https://github.com/araddon/dateparse.git
synced 2024-11-13 05:06:36 +08:00
Support RabbitMQ log format (dd-mon-yyyy::hh:mm:ss)
Adapt https://github.com/araddon/dateparse/pull/122 by https://github.com/bizy01 to add support for RMQ log format. Refactor to avoid redundant code. Add format validations. As a side note, will also support the format dd-mm-yyyy:hh:mm:ss.
This commit is contained in:
parent
249dd7368c
commit
0c3943eacd
97
parseany.go
97
parseany.go
@ -598,44 +598,76 @@ iterRunes:
|
|||||||
// 13-Feb-03 ambiguous
|
// 13-Feb-03 ambiguous
|
||||||
// 28-Feb-03 ambiguous
|
// 28-Feb-03 ambiguous
|
||||||
// 29-Jun-2016 dd-month(alpha)-yyyy
|
// 29-Jun-2016 dd-month(alpha)-yyyy
|
||||||
|
// 8-Mar-2018::
|
||||||
// dateDigitDashDigitDash:
|
// dateDigitDashDigitDash:
|
||||||
// 29-06-2026
|
// 29-06-2026
|
||||||
|
// 08-03-18:: ambiguous (dd-mm-yy or yy-mm-dd)
|
||||||
switch r {
|
switch r {
|
||||||
case ' ':
|
case ' ', ':':
|
||||||
// we need to find if this was 4 digits, aka year
|
doubleColonTimeConnector := false
|
||||||
// or 2 digits which makes it ambiguous year/day
|
if r == ':' {
|
||||||
length := i - (p.moi + p.molen + 1)
|
p.link++
|
||||||
if length == 4 {
|
if p.link == 2 {
|
||||||
p.yearlen = 4
|
if i+1 < len(p.datestr) {
|
||||||
p.set(p.yeari, "2006")
|
// only legitimate content to follow "::" is the start of the time
|
||||||
// We now also know that part1 was the day
|
nextChar, _ := utf8.DecodeRuneInString(p.datestr[i+1:])
|
||||||
p.dayi = 0
|
if unicode.IsDigit(nextChar) {
|
||||||
p.daylen = p.part1Len
|
doubleColonTimeConnector = true
|
||||||
if !p.setDay() {
|
}
|
||||||
return p, unknownErr(datestr)
|
}
|
||||||
|
if !doubleColonTimeConnector {
|
||||||
|
return p, unknownErr(datestr)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} else if length == 2 {
|
} else if p.link > 0 {
|
||||||
// We have no idea if this is
|
return p, unknownErr(datestr)
|
||||||
// yy-mon-dd OR dd-mon-yy
|
}
|
||||||
// (or for dateDigitDashDigitDash, yy-mm-dd OR dd-mm-yy)
|
if r == ' ' || doubleColonTimeConnector {
|
||||||
//
|
// we need to find if this was 4 digits, aka year
|
||||||
// We are going to ASSUME (bad, bad) that it is dd-mon-yy (dd-mm-yy),
|
// or 2 digits which makes it ambiguous year/day
|
||||||
// which is a horrible assumption, but seems to be the convention for
|
var sepLen int
|
||||||
// dates that are formatted in this way.
|
if doubleColonTimeConnector {
|
||||||
p.ambiguousMD = true
|
sepLen = 2
|
||||||
p.yearlen = 2
|
} else {
|
||||||
p.set(p.yeari, "06")
|
sepLen = 1
|
||||||
// We now also know that part1 was the day
|
}
|
||||||
p.dayi = 0
|
length := i - (p.moi + p.molen + sepLen)
|
||||||
p.daylen = p.part1Len
|
if length == 4 {
|
||||||
if !p.setDay() {
|
p.yearlen = 4
|
||||||
return p, unknownErr(datestr)
|
p.set(p.yeari, "2006")
|
||||||
}
|
// We now also know that part1 was the day
|
||||||
} else {
|
p.dayi = 0
|
||||||
|
p.daylen = p.part1Len
|
||||||
|
if !p.setDay() {
|
||||||
|
return p, unknownErr(datestr)
|
||||||
|
}
|
||||||
|
} else if length == 2 {
|
||||||
|
// We have no idea if this is
|
||||||
|
// yy-mon-dd OR dd-mon-yy
|
||||||
|
// (or for dateDigitDashDigitDash, yy-mm-dd OR dd-mm-yy)
|
||||||
|
//
|
||||||
|
// We are going to ASSUME (bad, bad) that it is dd-mon-yy (dd-mm-yy),
|
||||||
|
// which is a horrible assumption, but seems to be the convention for
|
||||||
|
// dates that are formatted in this way.
|
||||||
|
p.ambiguousMD = true
|
||||||
|
p.yearlen = 2
|
||||||
|
p.set(p.yeari, "06")
|
||||||
|
// We now also know that part1 was the day
|
||||||
|
p.dayi = 0
|
||||||
|
p.daylen = p.part1Len
|
||||||
|
if !p.setDay() {
|
||||||
|
return p, unknownErr(datestr)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return p, unknownErr(datestr)
|
||||||
|
}
|
||||||
|
p.stateTime = timeStart
|
||||||
|
break iterRunes
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
if !unicode.IsDigit(r) && !unicode.IsLetter(r) && p.link > 0 {
|
||||||
return p, unknownErr(datestr)
|
return p, unknownErr(datestr)
|
||||||
}
|
}
|
||||||
p.stateTime = timeStart
|
|
||||||
break iterRunes
|
|
||||||
}
|
}
|
||||||
|
|
||||||
case dateDigitYearSlash:
|
case dateDigitYearSlash:
|
||||||
@ -2397,6 +2429,7 @@ type parser struct {
|
|||||||
fullMonth string
|
fullMonth string
|
||||||
parsedAMPM bool
|
parsedAMPM bool
|
||||||
skip int
|
skip int
|
||||||
|
link int
|
||||||
extra int
|
extra int
|
||||||
part1Len int
|
part1Len int
|
||||||
yeari int
|
yeari int
|
||||||
|
@ -446,6 +446,9 @@ var testInputs = []dateTest{
|
|||||||
// Git log default date format - https://github.com/araddon/dateparse/pull/92
|
// Git log default date format - https://github.com/araddon/dateparse/pull/92
|
||||||
{in: "Thu Apr 7 15:13:13 2005 -0700", out: "2005-04-07 22:13:13 +0000 UTC"},
|
{in: "Thu Apr 7 15:13:13 2005 -0700", out: "2005-04-07 22:13:13 +0000 UTC"},
|
||||||
{in: "Tue Dec 12 23:07:11 2023 -0700", out: "2023-12-13 06:07:11 +0000 UTC"},
|
{in: "Tue Dec 12 23:07:11 2023 -0700", out: "2023-12-13 06:07:11 +0000 UTC"},
|
||||||
|
// RabbitMQ log format - https://github.com/araddon/dateparse/pull/122
|
||||||
|
{in: "8-Mar-2018::14:09:27", out: "2018-03-08 14:09:27 +0000 UTC"},
|
||||||
|
{in: "08-03-2018::02:09:29 PM", out: "2018-03-08 14:09:29 +0000 UTC"},
|
||||||
// yyyy-mm-dd hh:mm:ss,000
|
// yyyy-mm-dd hh:mm:ss,000
|
||||||
{in: "2014-05-11 08:20:13,787", out: "2014-05-11 08:20:13.787 +0000 UTC"},
|
{in: "2014-05-11 08:20:13,787", out: "2014-05-11 08:20:13.787 +0000 UTC"},
|
||||||
// yyyy-mm-dd hh:mm:ss +0000
|
// yyyy-mm-dd hh:mm:ss +0000
|
||||||
@ -827,6 +830,10 @@ var testParseErrors = []dateTest{
|
|||||||
{in: "2014-02-13 00:00:00 utc", err: true}, // lowercase timezones are not valid
|
{in: "2014-02-13 00:00:00 utc", err: true}, // lowercase timezones are not valid
|
||||||
{in: "2014-02-13t00:00:00.0z", err: true}, // lowercase 't' separator is not supported
|
{in: "2014-02-13t00:00:00.0z", err: true}, // lowercase 't' separator is not supported
|
||||||
{in: "2014-02-13T00:00:00.0z", err: true}, // lowercase 'z' zulu timezone indicator not a valid format
|
{in: "2014-02-13T00:00:00.0z", err: true}, // lowercase 'z' zulu timezone indicator not a valid format
|
||||||
|
// Invalid variants of RabbitMQ log format
|
||||||
|
{in: "8-Mar-2018:14:09:27", err: true},
|
||||||
|
{in: "8-Mar-2018: 14:09:27", err: true},
|
||||||
|
{in: "8-Mar-2018:::14:09:27", err: true},
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestParseErrors(t *testing.T) {
|
func TestParseErrors(t *testing.T) {
|
||||||
@ -1093,5 +1100,5 @@ func TestRetryAmbiguousDateWithSwap(t *testing.T) {
|
|||||||
|
|
||||||
// Convenience function for debugging a particular broken test case
|
// Convenience function for debugging a particular broken test case
|
||||||
func TestDebug(t *testing.T) {
|
func TestDebug(t *testing.T) {
|
||||||
MustParse("Tue Dec 12 23:07:11 2023 -0700")
|
MustParse("8-Mar-2018::14:09:27")
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user