ref: dd19d0cc77c42bc4afb2753595b578403b459326
parent: 17aafb39ddd04a443994b3cb605970070b274377
author: Noah Campbell <noahcampbell@gmail.com>
date: Sun Aug 11 16:34:54 EDT 2013
Provide better support for various date formats. Fixes #30 as long as the date is well formatted.
--- a/hugolib/helpers.go
+++ b/hugolib/helpers.go
@@ -15,6 +15,7 @@
import (
"bytes"
+ "errors"
"fmt"
"github.com/kr/pretty"
"html/template"
@@ -63,18 +64,30 @@
func interfaceToStringToDate(i interface{}) time.Time {s := interfaceToString(i)
- d, e := time.Parse("02 Jan 06 15:04 MST", s)- if e != nil {- d, e = time.Parse("2006-01-02", s)+ if d, e := parseDateWith(s, []string{+ time.RFC3339,
+ time.RFC1123Z,
+ time.RFC1123,
+ time.RFC822Z,
+ time.RFC822,
+ time.ANSIC,
+ time.UnixDate,
+ time.RubyDate,
+ }); e == nil {+ return d
}
- if e != nil {- d, e = time.Parse("02 Jan 06", s)- }
+ return time.Unix(0, 0)
+}
- return d
-
+func parseDateWith(s string, dates []string) (d time.Time, e error) {+ for _, dateType := range dates {+ if d, e = time.Parse(dateType, s); e == nil {+ return
+ }
+ }
+ return d, errors.New(fmt.Sprintf("Unable to parse date: %s", s))}
func interfaceToBool(i interface{}) bool {--- a/hugolib/page_test.go
+++ b/hugolib/page_test.go
@@ -5,6 +5,7 @@
"io"
"strings"
"testing"
+ "time"
)
var EMPTY_PAGE = ""
@@ -69,6 +70,51 @@
Text
`
+var PAGE_WITH_INVALID_DATE = `---
+date: 2010-05-02 15:29:31+08:00
+---
+Page With Invalid Date (missing the T for RFC 3339)`
+
+var PAGE_WITH_DATE_RFC3339 = `---
+date: 2010-05-02T15:29:31+08:00
+---
+Page With Date RFC3339`
+
+var PAGE_WITH_DATE_RFC1123 = `---
+date: Sun, 02 May 2010 15:29:31 PST
+---
+Page With Date RFC1123`
+
+var PAGE_WITH_DATE_RFC1123Z = `---
+date: Sun, 02 May 2010 15:29:31 +0800
+---
+Page With Date RFC1123Z`
+
+var PAGE_WITH_DATE_RFC822 = `---
+date: 02 May 10 15:29 PST
+---
+Page With Date RFC822`
+
+var PAGE_WITH_DATE_RFC822Z = `---
+date: 02 May 10 15:29 +0800
+---
+Page With Date RFC822Z`
+
+var PAGE_WITH_DATE_ANSIC = `---
+date: Sun May 2 15:29:31 2010
+---
+Page With Date ANSIC`
+
+var PAGE_WITH_DATE_UnixDate = `---
+date: Sun May 2 15:29:31 PST 2010
+---
+Page With Date UnixDate`
+
+var PAGE_WITH_DATE_RubyDate = `---
+date: Sun May 02 15:29:31 +0800 2010
+---
+Page With Date RubyDate`
+
func checkError(t *testing.T, err error, expected string) { if err == nil { t.Fatalf("err is nil")@@ -167,5 +213,41 @@
_, err := ReadFrom(strings.NewReader(INVALID_FRONT_MATTER_LEADING_WS), "invalid/front/matter/leading/ws")
if err != nil { t.Fatalf("Unable to parse front matter given leading whitespace: %s", err)+ }
+}
+
+func TestDegenerateDateFrontMatter(t *testing.T) {+ p, _ := ReadFrom(strings.NewReader(PAGE_WITH_INVALID_DATE), "page/with/invalid/date")
+ if p.Date != time.Unix(0, 0) {+ t.Fatalf("Date should be set to computer epoch. Got: %s", p.Date)+ }
+}
+
+func TestParsingDateInFrontMatter(t *testing.T) {+
+ for _, test := range []struct {+ buf string
+ dt string
+ }{+ {PAGE_WITH_DATE_RFC3339, "2010-05-02T15:29:31+08:00"},+ {PAGE_WITH_DATE_RFC1123, "2010-05-02T15:29:31-08:00"},+ {PAGE_WITH_DATE_RFC1123Z, "2010-05-02T15:29:31+08:00"},+ {PAGE_WITH_DATE_RFC822, "2010-05-02T15:29:00-08:00"},+ {PAGE_WITH_DATE_RFC822Z, "2010-05-02T15:29:00+08:00"},+ {PAGE_WITH_DATE_ANSIC, "2010-05-02T15:29:31Z"},+ {PAGE_WITH_DATE_UnixDate, "2010-05-02T15:29:31-08:00"},+ {PAGE_WITH_DATE_RubyDate, "2010-05-02T15:29:31+08:00"},+ } {+ dt, e := time.Parse(time.RFC3339, test.dt)
+ if e != nil {+ t.Fatalf("Unable to parse date time (RFC3339) for running the test: %s", e)+ }
+ p, err := ReadFrom(strings.NewReader(test.buf), "page/with/date")
+ if err != nil {+ t.Fatalf("Expected to be able to parse page.")+ }
+ if !dt.Equal(p.Date) {+ t.Errorf("Date does not equal frontmatter:\n%s\nGot: %s. Diff: %s", test.buf, p.Date, dt.Sub(p.Date))+ }
}
}
--
⑨