From 2c00358f4fdc9a7868214936cea90fe2b4977af2 Mon Sep 17 00:00:00 2001 From: Gisle Aune Date: Sat, 20 Jul 2019 15:20:39 +0200 Subject: [PATCH] Fixed advance by one date bug by treating any out of order posts as a date bump. --- services/parsers/mirclike.go | 7 ++++-- services/parsers/mirclike_test.go | 36 ++++++++++++++++++++++++++++++- 2 files changed, 40 insertions(+), 3 deletions(-) diff --git a/services/parsers/mirclike.go b/services/parsers/mirclike.go index ed0c6fd..a3f59e1 100644 --- a/services/parsers/mirclike.go +++ b/services/parsers/mirclike.go @@ -83,9 +83,12 @@ func MircPost(line string, date time.Time, prev models.Post) (models.Post, error } // Determine timestamp from parsed data and previous post. + if !prev.Time.IsZero() { + date = prev.Time + } ts := time.Date(date.Year(), date.Month(), date.Day(), tsUnits[0], tsUnits[1], tsUnits[2], 0, date.Location()) - if !prev.Time.IsZero() && prev.Time.Sub(ts) > 30*time.Minute { - ts = time.Date(prev.Time.Year(), prev.Time.Month(), prev.Time.Day()+1, tsUnits[0], tsUnits[1], tsUnits[2], 0, date.Location()) + if ts.Before(prev.Time) { + ts = ts.Add(time.Hour * 24) } if line[tsEndIndex+2] == '*' { diff --git a/services/parsers/mirclike_test.go b/services/parsers/mirclike_test.go index dc0a37b..247a026 100644 --- a/services/parsers/mirclike_test.go +++ b/services/parsers/mirclike_test.go @@ -92,7 +92,6 @@ func TestMircPostNextDay(t *testing.T) { Time time.Time }{ {Prev: parseDate(t, "2019-01-12 12:00:00"), TS: "12:01", Time: parseDate(t, "2019-01-12 12:01:00")}, - {Prev: parseDate(t, "2019-01-12 12:00:00"), TS: "11:53:13", Time: parseDate(t, "2019-01-12 11:53:13")}, {Prev: parseDate(t, "2019-04-08 23:51:59"), TS: "00:09", Time: parseDate(t, "2019-04-09 00:09:00")}, {Prev: parseDate(t, "2019-01-12 12:00:00"), TS: "11:29:59", Time: parseDate(t, "2019-01-13 11:29:59")}, } @@ -111,6 +110,41 @@ func TestMircPostNextDay(t *testing.T) { } } +func TestMircPostDayPerPostBug(t *testing.T) { + startDate := parseDate(t, "2019-07-20 23:25:00") + table := []struct { + TS string + Time time.Time + }{ + {TS: "23:25", Time: parseDate(t, "2019-07-20 23:25:00")}, + {TS: "23:45", Time: parseDate(t, "2019-07-20 23:45:00")}, + {TS: "23:59", Time: parseDate(t, "2019-07-20 23:59:00")}, + {TS: "00:00", Time: parseDate(t, "2019-07-21 00:00:00")}, + {TS: "00:03", Time: parseDate(t, "2019-07-21 00:03:00")}, + {TS: "00:06", Time: parseDate(t, "2019-07-21 00:06:00")}, + {TS: "12:00", Time: parseDate(t, "2019-07-21 12:00:00")}, + {TS: "22:00", Time: parseDate(t, "2019-07-21 22:00:00")}, + {TS: "23:55", Time: parseDate(t, "2019-07-21 23:55:00")}, + {TS: "00:05", Time: parseDate(t, "2019-07-22 00:05:00")}, + } + + prev := startDate + for i, row := range table { + t.Run(fmt.Sprintf("Row_%d", i), func(t *testing.T) { + input := fmt.Sprintf("[%s] * Test post content.", row.TS) + + post, err := parsers.MircPost(input, startDate, models.Post{Time: prev}) + if err != nil { + t.Fatal("Could not parse post:", err) + } + + prev = post.Time + + assert.Equal(t, row.Time, post.Time) + }) + } +} + func TestMircLog(t *testing.T) { parsed, err := parsers.MircLog(testLog, parseDate(t, "2018-05-11 00:00:00"), false) if err != nil {