Skip to content
/ hugo Public
forked from gohugoio/hugo

Commit

Permalink
tpl/time: Use configured location when date passed to Format is string
Browse files Browse the repository at this point in the history
  • Loading branch information
bep committed Oct 30, 2021
1 parent 3339c2b commit e82cbd7
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 34 deletions.
2 changes: 1 addition & 1 deletion tpl/time/time.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
87 changes: 54 additions & 33 deletions tpl/time/time_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import (
"testing"
"time"

qt "github.com/frankban/quicktest"

translators "github.com/gohugoio/localescompressed"
)

Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit e82cbd7

Please sign in to comment.