mirror of
https://github.com/gohugoio/hugo.git
synced 2024-11-28 22:32:05 -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
|
// 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
|
||||||
}
|
}
|
||||||
|
|
|
@ -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,8 +82,10 @@ func TestTimeLocation(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestFormat(t *testing.T) {
|
func TestFormat(t *testing.T) {
|
||||||
t.Parallel()
|
c := qt.New(t)
|
||||||
|
|
||||||
|
c.Run("UTC", func(c *qt.C) {
|
||||||
|
c.Parallel()
|
||||||
ns := New(translators.GetTranslator("en"), time.UTC)
|
ns := New(translators.GetTranslator("en"), time.UTC)
|
||||||
|
|
||||||
for i, test := range []struct {
|
for i, test := range []struct {
|
||||||
|
@ -105,18 +109,35 @@ func TestFormat(t *testing.T) {
|
||||||
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) {
|
||||||
|
|
Loading…
Reference in a new issue