From 56efd2eb3f554b590fad8bebc4a1d752b66e5363 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Guillot?= Date: Wed, 26 Dec 2018 20:24:38 -0800 Subject: [PATCH] Add workaround for non GMT dates (RFC822, RFC850, and RFC1123) RFC822, RFC850, and RFC1123 are supposed to be always in GMT. This is a workaround for the one defined in PST timezone. --- reader/date/parser.go | 27 +++++++++++- reader/date/parser_test.go | 86 +++++++++++++++++++++++++++++++++++--- 2 files changed, 105 insertions(+), 8 deletions(-) diff --git a/reader/date/parser.go b/reader/date/parser.go index 8a22df83..a6cfbec5 100644 --- a/reader/date/parser.go +++ b/reader/date/parser.go @@ -206,8 +206,15 @@ func Parse(ds string) (t time.Time, err error) { return t, errors.New("date parser: empty value") } - for _, f := range dateFormats { - if t, err = time.Parse(f, d); err == nil { + for _, layout := range dateFormats { + switch layout { + case time.RFC822, time.RFC850, time.RFC1123: + if t, err = parseLocalTimeDates(layout, d); err == nil { + return + } + } + + if t, err = time.Parse(layout, d); err == nil { return } } @@ -221,6 +228,22 @@ func Parse(ds string) (t time.Time, err error) { return } +// According to Golang documentation: +// +// RFC822, RFC850, and RFC1123 formats should be applied only to local times. +// Applying them to UTC times will use "UTC" as the time zone abbreviation, +// while strictly speaking those RFCs require the use of "GMT" in that case. +func parseLocalTimeDates(layout, ds string) (t time.Time, err error) { + loc := time.UTC + + // Workaround for dates that don't use GMT. + if strings.HasSuffix(ds, "PST") { + loc, _ = time.LoadLocation("America/Los_Angeles") + } + + return time.ParseInLocation(layout, ds, loc) +} + // Replace German and French dates to English. func replaceNonEnglishWords(ds string) string { r := strings.NewReplacer( diff --git a/reader/date/parser_test.go b/reader/date/parser_test.go index b3306326..a215607a 100644 --- a/reader/date/parser_test.go +++ b/reader/date/parser_test.go @@ -4,7 +4,9 @@ package date // import "miniflux.app/reader/date" -import "testing" +import ( + "testing" +) func TestParseEmptyDate(t *testing.T) { if _, err := Parse(" "); err == nil { @@ -24,19 +26,91 @@ func TestParseAtomDate(t *testing.T) { t.Fatalf(`Atom dates should be parsed correctly`) } - if date.Unix() != 1513980589 { - t.Fatal(`Invalid date parsed`) + expectedTS := int64(1513980589) + if date.Unix() != expectedTS { + t.Errorf(`The Unix timestamp should be %v instead of %v`, expectedTS, date.Unix()) + } + + _, offset := date.Zone() + expectedOffset := 0 + if offset != expectedOffset { + t.Errorf(`The offset should be %v instead of %v`, expectedOffset, offset) } } -func TestParseRSSDate(t *testing.T) { +func TestParseRSSDateGMT(t *testing.T) { date, err := Parse("Tue, 03 Jun 2003 09:39:21 GMT") if err != nil { t.Fatalf(`RSS dates should be parsed correctly`) } - if date.Unix() != 1054633161 { - t.Fatal(`Invalid date parsed`) + expectedTS := int64(1054633161) + if date.Unix() != expectedTS { + t.Errorf(`The Unix timestamp should be %v instead of %v`, expectedTS, date.Unix()) + } + + expectedLocation := "GMT" + if date.Location().String() != expectedLocation { + t.Errorf(`The location should be %q instead of %q`, expectedLocation, date.Location()) + } + + name, offset := date.Zone() + + expectedName := "GMT" + if name != expectedName { + t.Errorf(`The zone name should be %q instead of %q`, expectedName, name) + } + + expectedOffset := 0 + if offset != expectedOffset { + t.Errorf(`The offset should be %v instead of %v`, expectedOffset, offset) + } +} + +func TestParseRSSDatePST(t *testing.T) { + date, err := Parse("Wed, 26 Dec 2018 10:00:54 PST") + if err != nil { + t.Fatalf(`RSS dates with PST timezone should be parsed correctly: %v`, err) + } + + expectedTS := int64(1545847254) + if date.Unix() != expectedTS { + t.Errorf(`The Unix timestamp should be %v instead of %v`, expectedTS, date.Unix()) + } + + expectedLocation := "America/Los_Angeles" + if date.Location().String() != expectedLocation { + t.Errorf(`The location should be %q instead of %q`, expectedLocation, date.Location()) + } + + name, offset := date.Zone() + + expectedName := "PST" + if name != expectedName { + t.Errorf(`The zone name should be %q instead of %q`, expectedName, name) + } + + expectedOffset := -28800 + if offset != expectedOffset { + t.Errorf(`The offset should be %v instead of %v`, expectedOffset, offset) + } +} + +func TestParseRSSDateOffset(t *testing.T) { + date, err := Parse("Sun, 28 Oct 2018 13:48:00 +0100") + if err != nil { + t.Fatalf(`RSS dates with offset should be parsed correctly: %v`, err) + } + + expectedTS := int64(1540730880) + if date.Unix() != expectedTS { + t.Errorf(`The Unix timestamp should be %v instead of %v`, expectedTS, date.Unix()) + } + + _, offset := date.Zone() + expectedOffset := 3600 + if offset != expectedOffset { + t.Errorf(`The offset should be %v instead of %v`, expectedOffset, offset) } }