Skip to content

Commit

Permalink
Fix timestamp parser for short year format (grafana#2708)
Browse files Browse the repository at this point in the history
Signed-off-by: Cyril Tovena <cyril.tovena@gmail.com>
  • Loading branch information
Falco20019 authored and cyriltovena committed Oct 21, 2020
1 parent 63a91aa commit f6eb8d6
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
2 changes: 1 addition & 1 deletion pkg/logentry/stages/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
Expand Down
22 changes: 20 additions & 2 deletions pkg/logentry/stages/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit f6eb8d6

Please sign in to comment.