Handle more date formats
This commit is contained in:
parent
e4b4beabf0
commit
e7afec7eca
2 changed files with 63 additions and 2 deletions
|
@ -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
|
||||
}
|
||||
|
|
56
reader/date/parser_test.go
Normal file
56
reader/date/parser_test.go
Normal file
|
@ -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)
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue