From 18fe186be1798147e03fce38bdf65eab68434d1c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benjamin=20Kr=C3=A4mer?= Date: Thu, 1 Oct 2020 16:17:40 +0200 Subject: [PATCH] Fix timestamp parser for short year format --- pkg/logentry/stages/util.go | 2 +- pkg/logentry/stages/util_test.go | 22 ++++++++++++++++++++-- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/pkg/logentry/stages/util.go b/pkg/logentry/stages/util.go index 407387f7c486..d2b7d514f2fb 100644 --- a/pkg/logentry/stages/util.go +++ b/pkg/logentry/stages/util.go @@ -112,7 +112,7 @@ func convertDateLayout(predef string, location *time.Location) parser { return time.Unix(0, i), nil } default: - if !strings.Contains(predef, "2006") { + if !strings.Contains(predef, "06") && !strings.Contains(predef, "2006") { return func(t string) (time.Time, error) { return parseTimestampWithoutYear(predef, location, t, time.Now()) } diff --git a/pkg/logentry/stages/util_test.go b/pkg/logentry/stages/util_test.go index 5cf5e6c9cac8..6e25d245db1e 100644 --- a/pkg/logentry/stages/util_test.go +++ b/pkg/logentry/stages/util_test.go @@ -83,19 +83,37 @@ func TestConvertDateLayout(t *testing.T) { timestamp string expected time.Time }{ - "custom layout with year": { + "custom layout with short year": { + "06 Jan 02 15:04:05", + nil, + "19 Jul 15 01:02:03", + time.Date(2019, 7, 15, 1, 2, 3, 0, time.UTC), + }, + "custom layout with long year": { "2006 Jan 02 15:04:05", nil, "2019 Jul 15 01:02:03", time.Date(2019, 7, 15, 1, 2, 3, 0, time.UTC), }, + "custom layout with short year and location": { + "06 Jan 02 15:04:05", + location, + "19 Jul 15 01:02:03", + time.Date(2019, 7, 15, 1, 2, 3, 0, location), + }, + "custom layout with long year and location": { + "2006 Jan 02 15:04:05", + location, + "2019 Jul 15 01:02:03", + time.Date(2019, 7, 15, 1, 2, 3, 0, location), + }, "custom layout without year": { "Jan 02 15:04:05", nil, "Jul 15 01:02:03", time.Date(time.Now().Year(), 7, 15, 1, 2, 3, 0, time.UTC), }, - "custom layout with year and location": { + "custom layout without year and location": { "Jan 02 15:04:05", location, "Jul 15 01:02:03",