mirror of
https://github.com/gohugoio/hugo.git
synced 2024-11-14 20:37:55 -05:00
tpl/time: Use configured location when date passed to Format is string
Updates #9084
This commit is contained in:
parent
3339c2bb61
commit
e82cbd746f
2 changed files with 55 additions and 34 deletions
|
@ -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
|
||||
// with the layout string
|
||||
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 {
|
||||
return "", err
|
||||
}
|
||||
|
|
|
@ -18,6 +18,8 @@ import (
|
|||
"testing"
|
||||
"time"
|
||||
|
||||
qt "github.com/frankban/quicktest"
|
||||
|
||||
translators "github.com/gohugoio/localescompressed"
|
||||
)
|
||||
|
||||
|
@ -80,43 +82,62 @@ func TestTimeLocation(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 {
|
||||
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"},
|
||||
// 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.123, false},
|
||||
{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.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"},
|
||||
// Custom layouts, as introduced in Hugo 0.87.
|
||||
{":date_medium", "2015-01-21", "Jan 21, 2015"},
|
||||
} {
|
||||
result, err := ns.Format(test.layout, test.value)
|
||||
if b, ok := test.expect.(bool); ok && !b {
|
||||
if err == nil {
|
||||
t.Errorf("[%d] DateFormat didn't return an expected error, got %v", i, result)
|
||||
}
|
||||
} else {
|
||||
if err != nil {
|
||||
t.Errorf("[%d] DateFormat failed: %s", i, err)
|
||||
continue
|
||||
}
|
||||
if result != test.expect {
|
||||
t.Errorf("[%d] DateFormat got %v but expected %v", i, result, test.expect)
|
||||
for i, test := 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"},
|
||||
// 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.123, false},
|
||||
{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.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"},
|
||||
// Custom layouts, as introduced in Hugo 0.87.
|
||||
{":date_medium", "2015-01-21", "Jan 21, 2015"},
|
||||
} {
|
||||
result, err := ns.Format(test.layout, test.value)
|
||||
if b, ok := test.expect.(bool); ok && !b {
|
||||
if err == nil {
|
||||
c.Errorf("[%d] DateFormat didn't return an expected error, got %v", i, result)
|
||||
}
|
||||
} else {
|
||||
if err != nil {
|
||||
c.Errorf("[%d] DateFormat failed: %s", i, err)
|
||||
continue
|
||||
}
|
||||
if 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) {
|
||||
|
|
Loading…
Reference in a new issue