From e82cbd746fd4b07e40fedacc4247b9cd50ef70e7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B8rn=20Erik=20Pedersen?= Date: Sat, 30 Oct 2021 16:06:00 +0200 Subject: [PATCH] tpl/time: Use configured location when date passed to Format is string Updates #9084 --- tpl/time/time.go | 2 +- tpl/time/time_test.go | 87 +++++++++++++++++++++++++++---------------- 2 files changed, 55 insertions(+), 34 deletions(-) diff --git a/tpl/time/time.go b/tpl/time/time.go index b89dcfe09..21ebae4f8 100644 --- a/tpl/time/time.go +++ b/tpl/time/time.go @@ -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 } diff --git a/tpl/time/time_test.go b/tpl/time/time_test.go index f6e4d0f72..0aeaed579 100644 --- a/tpl/time/time_test.go +++ b/tpl/time/time_test.go @@ -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) {