Просмотр исходного кода

fix(date): correct feed date parsing and reorganize the parser

Fix swapped day and month, lost minutes, ignored a.m./p.m., wrong or
dropped offsets, French months and year-0 dates.

Split the parser into layout, translation and time zone files, and
remove 33 unreachable layouts.
Fred 1 час назад
Родитель
Сommit
6b52ee50eb

+ 213 - 0
internal/reader/date/layouts.go

@@ -0,0 +1,213 @@
+// SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
+// SPDX-License-Identifier: Apache-2.0
+
+package date // import "miniflux.app/v2/internal/reader/date"
+
+import "time"
+
+// layouts lists the date formats found in feeds, after the replacer has
+// translated localized names. Parse tries them in order and keeps the first one
+// that matches.
+//
+// The order only matters between layouts that can match the same input. When a
+// day-first and a month-first layout differ only in that order, the day-first
+// one comes first, so "02/01/2006" reads as 2 January.
+//
+// When adding a layout, keep in mind how time.Parse reads it:
+//   - "2", "1", "3", "4" and "5" accept one or two digits, so a "02" copy of an
+//     earlier "2" layout is never reached.
+//   - A space matches any run of spaces.
+//   - Day and month names match in any case.
+//   - Anything that is not a layout element must appear as is: "14", "6" and
+//     "-700" are literal text, not an hour, a year or an offset.
+//
+// Add a test case to validDates for each new layout.
+var layouts = [...]string{
+	// Go standard layouts.
+	time.RFC822, // RSS
+	time.RFC850,
+	time.RFC1123,
+	time.RFC822Z, // RSS
+	time.RFC3339, // Atom
+	time.UnixDate,
+	time.RubyDate,
+	time.RFC1123Z,
+	time.ANSIC,
+
+	// Abbreviated weekday, then month name and day.
+	"Mon, January 2, 2006, 3:04 PM MST",
+	"Mon, January 2 2006 15:04:05 -0700",
+	"Mon, January 02, 2006, 15:04:05 MST",
+	"Mon, January 02, 2006 15:04:05 MST",
+	"Mon, Jan 2, 2006 15:04 MST",
+	"Mon, Jan 2 2006 15:04 MST",
+	"Mon, Jan 2 2006 15:04:05 MST",
+	"Mon, Jan 2, 2006 15:04:05 MST",
+	"Mon, Jan 2 2006 15:04:05 -0700",
+	"Mon Jan 2 15:04 2006",
+	"Mon Jan 2 15:04:05 2006 MST",
+	"Mon Jan 02, 2006 3:04 pm",
+	"Mon, Jan 02,2006 15:04:05 MST",
+	"Mon Jan 02 2006 15:04:05 -0700",
+	"Mon Jan 02 2006 15:04:05 MST",
+	"Mon, January 2, 2006 at 03:04 PM MST",
+	"Mon, Jan 02 2006 03:04:05 PM",
+
+	// Abbreviated weekday, then day and month name.
+	"Mon, 02 Jan 2006 15:04:05 MST -07:00",
+	"Mon, 2 January 2006 15:04 MST",
+	"Mon, 2 January 2006, 15:04 -0700",
+	"Mon, 2 January 2006, 15:04:05 MST",
+	"Mon, 2 January 2006 15:04:05 MST",
+	"Mon, 2 January 2006 15:04:05 -0700",
+	"Mon, 2 January 2006",
+	"Mon, 2 Jan 2006 3:04:05 PM -0700",
+	"Mon, 2 Jan 2006 15:4:5 MST",
+	"Mon, 2 Jan 2006 15:4:5 -0700 GMT",
+	"Mon, 2, Jan 2006 15:4",
+	"Mon, 2 Jan 2006 15:04 MST",
+	"Mon, 2 Jan 2006, 15:04 -0700",
+	"Mon, 2 Jan 2006 15:04 -0700",
+	"Mon, 2 Jan 2006 15:04:05 UT",
+	"Mon, 2 Jan 2006 15:04:05MST",
+	"Mon 2 Jan 2006 15:04:05 MST",
+	"Mon,2 Jan 2006 15:04:05 MST",
+	"Mon, 2 Jan 2006 15:04:05 -0700 MST",
+	"Mon, 2 Jan 2006 15:04:05-0700",
+	"Mon, 2 Jan 2006 15:04:05 -0700",
+	"Mon, 2 Jan 2006 15:04:05",
+	"Mon, 2 Jan 2006 15:04",
+	"Mon, 02 Jan 2006, 15:04",
+	"Mon, 2 Jan 2006, 15:04",
+	"Mon,2 Jan 2006",
+	"Mon, 2 Jan 2006",
+	"Mon, 2 Jan 06 15:04:05 MST",
+	"Mon, 2 Jan 06 15:04:05 -0700",
+	"Mon,02 January 2006 15:04:05 MST",
+	"Mon, 02 Jan 2006 3:04:05 PM MST",
+	"Mon, 02 Jan 2006 15 -0700",
+	"Mon,02 Jan 2006 15:04 MST",
+	"Mon, 02 Jan 2006 15:04:05 Z",
+	"Mon, 02 Jan 2006 15:04:05 MST-07:00",
+	"Mon, 02 Jan 2006 15:04:05 MST -0700",
+	"Mon, 02 Jan 2006, 15:04:05 MST",
+	"Mon , 02 Jan 2006 15:04:05 MST",
+	"Mon, 02 Jan 2006 15:04:05 GMT-0700",
+	"Mon,02 Jan 2006 15:04:05 -0700",
+	"Mon, 02 Jan 2006 15:04:05 -07:00",
+	"Mon, 02 Jan 2006 15:04:05 --0700",
+	"Mon 02 Jan 2006 15:04:05 -0700",
+	"Mon 02 Jan 2006, 15:04:05 MST",
+	"Mon, 02 Jan 2006 15:04:05 -07",
+	"Mon, 02 Jan 2006 15:04:05 00",
+	"Mon, 02 Jan 2006 3:04 PM MST",
+	"Mon, 2th Jan 2006 15:04:05 MST",
+	"Mon, 2rd Jan 2006 15:04:05 MST",
+	"Mon, 2nd Jan 2006 15:04:05 MST",
+	"Mon, 2st Jan 2006 15:04:05 MST",
+
+	// Abbreviated weekday, then numeric date.
+	"Mon, 02/01/2006",
+	"Mon, 01/02/2006 - 15:04",
+	"Mon, 2006-01-02 15:04",
+	"Mon, 02 01 2006 15:04:05 -0700",
+
+	// Full weekday.
+	"Monday, 2. January 2006 - 15:04",
+	"Monday 02 January 2006",
+	"Monday, January 2, 2006 15:04:05 MST",
+	"Monday, January 2, 2006 03:04 PM",
+	"Monday, January 2, 2006",
+	"Monday, 2 January 2006 15:04:05 MST",
+	"Monday, 2 January 2006 15:04:05 -0700",
+	"Monday, 2 Jan 2006 15:04:05 MST",
+	"Monday, 2 Jan 2006 15:04:05 -0700",
+	"Monday, 02 January 2006 15:04:05",
+	"Monday, January 02, 2006 - 3:04pm",
+	"Monday, January 2, 2006 - 3:04pm",
+	"Monday, January 2, 2006 - 15:04",
+
+	// Month name first.
+	"Jan. 2, 2006, 3:04 PM",
+	"January 02 2006 03:04:05 PM",
+	"January 2, 2006 3:04 PM",
+	"January 2, 2006, 3:04 PM",
+	"January 2, 2006 15:04:05 MST",
+	"January 2, 2006 15:04:05",
+	"January 2, 2006",
+	"January 02, 2006 15:04",
+	"Jan 2, 2006 3:04:05 PM MST",
+	"Jan 2, 2006 3:04:05 PM",
+	"Jan 2, 2006 15:04:05 MST",
+	"Jan 2, 2006",
+	"Jan 02 2006 03:04:05PM",
+	"Jan. 2006",
+	"Jan 2, 2006 15:04 MST",
+
+	// Day, then month name.
+	"2 January 2006 15:04:05 MST",
+	"2 January 2006 15:04:05 -0700",
+	"2 January 2006",
+	"2 Jan 2006 15:04:05 Z",
+	"2 Jan 2006 15:04:05 MST",
+	"2 Jan 2006 15:04:05 -0700",
+	"2 Jan 2006",
+	"2 Jan 2006 15:04 MST",
+	"2006 January 02",
+	"02 Monday, Jan 2006 15:04",
+	"02 Jan 2006 15:04:05 UT",
+	"02 Jan 2006 15:04:05",
+	"2 January, 2006",
+	"2 Jan 2006 MST",
+
+	// Year first.
+	"2006-1-2T15:04:05Z",
+	"2006-1-2 15:04:05",
+	"2006-1-2",
+	"2006-01-02T15:04:05-07:00Z",
+	"2006-01-02T15:04Z",
+	"2006-01-02T15:04-07:00",
+	"2006-01-02T15:04:05-07:00:00",
+	"2006-01-02T15:04:05:-0700",
+	"2006-01-02T15:04:05-0700",
+	"2006-01-02T15:04:05-07:00",
+	"2006-01-02T15:04:05 -0700",
+	"2006-01-02T15:04:05:00",
+	"2006-01-02T15:04:05",
+	"2006-01-02T15:04",
+	"2006-01-02 at 15:04:05",
+	"2006-01-02 15:04:05Z",
+	"2006-01-02 15:04:05 MST",
+	"2006-01-02 15:04:05-0700",
+	"2006-01-02 15:04:05-07:00",
+	"2006-01-02 15:04:05 -0700",
+	"2006-01-02 15:04",
+	"2006-01-02 00:00:00.0 15:04:05.0 -0700",
+	"2006/01/02",
+	"2006-01-02 15:04:05 -07:00",
+
+	// Numeric dates, and time first.
+	"2.1.2006 15:04:05",
+	"2/1/2006",
+	"2-1-2006",
+	"15:04 02.01.2006 -0700",
+	"1/2/2006 3:04 PM MST",
+	"1/2/2006 3:04:05 PM MST",
+	"1/2/2006 3:04:05 PM",
+	"1/2/2006 15:04:05 MST",
+	"1/2/2006",
+	"06/1/2 15:04",
+	"06-1-2 15:04",
+	"02/01/2006 15:04 MST",
+	"02-01-2006 15:04:05 MST",
+	"02/01/2006 15:04:05",
+	"02.01.2006 15:04",
+	"02/01/2006 - 15:04",
+	"02.01.2006 -0700",
+	"01/02/2006 3:04 PM",
+	"01/02/2006 - 15:04",
+	"01-02-2006",
+	"01/02/2006 3:04 pm MST",
+	"01/02/06 15:04:05",
+	"02.01.06",
+}

