tpl/time: Use configured location when date passed to Format is string

Updates #9084
This commit is contained in:
Bjørn Erik Pedersen 2021-10-30 16:06:00 +02:00
parent 3339c2bb61
commit e82cbd746f
No known key found for this signature in database
GPG key ID: 330E6E2BD4859D8F
2 changed files with 55 additions and 34 deletions

View file

@ -63,7 +63,7 @@ func (ns *Namespace) AsTime(v interface{}, args ...interface{}) (interface{}, er
// the other form or returns it of the time.Time value. These are formatted // the other form or returns it of the time.Time value. These are formatted
// with the layout string // with the layout string
func (ns *Namespace) Format(layout string, v interface{}) (string, error) { func (ns *Namespace) Format(layout string, v interface{}) (string, error) {
t, err := cast.ToTimeE(v) t, err := htime.ToTimeInDefaultLocationE(v, ns.location)
if err != nil { if err != nil {
return "", err return "", err
} }

View file

@ -18,6 +18,8 @@ import (
"testing" "testing"
"time" "time"
qt "github.com/frankban/quicktest"
translators "github.com/gohugoio/localescompressed" translators "github.com/gohugoio/localescompressed"
) )
@ -80,43 +82,62 @@ func TestTimeLocation(t *testing.T) {
} }
func TestFormat(t *testing.T) { func TestFormat(t *testing.T) {
t.Parallel() c := qt.New(t)
ns := New(translators.GetTranslator("en"), time.UTC) c.Run("UTC", func(c *qt.C) {
c.Parallel()
ns := New(translators.GetTranslator("en"), time.UTC)
for i, test := range []struct { for i, test := range []struct {
layout string layout string
value interface{} value interface{}
expect interface{} expect interface{}
}{ }{
{"Monday, Jan 2, 2006", "2015-01-21", "Wednesday, Jan 21, 2015"}, {"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"}, {"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"}, {"This isn't a date layout string", "2015-01-21", "This isn't a date layout string"},
// The following test case gives either "Tuesday, Jan 20, 2015" or "Monday, Jan 19, 2015" depending on the local time zone // The following test case gives either "Tuesday, Jan 20, 2015" or "Monday, Jan 19, 2015" depending on the local time zone
{"Monday, Jan 2, 2006", 1421733600, time.Unix(1421733600, 0).Format("Monday, Jan 2, 2006")}, {"Monday, Jan 2, 2006", 1421733600, time.Unix(1421733600, 0).Format("Monday, Jan 2, 2006")},
{"Monday, Jan 2, 2006", 1421733600.123, false}, {"Monday, Jan 2, 2006", 1421733600.123, false},
{time.RFC3339, time.Date(2016, time.March, 3, 4, 5, 0, 0, time.UTC), "2016-03-03T04:05:00Z"}, {time.RFC3339, time.Date(2016, time.March, 3, 4, 5, 0, 0, time.UTC), "2016-03-03T04:05:00Z"},
{time.RFC1123, time.Date(2016, time.March, 3, 4, 5, 0, 0, time.UTC), "Thu, 03 Mar 2016 04:05:00 UTC"}, {time.RFC1123, time.Date(2016, time.March, 3, 4, 5, 0, 0, time.UTC), "Thu, 03 Mar 2016 04:05:00 UTC"},
{time.RFC3339, "Thu, 03 Mar 2016 04:05:00 UTC", "2016-03-03T04:05:00Z"}, {time.RFC3339, "Thu, 03 Mar 2016 04:05:00 UTC", "2016-03-03T04:05:00Z"},
{time.RFC1123, "2016-03-03T04:05:00Z", "Thu, 03 Mar 2016 04:05:00 UTC"}, {time.RFC1123, "2016-03-03T04:05:00Z", "Thu, 03 Mar 2016 04:05:00 UTC"},
// Custom layouts, as introduced in Hugo 0.87. // Custom layouts, as introduced in Hugo 0.87.
{":date_medium", "2015-01-21", "Jan 21, 2015"}, {":date_medium", "2015-01-21", "Jan 21, 2015"},
} { } {
result, err := ns.Format(test.layout, test.value) result, err := ns.Format(test.layout, test.value)
if b, ok := test.expect.(bool); ok && !b { if b, ok := test.expect.(bool); ok && !b {
if err == nil { if err == nil {
t.Errorf("[%d] DateFormat didn't return an expected error, got %v", i, result) c.Errorf("[%d] DateFormat didn't return an expected error, got %v", i, result)
} }
} else { } else {
if err != nil { if err != nil {
t.Errorf("[%d] DateFormat failed: %s", i, err) c.Errorf("[%d] DateFormat failed: %s", i, err)
continue continue
} }
if result != test.expect { if result != test.expect {
t.Errorf("[%d] DateFormat got %v but expected %v", i, result, test.expect) c.Errorf("[%d] DateFormat got %v but expected %v", i, result, test.expect)
}
} }
} }
} })
//Issue #9084
c.Run("TZ America/Los_Angeles", func(c *qt.C) {
c.Parallel()
loc, err := time.LoadLocation("America/Los_Angeles")
c.Assert(err, qt.IsNil)
ns := New(translators.GetTranslator("en"), loc)
d, err := ns.Format(":time_full", "2020-03-09T11:00:00")
c.Assert(err, qt.IsNil)
c.Assert(d, qt.Equals, "11:00:00 am Pacific Daylight Time")
})
} }
func TestDuration(t *testing.T) { func TestDuration(t *testing.T) {