aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorGravatar Frédéric Guillot <fred@miniflux.net>2017-12-22 17:59:28 -0800
committerGravatar Frédéric Guillot <fred@miniflux.net>2017-12-22 17:59:28 -0800
commite7afec7ecaadd90711f2453e9b6b249ef1672864 (patch)
tree0b17166dbf944f1b5b1e28f37fc8294ba9e80452
parente4b4beabf0e82c51b64c64a1216b04cb3b519f6d (diff)
Handle more date formats
-rw-r--r--reader/date/parser.go9
-rw-r--r--reader/date/parser_test.go56
2 files changed, 63 insertions, 2 deletions
diff --git a/reader/date/parser.go b/reader/date/parser.go
index e573ff8..353ee8d 100644
--- a/reader/date/parser.go
+++ b/reader/date/parser.go
@@ -5,6 +5,7 @@
package date
import (
+ "errors"
"fmt"
"strings"
"time"
@@ -45,6 +46,8 @@ var dateFormats = []string{
"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, 2 January 2006 15:04 MST",
"Mon, 2 January 2006, 15:04 -0700",
"Mon, 2 January 2006, 15:04:05 MST",
@@ -100,6 +103,7 @@ var dateFormats = []string{
"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",
"January 2, 2006 3:04 PM",
"January 2, 2006, 3:04 p.m.",
"January 2, 2006 15:04:05 MST",
@@ -125,6 +129,7 @@ var dateFormats = []string{
"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",
@@ -189,7 +194,7 @@ var dateFormats = []string{
func Parse(ds string) (t time.Time, err error) {
d := strings.TrimSpace(ds)
if d == "" {
- return t, fmt.Errorf("Date string is empty")
+ return t, errors.New("date parser: empty value")
}
for _, f := range dateFormats {
@@ -198,6 +203,6 @@ func Parse(ds string) (t time.Time, err error) {
}
}
- err = fmt.Errorf("Failed to parse date: %s", ds)
+ err = fmt.Errorf(`date parser: failed to parse date "%s"`, ds)
return
}
diff --git a/reader/date/parser_test.go b/reader/date/parser_test.go
new file mode 100644
index 0000000..c8bb0fb
--- /dev/null
+++ b/reader/date/parser_test.go
@@ -0,0 +1,56 @@
+// Copyright 2017 Frédéric Guillot. All rights reserved.
+// Use of this source code is governed by the Apache 2.0
+// license that can be found in the LICENSE file.
+
+package date
+
+import "testing"
+
+func TestParseEmptyDate(t *testing.T) {
+ if _, err := Parse(" "); err == nil {
+ t.Fatalf(`Empty dates should return an error`)
+ }
+}
+
+func TestParseInvalidDate(t *testing.T) {
+ if _, err := Parse("invalid"); err == nil {
+ t.Fatalf(`Invalid dates should return an error`)
+ }
+}
+
+func TestParseAtomDate(t *testing.T) {
+ date, err := Parse("2017-12-22T22:09:49+00:00")
+ if err != nil {
+ t.Fatalf(`Atom dates should be parsed correctly`)
+ }
+
+ if date.Unix() != 1513980589 {
+ t.Fatal(`Invalid date parsed`)
+ }
+}
+
+func TestParseRSSDate(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`)
+ }
+}
+
+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",
+ }
+
+ for _, date := range dates {
+ if _, err := Parse(date); err != nil {
+ t.Fatalf(`Unable to parse date: "%s"`, date)
+ }
+ }
+}