+ 26 - 348
internal/reader/date/parser.go

@@ -11,337 +11,44 @@ import (
 	"time"
 )
 
-// RFC822, RFC850, and RFC1123 formats should be applied only to local times.
-var dateFormatsLocalTimesOnly = [...]string{
-	time.RFC822, // RSS
-	time.RFC850,
-	time.RFC1123,
-}
-
-// dateFormats taken from github.com/mjibson/goread
-var dateFormats = [...]string{
-	time.RFC822Z, // RSS
-	time.RFC3339, // Atom
-	time.UnixDate,
-	time.RubyDate,
-	time.RFC1123Z,
-	time.ANSIC,
-	"Mon, 02 Jan 2006 15:04:05 MST -07:00",
-	"Mon, January 2, 2006, 3:04 PM MST",
-	"Mon, January 2 2006 15:04:05 -0700",
-	"Mon, January 02, 2006, 15:04:05 MST",
-	"Mon, January 02, 2006 15:04:05 MST",
-	"Mon, Jan 2, 2006 15:04 MST",
-	"Mon, Jan 2 2006 15:04 MST",
-	"Mon, Jan 2 2006 15:04:05 MST",
-	"Mon, Jan 2, 2006 15:04:05 MST",
-	"Mon, Jan 2 2006 15:04:05 -700",
-	"Mon, Jan 2 2006 15:04:05 -0700",
-	"Mon Jan 2 15:04 2006",
-	"Mon Jan 2 15:04:05 2006 MST",
-	"Mon Jan 02, 2006 3:04 pm",
-	"Mon, Jan 02,2006 15:04:05 MST",
-	"Mon Jan 02 2006 15:04:05 -0700",
-	"Mon, 02/01/2006",
-	"Monday, 2. January 2006 - 15:04",
-	"Monday 02 January 2006",
-	"Monday, January 2, 2006 15:04:05 MST",
-	"Monday, January 2, 2006 03:04 PM",
-	"Monday, January 2, 2006",
-	"Monday, January 02, 2006",
-	"Monday, 2 January 2006 15:04:05 MST",
-	"Monday, 2 January 2006 15:04:05 -0700",
-	"Monday, 2 Jan 2006 15:04:05 MST",
-	"Monday, 2 Jan 2006 15:04:05 -0700",
-	"Monday, 02 January 2006 15:04:05 MST",
-	"Monday, 02 January 2006 15:04:05 -0700",
-	"Monday, 02 January 2006 15:04:05",
-	"Monday, January 02, 2006 - 3:04pm",
-	"Monday, January 2, 2006 - 3:04pm",
-	"Mon, 01/02/2006 - 15:04",
-	"Mon, 2 January 2006 15:04 MST",
-	"Mon, 2 January 2006, 15:04 -0700",
-	"Mon, 2 January 2006, 15:04:05 MST",
-	"Mon, 2 January 2006 15:04:05 MST",
-	"Mon, 2 January 2006 15:04:05 -0700",
-	"Mon, 2 January 2006",
-	"Mon, 2 Jan 2006 3:04:05 PM -0700",
-	"Mon, 2 Jan 2006 15:4:5 MST",
-	"Mon, 2 Jan 2006 15:4:5 -0700 GMT",
-	"Mon, 2, Jan 2006 15:4",
-	"Mon, 2 Jan 2006 15:04 MST",
-	"Mon, 2 Jan 2006, 15:04 -0700",
-	"Mon, 2 Jan 2006 15:04 -0700",
-	"Mon, 2 Jan 2006 15:04:05 UT",
-	"Mon, 2 Jan 2006 15:04:05MST",
-	"Mon, 2 Jan 2006 15:04:05 MST",
-	"Mon 2 Jan 2006 15:04:05 MST",
-	"mon,2 Jan 2006 15:04:05 MST",
-	"Mon, 2 Jan 2006 15:04:05 -0700 MST",
-	"Mon, 2 Jan 2006 15:04:05-0700",
-	"Mon, 2 Jan 2006 15:04:05 -0700",
-	"Mon, 2 Jan 2006 15:04:05",
-	"Mon, 2 Jan 2006 15:04",
-	"Mon, 02 Jan 2006, 15:04",
-	"Mon, 2 Jan 2006, 15:04",
-	"Mon,2 Jan 2006",
-	"Mon, 2 Jan 2006",
-	"Mon, 2 Jan 15:04:05 MST",
-	"Mon, 2 Jan 06 15:04:05 MST",
-	"Mon, 2 Jan 06 15:04:05 -0700",
-	"Mon, 2006-01-02 15:04",
-	"Mon,02 January 2006 14:04:05 MST",
-	"Mon, 02 January 2006",
-	"Mon, 02 Jan 2006 3:04:05 PM MST",
-	"Mon, 02 Jan 2006 15 -0700",
-	"Mon,02 Jan 2006 15:04 MST",
-	"Mon, 02 Jan 2006 15:04 MST",
-	"Mon, 02 Jan 2006 15:04 -0700",
-	"Mon, 02 Jan 2006 15:04:05 Z",
-	"Mon, 02 Jan 2006 15:04:05 UT",
-	"Mon, 02 Jan 2006 15:04:05 MST-07:00",
-	"Mon, 02 Jan 2006 15:04:05 MST -0700",
-	"Mon, 02 Jan 2006, 15:04:05 MST",
-	"Mon, 02 Jan 2006 15:04:05MST",
-	"Mon, 02 Jan 2006 15:04:05 MST",
-	"Mon , 02 Jan 2006 15:04:05 MST",
-	"Mon, 02 Jan 2006 15:04:05 GMT-0700",
-	"Mon,02 Jan 2006 15:04:05 -0700",
-	"Mon, 02 Jan 2006 15:04:05 -0700",
-	"Mon, 02 Jan 2006 15:04:05 -07:00",
-	"Mon, 02 Jan 2006 15:04:05 --0700",
-	"Mon 02 Jan 2006 15:04:05 -0700",
-	"Mon 02 Jan 2006, 15:04:05 MST",
-	"Mon, 02 Jan 2006 15:04:05 MST",
-	"Mon, 02 Jan 2006 15:04:05 -07",
-	"Mon, 02 Jan 2006 15:04:05 00",
-	"Mon, 02 Jan 2006 15:04:05",
-	"Mon, 02 Jan 2006",
-	"Mon, 02 Jan 06 15:04:05 MST",
-	"Mon, 02 Jan 2006 3:04 PM MST",
-	"Mon Jan 02 2006 15:04:05 MST",
-	"Mon, 01 02 2006 15:04:05 -0700",
-	"Mon, 2th Jan 2006 15:05:05 MST",
-	"Jan. 2, 2006, 3:04 a.m.",
-	"fri, 02 jan 2006 15:04:05 -0700",
-	"January 02 2006 03:04:05 PM",
-	"January 2, 2006 3:04 PM",
-	"January 2, 2006, 3:04 p.m.",
-	"January 2, 2006 15:04:05 MST",
-	"January 2, 2006 15:04:05",
-	"January 2, 2006 03:04 PM",
-	"January 2, 2006",
-	"January 02, 2006 15:04:05 MST",
-	"January 02, 2006 15:04",
-	"January 02, 2006 03:04 PM",
-	"January 02, 2006",
-	"Jan 2, 2006 3:04:05 PM MST",
-	"Jan 2, 2006 3:04:05 PM",
-	"Jan 2, 2006 15:04:05 MST",
-	"Jan 2, 2006",
-	"Jan 02 2006 03:04:05PM",
-	"Jan 02, 2006",
-	"6/1/2 15:04",
-	"6-1-2 15:04",
-	"2 January 2006 15:04:05 MST",
-	"2 January 2006 15:04:05 -0700",
-	"2 January 2006",
-	"2 Jan 2006 15:04:05 Z",
-	"2 Jan 2006 15:04:05 MST",
-	"2 Jan 2006 15:04:05 -0700",
-	"2 Jan 2006",
-	"2 Jan 2006 15:04 MST",
-	"2.1.2006 15:04:05",
-	"2/1/2006",
-	"2-1-2006",
-	"2006 January 02",
-	"2006-1-2T15:04:05Z",
-	"2006-1-2 15:04:05",
-	"2006-1-2",
-	"2006-01-02T15:04:05-07:00Z",
-	"2006-1-02T15:04:05Z",
-	"2006-01-02T15:04Z",
-	"2006-01-02T15:04-07:00",
-	"2006-01-02T15:04:05Z",
-	"2006-01-02T15:04:05-07:00:00",
-	"2006-01-02T15:04:05:-0700",
-	"2006-01-02T15:04:05-0700",
-	"2006-01-02T15:04:05-07:00",
-	"2006-01-02T15:04:05 -0700",
-	"2006-01-02T15:04:05:00",
-	"2006-01-02T15:04:05",
-	"2006-01-02T15:04",
-	"2006-01-02 at 15:04:05",
-	"2006-01-02 15:04:05Z",
-	"2006-01-02 15:04:05 MST",
-	"2006-01-02 15:04:05-0700",
-	"2006-01-02 15:04:05-07:00",
-	"2006-01-02 15:04:05 -0700",
-	"2006-01-02 15:04",
-	"2006-01-02 00:00:00.0 15:04:05.0 -0700",
-	"2006/01/02",
-	"2006-01-02",
-	"15:04 02.01.2006 -0700",
-	"1/2/2006 3:04 PM MST",
-	"1/2/2006 3:04:05 PM MST",
-	"1/2/2006 3:04:05 PM",
-	"1/2/2006 15:04:05 MST",
-	"1/2/2006",
-	"06/1/2 15:04",
-	"06-1-2 15:04",
-	"02 Monday, Jan 2006 15:04",
-	"02 Jan 2006 15:04 MST",
-	"02 Jan 2006 15:04:05 UT",
-	"02 Jan 2006 15:04:05 MST",
-	"02 Jan 2006 15:04:05 -0700",
-	"02 Jan 2006 15:04:05",
-	"02 Jan 2006",
-	"02/01/2006 15:04 MST",
-	"02-01-2006 15:04:05 MST",
-	"02.01.2006 15:04:05",
-	"02/01/2006 15:04:05",
-	"02.01.2006 15:04",
-	"02/01/2006 - 15:04",
-	"02.01.2006 -0700",
-	"02/01/2006",
-	"02-01-2006",
-	"01/02/2006 3:04 PM",
-	"01/02/2006 15:04:05 MST",
-	"01/02/2006 - 15:04",
-	"01/02/2006",
-	"01-02-2006",
-	"Jan. 2006",
-	"Jan. 2, 2006, 03:04 p.m.",
-	"2006-01-02 15:04:05 -07:00",
-	"2 January, 2006",
-	"2 Jan 2006 MST",
-	"Mon, January 2, 2006 at 03:04 PM MST",
-	"Jan 2, 2006 15:04 MST",
-	"01/02/2006 3:04 pm MST",
-	"Mon, 2th Jan 2006 15:04:05 MST",
-	"Mon, 2rd Jan 2006 15:04:05 MST",
-	"Mon, 2nd Jan 2006 15:04:05 MST",
-	"Mon, 2st Jan 2006 15:04:05 MST",
-	"Mon, Jan 02 2006 03:04:05 PM",
-	"Monday, January 2, 2006 - 15:04",
-	"01/02/06 15:04:05",
-	"02.01.06",
-}
+// Parse parses a date found in a feed: a Unix timestamp, or a date in one of the
+// many formats listed in layouts, possibly with localized names.
+func Parse(rawInput string) (time.Time, error) {
+	input := strings.TrimSpace(rawInput)
+	if input == "" {
+		return time.Time{}, errors.New(`date parser: empty value`)
+	}
 
-var replacer = strings.NewReplacer(
-	// Timezones
-	"Europe/Brussels", "CET",
-	"America/Los_Angeles", "PDT",
-	"GMT+0000 (Coordinated Universal Time)", "GMT",
-	"GMT-", "GMT -",
+	if timestamp, err := strconv.ParseInt(input, 10, 64); err == nil {
+		return time.Unix(timestamp, 0), nil
+	}
 
-	// Localized dates
-	"Mo,", "Mon,",
-	"Di,", "Tue,",
-	"Mi,", "Wed,",
-	"Do,", "Thu,",
-	"Fr,", "Fri,",
-	"Sa,", "Sat,",
-	"So,", "Sun,",
-	"Mär ", "Mar ",
-	"Mai ", "May ",
-	"Okt ", "Oct ",
-	"Dez ", "Dec ",
-	"lun,", "Mon,",
-	"mar,", "Tue,",
-	"mer,", "Wed,",
-	"jeu,", "Thu,",
-	"ven,", "Fri,",
-	"sam,", "Sat,",
-	"dim,", "Sun,",
-	"lun.", "Mon",
-	"mar.", "Tue",
-	"mer.", "Wed",
-	"jeu.", "Thu",
-	"ven.", "Fri",
-	"sam.", "Sat",
-	"dim.", "Sun",
-	"Lundi,", "Monday,",
-	"Mardi,", "Tuesday,",
-	"Mercredi,", "Wednesday,",
-	"Jeudi,", "Thursday,",
-	"Vendredi,", "Friday,",
-	"Samedi,", "Saturday,",
-	"Dimanche,", "Sunday,",
-	"jan.", "January ",
-	"feb.", "February ",
-	"mars.", "March ",
-	"avril.", "April ",
-	"mai.", "May ",
-	"juin.", "June ",
-	"juil.", "July",
-	"août.", "August",
-	"sept.", "September",
-	"oct.", "October",
-	"nov.", "November",
-	"dec.", "December",
-	"déc.", "December",
-	"janvier ", "January ",
-	"février ", "February ",
-	"mars ", "March ",
-	"avril ", "April ",
-	"mai ", "May ",
-	"juin ", "June ",
-	"juillet ", "July",
-	"août ", "August",
-	"septembre ", "September",
-	"octobre ", "October",
-	"november ", "November",
-	"décembre ", "December",
-	"Janvier", "January",
-	"Février", "February",
-	"Mars", "March",
-	"Avril", "April",
-	"Mai", "May",
-	"Juin", "June",
-	"Juillet", "July",
-	"Août", "August",
-	"Septembre", "September",
-	"Octobre", "October",
-	"Novembre", "November",
-	"Décembre", "December",
-	"avr ", "Apr ",
-	"mai ", "May ",
-	"jui ", "Jun ",
-	"juin ", "June ",
-	"Thurs,", "Thu,",
-	"Thur,", "Thu,",
-)
-var losAngelesLocation, _ = time.LoadLocation("America/Los_Angeles")
-var newYorkLocation, _ = time.LoadLocation("America/New_York")
+	value, wallClockLocation := replaceIANATimezone(input)
+	value = padShortOffset(normalizeRFC3339Separators(replacer.Replace(value)))
 
-// Parse parses a given date string using a large
-// list of commonly found feed date formats.
-func Parse(rawInput string) (t time.Time, err error) {
-	rawInput = strings.TrimSpace(rawInput)
-	if rawInput == "" {
-		return t, errors.New(`date parser: empty value`)
+	parsed, ok := parseLayouts(value)
+	if !ok {
+		return time.Time{}, fmt.Errorf(`date parser: failed to parse date "%s"`, input)
 	}
 
-	if timestamp, err := strconv.ParseInt(rawInput, 10, 64); err == nil {
-		return time.Unix(timestamp, 0), nil
+	if wallClockLocation != nil {
+		parsed = withWallClockIn(parsed, wallClockLocation)
 	}
 
-	processedInput := normalizeRFC3339Separators(replacer.Replace(rawInput))
+	return checkTimezoneRange(parsed), nil
+}
 
-	for _, layout := range dateFormatsLocalTimesOnly {
-		if t, err = parseLocalTimeDates(layout, processedInput); err == nil {
-			return checkTimezoneRange(t), nil
-		}
-	}
+// parseLayouts parses the value with the first layout that matches it.
+func parseLayouts(value string) (time.Time, bool) {
+	location := timezoneLocation(value)
 
-	for _, layout := range dateFormats {
-		if t, err = time.Parse(layout, processedInput); err == nil {
-			return checkTimezoneRange(t), nil
+	for _, layout := range layouts {
+		if parsed, err := time.ParseInLocation(layout, value, location); err == nil {
+			return parsed, true
 		}
 	}
 
-	return t, fmt.Errorf(`date parser: failed to parse date "%s"`, rawInput)
+	return time.Time{}, false
 }
 
 // normalizeRFC3339Separators upper-cases the "t" separator and the "z" time zone
@@ -359,32 +66,3 @@ func normalizeRFC3339Separators(value string) string {
 
 	return value
 }
-
-// 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") || strings.HasSuffix(ds, "PDT") {
-		loc = losAngelesLocation
-	} else if strings.HasSuffix(ds, "EST") || strings.HasSuffix(ds, "EDT") {
-		loc = newYorkLocation
-	}
-
-	return time.ParseInLocation(layout, ds, loc)
-}
-
-// https://en.wikipedia.org/wiki/List_of_UTC_offsets
-// Offset range: westernmost (−12:00) to the easternmost (+14:00)
-// Avoid "pq: time zone displacement out of range" errors
-func checkTimezoneRange(t time.Time) time.Time {
-	_, offset := t.Zone()
-	if offset > 14*60*60 || offset < -12*60*60 {
-		t = t.UTC()
-	}
-	return t
-}

+ 370 - 224
internal/reader/date/parser_test.go

@@ -4,287 +4,433 @@
 package date // import "miniflux.app/v2/internal/reader/date"
 
 import (
+	"strings"
 	"testing"
+	"time"
 )
 
-func FuzzParse(f *testing.F) {
-	f.Add("2017-12-22T22:09:49+00:00")
-	f.Add("Fri, 31 Mar 2023 20:19:00 America/Los_Angeles")
-	f.Fuzz(func(t *testing.T, date string) {
-		Parse(date)
-	})
-}
+// validDates lists inputs that Parse must accept, with the instant each one
+// represents, written in RFC 3339 with the offset given by the input.
+var validDates = []struct {
+	input string
+	want  string
+}{
+	// Standard formats.
+	{"2017-12-22T22:09:49+00:00", "2017-12-22T22:09:49Z"},
+	{"1985-04-12T23:20:50.52Z", "1985-04-12T23:20:50.52Z"},
+	{"03 Jun 03 09:39 GMT", "2003-06-03T09:39:00Z"},
+	{"03 Jun 03 09:39 -0700", "2003-06-03T09:39:00-07:00"},
+	{"Tuesday, 03-Jun-03 09:39:21 GMT", "2003-06-03T09:39:21Z"},
+	{"Tue, 03 Jun 2003 09:39:21 GMT", "2003-06-03T09:39:21Z"},
+	{"Sun, 28 Oct 2018 13:48:00 +0100", "2018-10-28T13:48:00+01:00"},
+	{"Wed Feb 10 22:46:00 UTC 2021", "2021-02-10T22:46:00Z"},
+	{"Wed Feb 10 22:46:00 +0100 2021", "2021-02-10T22:46:00+01:00"},
+	{"Wed Feb 10 22:46:00 2021", "2021-02-10T22:46:00Z"},
 
-func TestParseEmptyDate(t *testing.T) {
-	if _, err := Parse("  "); err == nil {
-		t.Fatalf(`Empty dates should return an error`)
-	}
+	// RFC 3339 section 5.6 allows "T" and "Z" to be lower case.
+	{"2003-12-13t18:30:02z", "2003-12-13T18:30:02Z"},
+	{"2003-12-13T18:30:02z", "2003-12-13T18:30:02Z"},
+	{"2003-12-13t18:30:02Z", "2003-12-13T18:30:02Z"},
+	{"1985-04-12t23:20:50.52z", "1985-04-12T23:20:50.52Z"},
+	{"1996-12-19t16:39:57-08:00", "1996-12-19T16:39:57-08:00"},
+	// The separators are looked for at fixed positions, after the input is trimmed.
+	{" 2003-12-13t18:30:02z\n", "2003-12-13T18:30:02Z"},
+
+	// Unix timestamps.
+	{"1520932969", "2018-03-13T09:22:49Z"},
+	{"0", "1970-01-01T00:00:00Z"},
+	{"-1", "1969-12-31T23:59:59Z"},
+
+	// Time zone abbreviations and names.
+	{"Wed, 26 Dec 2018 10:00:54 PST", "2018-12-26T10:00:54-08:00"},
+	{"Tue, 26 Jun 2018 10:00:54 PDT", "2018-06-26T10:00:54-07:00"},
+	{"Wed, 10 Feb 2021 22:46:00 EST", "2021-02-10T22:46:00-05:00"},
+	{"Thu, 10 Jun 2021 22:46:00 EDT", "2021-06-10T22:46:00-04:00"},
+	{"Fri, 31 Mar 2023 20:19:00 America/Los_Angeles", "2023-03-31T20:19:00-07:00"},
+	{"Tue, 31 Jan 2023 20:19:00 America/Los_Angeles", "2023-01-31T20:19:00-08:00"},
+	{"Thu, 25 Feb 2016 00:00:00 Europe/Brussels", "2016-02-25T00:00:00+01:00"},
+	{"Mon, 25 Jul 2016 00:00:00 Europe/Brussels", "2016-07-25T00:00:00+02:00"},
+
+	// Non-standard formats found in feeds.
+	{"Sun, 17 Dec 2017 1:55 PM EST", "2017-12-17T13:55:00-05:00"},
+	{"9 Dec 2016 12:00 GMT", "2016-12-09T12:00:00Z"},
+	{"Friday, December 22, 2017 - 3:09pm", "2017-12-22T15:09:00Z"},
+	{"Friday, December 8, 2017 - 3:07pm", "2017-12-08T15:07:00Z"},
+	{"Mon, 09 Apr 2018, 16:04", "2018-04-09T16:04:00Z"},
+	{"Tue 16 Feb 2016, 23:16:00 EDT", "2016-02-16T23:16:00-04:00"},
+	{"Tue, 16 Feb 2016 23:16:00 EDT", "2016-02-16T23:16:00-04:00"},
+	{"Tue, Feb 16 2016 23:16:00 EDT", "2016-02-16T23:16:00-04:00"},
+	{"March 30 2020 07:02:38 PM", "2020-03-30T19:02:38Z"},
+	{"Mon, 30 Mar 2020 19:53 +0000", "2020-03-30T19:53:00Z"},
+	{"Mon, 03/30/2020 - 19:19", "2020-03-30T19:19:00Z"},
+	{"2018-12-12T12:12", "2018-12-12T12:12:00Z"},
+	{"2020-11-08T16:20:00-05:00Z", "2020-11-08T16:20:00-05:00"},
+	{"Friday 06 November 2020", "2020-11-06T00:00:00Z"},
+	{"Mon, November 16, 2020, 11:12 PM EST", "2020-11-16T23:12:00-05:00"},
+	{"Thu Nov 12 2020 17:00:00 GMT+0000 (Coordinated Universal Time)", "2020-11-12T17:00:00Z"},
+	{"Sat, 11 04 2020 08:51:49 +0100", "2020-04-11T08:51:49+01:00"},
+	{"Mon, 16th Nov 2020 13:16:28 GMT", "2020-11-16T13:16:28Z"},
+	{"Nov. 2020", "2020-11-01T00:00:00Z"},
+	{"Fri, 26/06/2020", "2020-06-26T00:00:00Z"},
+	{"Thu, 29 Oct 2020 07:36:03 GMT-07:00", "2020-10-29T07:36:03-07:00"},
+	{"2018-10-23 04:07:42 +00:00", "2018-10-23T04:07:42Z"},
+	{"5 August, 2019", "2019-08-05T00:00:00Z"},
+	{"Thurs, 15 Oct 2020 00:00:39 +0000", "2020-10-15T00:00:39Z"},
+	{"Thur, 19 Nov 2020 00:00:39 +0000", "2020-11-19T00:00:39Z"},
+	{"26 Sep 2022 GMT", "2022-09-26T00:00:00Z"},
+	{"Thu, June 22, 2023 at 01:11 PM EDT", "2023-06-22T13:11:00-04:00"},
+	{"Apr 16, 2023 08:01 GMT", "2023-04-16T08:01:00Z"},
+	{"Jun 23, 2023 19:00 GMT", "2023-06-23T19:00:00Z"},
+	{"09/15/2014 4:20 pm PST", "2014-09-15T16:20:00-08:00"},
+	{"Fri, 23rd Jun 2023 09:32:20 GMT", "2023-06-23T09:32:20Z"},
+	{"Sat, Oct 28 2023 08:28:28 PM", "2023-10-28T20:28:28Z"},
+	{"Monday, October 6, 2023 - 16:29\n", "2023-10-06T16:29:00Z"},
+	{"10/30/23 21:55:58", "2023-10-30T21:55:58Z"},
+	{"30.10.23", "2023-10-30T00:00:00Z"},
+	{"Mon,02 March 2020 10:30:00 GMT", "2020-03-02T10:30:00Z"},
+	{"tue,2 Jan 2018 10:00:00 GMT", "2018-01-02T10:00:00Z"},
+
+	// Three-digit offsets.
+	{"Tue, Feb 16 2016 23:16:00 -700", "2016-02-16T23:16:00-07:00"},
+	{"Sun, 28 Oct 2018 13:48:00 +100", "2018-10-28T13:48:00+01:00"},
+
+	// Spelled out a.m. and p.m.
+	{"Nov. 16, 2020, 10:57 a.m.", "2020-11-16T10:57:00Z"},
+	{"Jan. 4, 2016, 12:37 p.m.", "2016-01-04T12:37:00Z"},
+	{"Jan. 4, 2016, 1:37 p.m.", "2016-01-04T13:37:00Z"},
+	{"Jan. 4, 2016, 01:37 p.m.", "2016-01-04T13:37:00Z"},
+	{"Jan. 4, 2016, 12:37 a.m.", "2016-01-04T00:37:00Z"},
+	{"November 16, 2020, 3:04 a.m.", "2020-11-16T03:04:00Z"},
+	{"November 16, 2020, 3:04 p.m.", "2020-11-16T15:04:00Z"},
+
+	// German dates.
+	{"Di, 23 Jan 2018 00:00:00 +0100", "2018-01-23T00:00:00+01:00"},
+	{"Do, 29 Mär 2018 00:00:00 +0200", "2018-03-29T00:00:00+02:00"},
+	{"Mo, 02 Mai 2022 10:00:00 +0200", "2022-05-02T10:00:00+02:00"},
+	{"Fr, 01 Okt 2021 10:00:00 +0200", "2021-10-01T10:00:00+02:00"},
+	{"So, 05 Dez 2021 10:00:00 +0100", "2021-12-05T10:00:00+01:00"},
+
+	// French dates.
+	{"mer, 9 avr 2018 00:00:00 +0200", "2018-04-09T00:00:00+02:00"},
+	{"ven., 03 juil. 2020 15:09:58 +0000", "2020-07-03T15:09:58Z"},
+	{"jeu., 02 avril 2020 00:00:00 +0200", "2020-04-02T00:00:00+02:00"},
+	{"mar., 01 déc. 2020 16:11:02 +0000", "2020-12-01T16:11:02Z"},
+	{"mer., 08 janvier 2020 10:00:00 +0100", "2020-01-08T10:00:00+01:00"},
+	{"sam., 08 février 2020 10:00:00 +0100", "2020-02-08T10:00:00+01:00"},
+	{"dim., 08 mars 2020 10:00:00 +0100", "2020-03-08T10:00:00+01:00"},
+	{"mer., 08 avril 2020 10:00:00 +0200", "2020-04-08T10:00:00+02:00"},
+	{"ven., 08 mai 2020 10:00:00 +0200", "2020-05-08T10:00:00+02:00"},
+	{"lun., 08 juin 2020 10:00:00 +0200", "2020-06-08T10:00:00+02:00"},
+	{"mer., 08 juillet 2020 10:00:00 +0200", "2020-07-08T10:00:00+02:00"},
+	{"sam., 08 août 2020 10:00:00 +0200", "2020-08-08T10:00:00+02:00"},
+	{"mar., 08 septembre 2020 10:00:00 +0200", "2020-09-08T10:00:00+02:00"},
+	{"jeu., 08 octobre 2020 10:00:00 +0200", "2020-10-08T10:00:00+02:00"},
+	{"dim., 08 novembre 2020 10:00:00 +0100", "2020-11-08T10:00:00+01:00"},
+	{"mar., 08 décembre 2020 10:00:00 +0100", "2020-12-08T10:00:00+01:00"},
+	{"Lundi, 16. Novembre 2020 - 15:54", "2020-11-16T15:54:00Z"},
+	{"Samedi, 05 Juillet 2025 10:00:00 +0200", "2025-07-05T10:00:00+02:00"},
+	{"Dimanche, 06 décembre 2020 10:00:00 +0100", "2020-12-06T10:00:00+01:00"},
 }
 
-func TestParseInvalidDate(t *testing.T) {
-	if _, err := Parse("invalid"); err == nil {
-		t.Fatalf(`Invalid dates should return an error`)
-	}
-}
+func mustParseRFC3339(tb testing.TB, value string) time.Time {
+	tb.Helper()
 
-func TestParseAtomDate(t *testing.T) {
-	date, err := Parse("2017-12-22T22:09:49+00:00")
+	parsed, err := time.Parse(time.RFC3339, value)
 	if err != nil {
-		t.Fatalf(`Atom dates should be parsed correctly`)
-	}
-
-	expectedTS := int64(1513980589)
-	if date.Unix() != expectedTS {
-		t.Errorf(`The Unix timestamp should be %v instead of %v`, expectedTS, date.Unix())
+		tb.Fatalf("Unable to parse RFC 3339 date %q: %v", value, err)
 	}
 
-	_, offset := date.Zone()
-	expectedOffset := 0
-	if offset != expectedOffset {
-		t.Errorf(`The offset should be %v instead of %v`, expectedOffset, offset)
-	}
+	return parsed
 }
 
-func TestParseAtomDateWithLowerCaseSeparators(t *testing.T) {
-	// RFC 3339 section 5.6 allows "T" and "Z" to be lower case.
-	testCases := map[string]int64{
-		"2003-12-13t18:30:02z":      1071340202,
-		"2003-12-13T18:30:02z":      1071340202,
-		"2003-12-13t18:30:02Z":      1071340202,
-		"1985-04-12t23:20:50.52z":   482196050,
-		"1996-12-19t16:39:57-08:00": 851042397,
-	}
-
-	for input, expectedTS := range testCases {
-		date, err := Parse(input)
-		if err != nil {
-			t.Errorf(`Unable to parse date %q: %v`, input, err)
-			continue
-		}
-
-		if date.Unix() != expectedTS {
-			t.Errorf(`The Unix timestamp for %q should be %v instead of %v`, input, expectedTS, date.Unix())
-		}
-	}
-}
+func loadLocation(tb testing.TB, name string) *time.Location {
+	tb.Helper()
 
-func TestParseAtomDateWithLowerCaseSeparatorsAndSurroundingWhitespace(t *testing.T) {
-	// The separators are looked for at fixed positions, after the input is trimmed.
-	date, err := Parse(" 2003-12-13t18:30:02z\n")
+	location, err := time.LoadLocation(name)
 	if err != nil {
-		t.Fatalf(`Unable to parse date: %v`, err)
+		tb.Fatalf("Unable to load location %q: %v", name, err)
 	}
 
-	expectedTS := int64(1071340202)
-	if date.Unix() != expectedTS {
-		t.Errorf(`The Unix timestamp should be %v instead of %v`, expectedTS, date.Unix())
-	}
+	return location
 }
 
-func TestParseRSSDateTimezone(t *testing.T) {
-	date, err := Parse("Fri, 31 Mar 2023 20:19:00 America/Los_Angeles")
-	if err != nil {
-		t.Fatalf(`RSS dates should be parsed correctly`)
-	}
-
-	expectedTS := int64(1680319140)
-	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 := "PDT"
-	if name != expectedName {
-		t.Errorf(`The zone name should be %q instead of %q`, expectedName, name)
-	}
+// setLocalTimezone replaces time.Local until the end of the test. Tests that
+// call it must not run in parallel.
+func setLocalTimezone(t *testing.T, location *time.Location) {
+	t.Helper()
 
-	expectedOffset := -25200
-	if offset != expectedOffset {
-		t.Errorf(`The offset should be %v instead of %v`, expectedOffset, offset)
-	}
+	previous := time.Local
+	time.Local = location
+	t.Cleanup(func() { time.Local = previous })
 }
 
-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`)
+func FuzzParse(f *testing.F) {
+	for _, testCase := range validDates {
+		f.Add(testCase.input)
 	}
 
-	expectedTS := int64(1054633161)
-	if date.Unix() != expectedTS {
-		t.Errorf(`The Unix timestamp should be %v instead of %v`, expectedTS, date.Unix())
-	}
+	f.Fuzz(func(t *testing.T, input string) {
+		parsed, err := Parse(input)
+		if err != nil {
+			return
+		}
 
-	expectedLocation := "GMT"
-	if date.Location().String() != expectedLocation {
-		t.Errorf(`The location should be %q instead of %q`, expectedLocation, date.Location())
-	}
+		if _, offset := parsed.Zone(); offset > 14*60*60 || offset < -12*60*60 {
+			t.Errorf("Parse(%q) returned the out of range offset %d", input, offset)
+		}
+	})
+}
 
-	name, offset := date.Zone()
+func TestParse(t *testing.T) {
+	for _, testCase := range validDates {
+		t.Run(testCase.input, func(t *testing.T) {
+			want := mustParseRFC3339(t, testCase.want)
 
-	expectedName := "GMT"
-	if name != expectedName {
-		t.Errorf(`The zone name should be %q instead of %q`, expectedName, name)
-	}
+			got, err := Parse(testCase.input)
+			if err != nil {
+				t.Fatalf("Unable to parse date %q: %v", testCase.input, err)
+			}
 
-	expectedOffset := 0
-	if offset != expectedOffset {
-		t.Errorf(`The offset should be %v instead of %v`, expectedOffset, offset)
+			if !got.Equal(want) {
+				t.Errorf("Parse(%q) = %s, want %s", testCase.input, got.Format(time.RFC3339Nano), want.Format(time.RFC3339Nano))
+			}
+		})
 	}
 }
 
-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)
+func TestParseInvalidDates(t *testing.T) {
+	inputs := []string{
+		"",
+		"  ",
+		"\t\n",
+		"invalid",
+		"2023-13-01",
+		"Mon, 32 Jan 2006 15:04:05 GMT",
+		"99999999999999999999",
+		// No year.
+		"Tue, 16 Feb 23:16:00 GMT",
+		// One-digit year.
+		"6/1/2 15:04",
 	}
 
-	expectedTS := int64(1545847254)
-	if date.Unix() != expectedTS {
-		t.Errorf(`The Unix timestamp should be %v instead of %v`, expectedTS, date.Unix())
-	}
+	for _, input := range inputs {
+		t.Run(input, func(t *testing.T) {
+			parsed, err := Parse(input)
+			if err == nil {
+				t.Fatalf("Parse(%q) = %s, want an error", input, parsed.Format(time.RFC3339Nano))
+			}
 
-	expectedLocation := "America/Los_Angeles"
-	if date.Location().String() != expectedLocation {
-		t.Errorf(`The location should be %q instead of %q`, expectedLocation, date.Location())
+			if trimmedInput := strings.TrimSpace(input); trimmedInput != "" && !strings.Contains(err.Error(), trimmedInput) {
+				t.Errorf("The error %q should contain the input %q", err, trimmedInput)
+			}
+		})
 	}
+}
 
-	name, offset := date.Zone()
-
-	expectedName := "PST"
-	if name != expectedName {
-		t.Errorf(`The zone name should be %q instead of %q`, expectedName, name)
+func TestLayoutsAreUnique(t *testing.T) {
+	seen := make(map[string]bool, len(layouts))
+	for _, layout := range layouts {
+		if seen[layout] {
+			t.Errorf("The layout %q is listed more than once", layout)
+		}
+		seen[layout] = true
 	}
+}
 
-	expectedOffset := -28800
-	if offset != expectedOffset {
-		t.Errorf(`The offset should be %v instead of %v`, expectedOffset, offset)
+func TestParseTimezone(t *testing.T) {
+	testCases := []struct {
+		input string
+		// wantLocation and wantZone are not checked when empty.
+		wantLocation string
+		wantZone     string
+		wantOffset   int
+	}{
+		{"Tue, 03 Jun 2003 09:39:21 GMT", "GMT", "GMT", 0},
+		{"Wed, 26 Dec 2018 10:00:54 PST", "America/Los_Angeles", "PST", -8 * 60 * 60},
+		{"Tue, 26 Jun 2018 10:00:54 PDT", "America/Los_Angeles", "PDT", -7 * 60 * 60},
+		{"Wed, 10 Feb 2021 22:46:00 EST", "America/New_York", "EST", -5 * 60 * 60},
+		{"Thu, 10 Jun 2021 22:46:00 EDT", "America/New_York", "EDT", -4 * 60 * 60},
+		{"Mon, November 16, 2020, 11:12 PM EST", "America/New_York", "EST", -5 * 60 * 60},
+		{"Fri, 31 Mar 2023 20:19:00 America/Los_Angeles", "America/Los_Angeles", "PDT", -7 * 60 * 60},
+		{"Tue, 31 Jan 2023 20:19:00 America/Los_Angeles", "America/Los_Angeles", "PST", -8 * 60 * 60},
+		{"Thu, 25 Feb 2016 00:00:00 Europe/Brussels", "Europe/Brussels", "CET", 1 * 60 * 60},
+		{"Mon, 25 Jul 2016 00:00:00 Europe/Brussels", "Europe/Brussels", "CEST", 2 * 60 * 60},
+		{"Sun, 28 Oct 2018 13:48:00 +0100", "", "", 1 * 60 * 60},
+		{"2017-12-22T22:09:49+00:00", "", "", 0},
+	}
+
+	for _, testCase := range testCases {
+		t.Run(testCase.input, func(t *testing.T) {
+			parsed, err := Parse(testCase.input)
+			if err != nil {
+				t.Fatalf("Unable to parse date %q: %v", testCase.input, err)
+			}
+
+			if testCase.wantLocation != "" && parsed.Location().String() != testCase.wantLocation {
+				t.Errorf("The location should be %q instead of %q", testCase.wantLocation, parsed.Location())
+			}
+
+			zone, offset := parsed.Zone()
+			if testCase.wantZone != "" && zone != testCase.wantZone {
+				t.Errorf("The zone name should be %q instead of %q", testCase.wantZone, zone)
+			}
+
+			if offset != testCase.wantOffset {
+				t.Errorf("The offset should be %d instead of %d", testCase.wantOffset, offset)
+			}
+		})
 	}
 }
 
-func TestParseRSSDateEST(t *testing.T) {
-	date, err := Parse("Wed, 10 Feb 2021 22:46:00 EST")
-	if err != nil {
-		t.Fatalf(`RSS dates with EST timezone should be parsed correctly: %v`, err)
+func TestParseDoesNotDependOnLocalTimezone(t *testing.T) {
+	// Go resolves a time zone abbreviation with the location the date is parsed
+	// in, which is time.Local for time.Parse.
+	inputs := []string{
+		"Sun, 17 Dec 2017 1:55 PM EST",
+		"Tue 16 Feb 2016, 23:16:00 EDT",
+		"Thu, June 22, 2023 at 01:11 PM EDT",
+		"09/15/2014 4:20 pm PST",
+		"Thu, 25 Feb 2016 00:00 CET",
+		"Fri, 31 Mar 2023 20:19:00 America/Los_Angeles",
 	}
 
-	expectedTS := int64(1613015160)
-	if date.Unix() != expectedTS {
-		t.Errorf(`The Unix timestamp should be %v instead of %v`, expectedTS, date.Unix())
-	}
+	setLocalTimezone(t, time.UTC)
 
-	expectedLocation := "America/New_York"
-	if date.Location().String() != expectedLocation {
-		t.Errorf(`The location should be %q instead of %q`, expectedLocation, date.Location())
+	expected := make(map[string]time.Time, len(inputs))
+	for _, input := range inputs {
+		parsed, err := Parse(input)
+		if err != nil {
+			t.Fatalf("Unable to parse date %q: %v", input, err)
+		}
+		expected[input] = parsed
 	}
 
-	name, offset := date.Zone()
+	for _, name := range []string{"America/Los_Angeles", "America/New_York", "Europe/Paris"} {
+		t.Run(name, func(t *testing.T) {
+			setLocalTimezone(t, loadLocation(t, name))
 
-	expectedName := "EST"
-	if name != expectedName {
-		t.Errorf(`The zone name should be %q instead of %q`, expectedName, name)
-	}
+			for _, input := range inputs {
+				parsed, err := Parse(input)
+				if err != nil {
+					t.Fatalf("Unable to parse date %q: %v", input, err)
+				}
 
-	expectedOffset := -18000
-	if offset != expectedOffset {
-		t.Errorf(`The offset should be %v instead of %v`, expectedOffset, offset)
+				if !parsed.Equal(expected[input]) {
+					t.Errorf("Parse(%q) = %s, want %s as with time.Local set to UTC", input, parsed.Format(time.RFC3339), expected[input].Format(time.RFC3339))
+				}
+			}
+		})
 	}
 }
-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)
+func TestParseTimezoneOutOfRange(t *testing.T) {
+	// Offsets go from -12:00 to +14:00; beyond that the date is converted to UTC.
+	testCases := []struct {
+		input      string
+		want       string
+		wantOffset int
+	}{
+		{"2023-05-29 00:00:00-13:00", "2023-05-29T13:00:00Z", 0},
+		{"2023-05-29 00:00:00+15:00", "2023-05-28T09:00:00Z", 0},
+		{"2023-05-29 00:00:00-12:00", "2023-05-29T12:00:00Z", -12 * 60 * 60},
+		{"2023-05-29 00:00:00+14:00", "2023-05-28T10:00:00Z", 14 * 60 * 60},
+	}
+
+	for _, testCase := range testCases {
+		t.Run(testCase.input, func(t *testing.T) {
+			parsed, err := Parse(testCase.input)
+			if err != nil {
+				t.Fatalf("Unable to parse date %q: %v", testCase.input, err)
+			}
+
+			if want := mustParseRFC3339(t, testCase.want); !parsed.Equal(want) {
+				t.Errorf("Parse(%q) = %s, want %s", testCase.input, parsed.Format(time.RFC3339), want.Format(time.RFC3339))
+			}
+
+			if _, offset := parsed.Zone(); offset != testCase.wantOffset {
+				t.Errorf("The offset should be %d instead of %d", testCase.wantOffset, offset)
+			}
+		})
 	}
 }
 
-func TestParseWeirdDateFormat(t *testing.T) {
-	dates := []string{
-		"Sun, 17 Dec 2017 1:55 PM EST",
-		"9 Dec 2016 12:00 GMT",
-		"Friday, December 22, 2017 - 3:09pm",
-		"Friday, December 8, 2017 - 3:07pm",
-		"Thu, 25 Feb 2016 00:00:00 Europe/Brussels",
-		"Mon, 09 Apr 2018, 16:04",
-		"Di, 23 Jan 2018 00:00:00 +0100",
-		"Do, 29 Mär 2018 00:00:00 +0200",
-		"mer, 9 avr 2018 00:00:00 +0200",
-		"1520932969",
-		"Tue 16 Feb 2016, 23:16:00 EDT",
-		"Tue, 16 Feb 2016 23:16:00 EDT",
-		"Tue, Feb 16 2016 23:16:00 EDT",
-		"March 30 2020 07:02:38 PM",
-		"Mon, 30 Mar 2020 19:53 +0000",
-		"Mon, 03/30/2020 - 19:19",
-		"2018-12-12T12:12",
-		"2020-11-08T16:20:00-05:00Z",
-		"Nov. 16, 2020, 10:57 a.m.",
-		"Friday 06 November 2020",
-		"Mon, November 16, 2020, 11:12 PM EST",
-		"Lundi, 16. Novembre 2020 - 15:54",
-		"Thu Nov 12 2020 17:00:00 GMT+0000 (Coordinated Universal Time)",
-		"Sat, 11 04 2020 08:51:49 +0100",
-		"Mon, 16th Nov 2020 13:16:28 GMT",
-		"Nov. 2020",
-		"ven., 03 juil. 2020 15:09:58 +0000",
-		"Fri, 26/06/2020",
-		"Thu, 29 Oct 2020 07:36:03 GMT-07:00",
-		"jeu., 02 avril 2020 00:00:00 +0200",
-		"Jan. 4, 2016, 12:37 p.m.",
-		"2018-10-23 04:07:42 +00:00",
-		"5 August, 2019",
-		"mar., 01 déc. 2020 16:11:02 +0000",
-		"Thurs, 15 Oct 2020 00:00:39 +0000",
-		"Thur, 19 Nov 2020 00:00:39 +0000",
-		"26 Sep 2022 GMT",
-		"Thu, June 22, 2023 at 01:11 PM EDT",
-		"Apr 16, 2023 08:01 GMT",
-		"Jun 23, 2023 19:00 GMT",
-		"09/15/2014 4:20 pm PST",
-		"Fri, 23rd Jun 2023 09:32:20 GMT",
-		"Sat, Oct 28 2023 08:28:28 PM",
-		"Monday, October 6, 2023 - 16:29\n",
-		"10/30/23 21:55:58",
-		"30.10.23",
-	}
-
-	for _, date := range dates {
-		if _, err := Parse(date); err != nil {
-			t.Errorf(`Unable to parse date: %q (%v)`, date, err)
-		}
+func TestCheckTimezoneRange(t *testing.T) {
+	testCases := []struct {
+		name       string
+		offset     int
+		wantOffset int
+	}{
+		{"UTC", 0, 0},
+		{"easternmost offset", 14 * 60 * 60, 14 * 60 * 60},
+		{"beyond the easternmost offset", 14*60*60 + 60, 0},
+		{"westernmost offset", -12 * 60 * 60, -12 * 60 * 60},
+		{"beyond the westernmost offset", -12*60*60 - 60, 0},
+	}
+
+	for _, testCase := range testCases {
+		t.Run(testCase.name, func(t *testing.T) {
+			input := time.Date(2023, time.May, 29, 0, 0, 0, 0, time.FixedZone("", testCase.offset))
+			got := checkTimezoneRange(input)
+
+			if !got.Equal(input) {
+				t.Errorf("The instant should be kept: got %s, want %s", got.Format(time.RFC3339), input.Format(time.RFC3339))
+			}
+
+			if _, offset := got.Zone(); offset != testCase.wantOffset {
+				t.Errorf("The offset should be %d instead of %d", testCase.wantOffset, offset)
+			}
+		})
 	}
 }
 
-func TestParseDateWithTimezoneOutOfRange(t *testing.T) {
-	inputs := []string{
-		"2023-05-29 00:00:00-13:00",
-		"2023-05-29 00:00:00+15:00",
+func TestNormalizeRFC3339Separators(t *testing.T) {
+	testCases := []struct {
+		input string
+		want  string
+	}{
+		{"2003-12-13t18:30:02z", "2003-12-13T18:30:02Z"},
+		{"2003-12-13T18:30:02z", "2003-12-13T18:30:02Z"},
+		{"2003-12-13t18:30:02Z", "2003-12-13T18:30:02Z"},
+		{"2003-12-13t18:30:02+01:00", "2003-12-13T18:30:02+01:00"},
+		{"2003-12-13T18:30:02Z", "2003-12-13T18:30:02Z"},
+		{"2003-12-13t", "2003-12-13T"},
+		{"2003-12-13", "2003-12-13"},
+		{"", ""},
+		{"2003/12/13t18:30:02z", "2003/12/13t18:30:02z"},
+		{"2003-12-13 18:30:02z", "2003-12-13 18:30:02z"},
+		{"Sat, 13 Dec 2003 18:30:02 z", "Sat, 13 Dec 2003 18:30:02 z"},
+	}
+
+	for _, testCase := range testCases {
+		t.Run(testCase.input, func(t *testing.T) {
+			if got := normalizeRFC3339Separators(testCase.input); got != testCase.want {
+				t.Errorf("normalizeRFC3339Separators(%q) = %q, want %q", testCase.input, got, testCase.want)
+			}
+		})
 	}
-	for _, input := range inputs {
-		date, err := Parse(input)
-
-		if err != nil {
-			t.Errorf(`Unable to parse date: %v`, err)
-		}
+}
 
-		if _, offset := date.Zone(); offset != 0 {
-			t.Errorf(`The offset should be reinitialized to 0 instead of %v because it's out of range`, offset)
-		}
+func TestPadShortOffset(t *testing.T) {
+	testCases := []struct {
+		input string
+		want  string
+	}{
+		{"Tue, Feb 16 2016 23:16:00 -700", "Tue, Feb 16 2016 23:16:00 -0700"},
+		{"Sun, 28 Oct 2018 13:48:00 +530", "Sun, 28 Oct 2018 13:48:00 +0530"},
+		{"Sun, 28 Oct 2018 13:48:00 -0700", "Sun, 28 Oct 2018 13:48:00 -0700"},
+		{"Sun, 28 Oct 2018 13:48:00 -07", "Sun, 28 Oct 2018 13:48:00 -07"},
+		{"Sun, 28 Oct 2018 13:48:00-700", "Sun, 28 Oct 2018 13:48:00-700"},
+		{"Sun, 28 Oct 2018 13:48:00 -7a0", "Sun, 28 Oct 2018 13:48:00 -7a0"},
+		{" -700", " -0700"},
+		{"-700", "-700"},
+		{"", ""},
+	}
+
+	for _, testCase := range testCases {
+		t.Run(testCase.input, func(t *testing.T) {
+			if got := padShortOffset(testCase.input); got != testCase.want {
+				t.Errorf("padShortOffset(%q) = %q, want %q", testCase.input, got, testCase.want)
+			}
+		})
 	}
 }

+ 106 - 0
internal/reader/date/timezone.go

@@ -0,0 +1,106 @@
+// SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
+// SPDX-License-Identifier: Apache-2.0
+
+package date // import "miniflux.app/v2/internal/reader/date"
+
+import (
+	"strings"
+	"time"
+)
+
+// The offsets of real time zones go from UTC−12:00 to UTC+14:00, and PostgreSQL
+// rejects dates outside of this range with "time zone displacement out of range".
+// See https://en.wikipedia.org/wiki/List_of_UTC_offsets
+const (
+	minOffset = -12 * 60 * 60
+	maxOffset = 14 * 60 * 60
+)
+
+// Dates are parsed in fixed locations, never in time.Local, so that the result
+// does not depend on the time zone of the server. A location is nil when the
+// time zone database is not available.
+var (
+	losAngelesLocation, _ = time.LoadLocation("America/Los_Angeles")
+	newYorkLocation, _    = time.LoadLocation("America/New_York")
+	brusselsLocation, _   = time.LoadLocation("Europe/Brussels")
+)
+
+type namedLocation struct {
+	name     string
+	location *time.Location
+}
+
+// timezoneAbbreviations lists the time zone abbreviations that are resolved in
+// a location that uses them, when a date ends with one. Go gives any other
+// abbreviation, except UTC and GMT, a zero offset.
+var timezoneAbbreviations = [...]namedLocation{
+	{"PST", losAngelesLocation},
+	{"PDT", losAngelesLocation},
+	{"EST", newYorkLocation},
+	{"EDT", newYorkLocation},
+}
+
+// ianaTimezones lists the IANA time zone names found in some feed dates.
+var ianaTimezones = [...]namedLocation{
+	{"America/Los_Angeles", losAngelesLocation},
+	{"Europe/Brussels", brusselsLocation},
+}
+
+// replaceIANATimezone replaces an IANA time zone name, which no layout accepts,
+// with "UTC", which every layout with a time zone abbreviation accepts. It
+// returns the location the parsed wall clock time belongs to, so that daylight
+// saving time is taken into account, or nil when the value has no such name or
+// the time zone database is not available.
+func replaceIANATimezone(value string) (string, *time.Location) {
+	for _, timezone := range ianaTimezones {
+		if strings.Contains(value, timezone.name) {
+			return strings.Replace(value, timezone.name, "UTC", 1), timezone.location
+		}
+	}
+
+	return value, nil
+}
+
+// timezoneLocation returns the location to parse a date in. Go resolves a time
+// zone abbreviation only with the zones of that location.
+func timezoneLocation(value string) *time.Location {
+	for _, timezone := range timezoneAbbreviations {
+		if timezone.location != nil && strings.HasSuffix(value, timezone.name) {
+			return timezone.location
+		}
+	}
+
+	return time.UTC
+}
+
+// withWallClockIn returns the time with the same wall clock time in location.
+func withWallClockIn(t time.Time, location *time.Location) time.Time {
+	return time.Date(t.Year(), t.Month(), t.Day(), t.Hour(), t.Minute(), t.Second(), t.Nanosecond(), location)
+}
+
+// padShortOffset pads a numeric offset written with three digits at the end of
+// a date, such as the "-700" of "Tue, Feb 16 2016 23:16:00 -700", to the four
+// digits that Go accepts.
+func padShortOffset(value string) string {
+	offsetStart := len(value) - 4
+	if offsetStart < 1 || value[offsetStart-1] != ' ' || (value[offsetStart] != '+' && value[offsetStart] != '-') {
+		return value
+	}
+
+	for _, digit := range value[offsetStart+1:] {
+		if digit < '0' || digit > '9' {
+			return value
+		}
+	}
+
+	return value[:offsetStart+1] + "0" + value[offsetStart+1:]
+}
+
+// checkTimezoneRange converts t to UTC when its offset is out of range.
+func checkTimezoneRange(t time.Time) time.Time {
+	if _, offset := t.Zone(); offset < minOffset || offset > maxOffset {
+		return t.UTC()
+	}
+
+	return t
+}

+ 110 - 0
internal/reader/date/translations.go

@@ -0,0 +1,110 @@
+// SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
+// SPDX-License-Identifier: Apache-2.0
+
+package date // import "miniflux.app/v2/internal/reader/date"
+
+import "strings"
+
+// replacer rewrites the parts of a date that no layout accepts as written, such
+// as localized day and month names, into their English form. When several pairs
+// match at the same position, the first one listed wins.
+var replacer = strings.NewReplacer(
+	// Time zone notations.
+	"GMT+0000 (Coordinated Universal Time)", "GMT",
+	"GMT-", "GMT -",
+
+	// Go only parses AM and PM.
+	"a.m.", "AM",
+	"p.m.", "PM",
+
+	// English day abbreviations longer than three letters.
+	"Thurs,", "Thu,",
+	"Thur,", "Thu,",
+
+	// German day names.
+	"Mo,", "Mon,",
+	"Di,", "Tue,",
+	"Mi,", "Wed,",
+	"Do,", "Thu,",
+	"Fr,", "Fri,",
+	"Sa,", "Sat,",
+	"So,", "Sun,",
+
+	// German month names.
+	"Mär ", "Mar ",
+	"Mai ", "May ",
+	"Okt ", "Oct ",
+	"Dez ", "Dec ",
+
+	// French day names, abbreviated with a comma.
+	"lun,", "Mon,",
+	"mar,", "Tue,",
+	"mer,", "Wed,",
+	"jeu,", "Thu,",
+	"ven,", "Fri,",
+	"sam,", "Sat,",
+	"dim,", "Sun,",
+
+	// French day names, abbreviated with a period.
+	"lun.", "Mon",
+	"mar.", "Tue",
+	"mer.", "Wed",
+	"jeu.", "Thu",
+	"ven.", "Fri",
+	"sam.", "Sat",
+	"dim.", "Sun",
+
+	// French day names in full.
+	"Lundi,", "Monday,",
+	"Mardi,", "Tuesday,",
+	"Mercredi,", "Wednesday,",
+	"Jeudi,", "Thursday,",
+	"Vendredi,", "Friday,",
+	"Samedi,", "Saturday,",
+	"Dimanche,", "Sunday,",
+
+	// French month names, abbreviated.
+	"jan.", "January ",
+	"feb.", "February ",
+	"mars.", "March ",
+	"avril.", "April ",
+	"mai.", "May ",
+	"juin.", "June ",
+	"juil.", "July",
+	"août.", "August",
+	"sept.", "September",
+	"oct.", "October",
+	"nov.", "November",
+	"dec.", "December",
+	"déc.", "December",
+	"avr ", "Apr ",
+	"jui ", "Jun ",
+
+	// French month names in full, in lower case.
+	"janvier ", "January ",
+	"février ", "February ",
+	"mars ", "March ",
+	"avril ", "April ",
+	"mai ", "May ",
+	"juin ", "June ",
+	"juillet ", "July ",
+	"août ", "August ",
+	"septembre ", "September ",
+	"octobre ", "October ",
+	"novembre ", "November ",
+	"décembre ", "December ",
+
+	// French month names in full, capitalized.
+	"Janvier", "January",
+	"Février", "February",
+	"Mars", "March",
+	"Avril", "April",
+	"Mai", "May",
+	"Juin", "June",
+	"Juillet", "July",
+	"Août", "August",
+	"Septembre", "September",
+	"Octobre", "October",
+	"Novembre", "November",
+	"Décembre", "December",
+)