hugo

Unnamed repository; edit this file 'description' to name the repository.

git clone git://git.shimmy1996.com/hugo.git
commit 37490ee27af10f14aa40bd5e2ae39952b21ac1bb
parent 878754c21f7b91310bd80b48a7e676b3b1071504
Author: Tatsushi Demachi <tdemachi@gmail.com>
Date:   Wed, 21 Jan 2015 22:52:12 +0900

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

Diffstat:
Mdocs/content/templates/functions.md | 5+++++
Mtpl/template.go | 12++++++++++++
Mtpl/template_test.go | 30++++++++++++++++++++++++++++++
3 files changed, 47 insertions(+), 0 deletions(-)
diff --git a/docs/content/templates/functions.md b/docs/content/templates/functions.md
@@ -391,6 +391,11 @@ Replace all occurences of the search string with the replacement string.
 
 e.g. `{{ replace "Batman and Robin" "Robin" "Catwoman" }}` → "Batman and Catwoman"
 
+### dateFormat
+Converts the textual representation of the datetime into the other form or returns it of Go `time.Time` type value. These are formatted with the layout string.
+
+e.g. `{{ dateFormat "Monday, Jan 2, 2006" "2015-01-21" }}` →"Wednesday, Jan 21, 2015"
+
 ### highlight
 Take a string of code and a language, uses Pygments to return the syntax highlighted code in HTML. Used in the [highlight shortcode](/extras/highlighting).
 
diff --git a/tpl/template.go b/tpl/template.go
@@ -905,6 +905,17 @@ func Replace(a, b, c interface{}) (string, error) {
 	return strings.Replace(aStr, bStr, cStr, -1), nil
 }
 
+// DateFormat converts the textual representation of the datetime string into
+// the other form or returns it of the time.Time value. These are formatted
+// with the layout string
+func DateFormat(layout string, v interface{}) (string, error) {
+	t, err := cast.ToTimeE(v)
+	if err != nil {
+		return "", err
+	}
+	return t.Format(layout), nil
+}
+
 func SafeHtml(text string) template.HTML {
 	return template.HTML(text)
 }
@@ -1281,6 +1292,7 @@ func init() {
 		"chomp":       Chomp,
 		"replace":     Replace,
 		"trim":        Trim,
+		"dateFormat":  DateFormat,
 	}
 
 }
diff --git a/tpl/template_test.go b/tpl/template_test.go
@@ -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