mirror of
https://github.com/gohugoio/hugo.git
synced 2025-08-27 22:09:53 +02:00
Add dateFormat template function
This is the implementation of the proposal at #235 and http://discuss.gohugo.io/t/parsing-dates-in-templates/603/3
This commit is contained in:
@@ -10,6 +10,7 @@ import (
|
||||
"reflect"
|
||||
"runtime"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
type tstNoStringer struct {
|
||||
@@ -988,6 +989,35 @@ func TestTrim(t *testing.T) {
|
||||
assert.NotNil(t, e, "tstNoStringer isn't trimmable")
|
||||
}
|
||||
|
||||
func TestDateFormat(t *testing.T) {
|
||||
for i, this := range []struct {
|
||||
layout string
|
||||
value interface{}
|
||||
expect interface{}
|
||||
}{
|
||||
{"Monday, Jan 2, 2006", "2015-01-21", "Wednesday, Jan 21, 2015"},
|
||||
{"Monday, Jan 2, 2006", time.Date(2015, time.January, 21, 0, 0, 0, 0, time.UTC), "Wednesday, Jan 21, 2015"},
|
||||
{"This isn't a date layout string", "2015-01-21", "This isn't a date layout string"},
|
||||
{"Monday, Jan 2, 2006", 1421733600, false},
|
||||
{"Monday, Jan 2, 2006", 1421733600.123, false},
|
||||
} {
|
||||
result, err := DateFormat(this.layout, this.value)
|
||||
if b, ok := this.expect.(bool); ok && !b {
|
||||
if err == nil {
|
||||
t.Errorf("[%d] DateFormat didn't return an expected error", i)
|
||||
}
|
||||
} else {
|
||||
if err != nil {
|
||||
t.Errorf("[%d] DateFormat failed: %s", i, err)
|
||||
continue
|
||||
}
|
||||
if result != this.expect {
|
||||
t.Errorf("[%d] DateFormat got %v but expected %v", i, result, this.expect)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestSafeHtml(t *testing.T) {
|
||||
for i, this := range []struct {
|
||||
str string
|
||||
|
Reference in New Issue
Block a user