diff --git a/commands/list.go b/commands/list.go index cf41244c0b8..971e665c791 100644 --- a/commands/list.go +++ b/commands/list.go @@ -100,7 +100,7 @@ List requires a subcommand, e.g. ` + "`hugo list drafts`.", return newSystemError("Error building sites", err) } - buildTime, err := htime.Now(b.buildTime) + buildTime, err := htime.ParseBuildTimeDefaultNow(b.buildTime) if err != nil { return newSystemError("Error building sites", err) } @@ -133,7 +133,7 @@ List requires a subcommand, e.g. ` + "`hugo list drafts`.", return newSystemError("Error building sites", err) } - buildTime, err := htime.Now(b.buildTime) + buildTime, err := htime.ParseBuildTimeDefaultNow(b.buildTime) if err != nil { return newSystemError("Error building sites", err) } diff --git a/common/htime/time.go b/common/htime/time.go index 939f9b5484e..d9ded68ca3d 100644 --- a/common/htime/time.go +++ b/common/htime/time.go @@ -14,7 +14,6 @@ package htime import ( - "fmt" "strings" "time" @@ -151,15 +150,19 @@ func ToTimeInDefaultLocationE(i interface{}, location *time.Location) (tim time. } // Now returns parsed `buildTime` value, which is passed via cli, or time.Now(). -// We must use this as the alternative of native time.Now() in the context -// where hugo renders time-dependent contents. -func Now(i string) (time.Time, error) { - if i == "" { +func ParseBuildTimeDefaultNow(bt string) (time.Time, error) { + if bt == "" { return time.Now(), nil } - t, err := cast.ToTimeInDefaultLocationE(i, nil) - if err != nil { - return t, fmt.Errorf(`failed to parse "buildTime" flag: %s`, err) + + return cast.StringToDateInDefaultLocation(bt, nil) +} + +// Now returns parsed `buildTime` value, which is passed via cli, or zero time. +func ParseBuildTimeDefaultZero(bt string) (time.Time, error) { + if bt == "" { + return time.Time{}, nil } - return t, nil + + return cast.StringToDateInDefaultLocation(bt, nil) } diff --git a/helpers/content.go b/helpers/content.go index e024162dc2e..1c1f6873b61 100644 --- a/helpers/content.go +++ b/helpers/content.go @@ -67,7 +67,7 @@ type ContentSpec struct { // NewContentSpec returns a ContentSpec initialized // with the appropriate fields from the given config.Provider. func NewContentSpec(cfg config.Provider, logger loggers.Logger, contentFs afero.Fs, ex *hexec.Exec) (*ContentSpec, error) { - buildTime, err := htime.Now(cfg.GetString("buildTime")) + buildTime, err := htime.ParseBuildTimeDefaultNow(cfg.GetString("buildTime")) if err != nil { return nil, err } diff --git a/tpl/time/init.go b/tpl/time/init.go index 1392ed27c2b..c196f44847f 100644 --- a/tpl/time/init.go +++ b/tpl/time/init.go @@ -15,7 +15,9 @@ package time import ( "errors" + "fmt" + "github.com/gohugoio/hugo/common/htime" "github.com/gohugoio/hugo/deps" "github.com/gohugoio/hugo/langs" "github.com/gohugoio/hugo/tpl/internal" @@ -28,7 +30,13 @@ func init() { if d.Language == nil { panic("Language must be set") } - ctx := New(langs.GetTranslator(d.Language), langs.GetLocation(d.Language), d.Cfg.GetString("buildTime")) + + bt, err := htime.ParseBuildTimeDefaultZero(d.Cfg.GetString("buildTime")) + if err != nil { + panic(fmt.Sprintf(`failed to parse "buildTime" flag: %s`, err)) + } + + ctx := New(langs.GetTranslator(d.Language), langs.GetLocation(d.Language), bt) ns := &internal.TemplateFuncsNamespace{ Name: name, diff --git a/tpl/time/time.go b/tpl/time/time.go index 937f57c70d7..728fa6788dc 100644 --- a/tpl/time/time.go +++ b/tpl/time/time.go @@ -27,7 +27,7 @@ import ( ) // New returns a new instance of the time-namespaced template functions. -func New(translator locales.Translator, location *time.Location, buildTime string) *Namespace { +func New(translator locales.Translator, location *time.Location, buildTime time.Time) *Namespace { return &Namespace{ timeFormatter: htime.NewTimeFormatter(translator), location: location, @@ -39,7 +39,7 @@ func New(translator locales.Translator, location *time.Location, buildTime strin type Namespace struct { timeFormatter htime.TimeFormatter location *time.Location - buildTime string + buildTime time.Time } // AsTime converts the textual representation of the datetime string into @@ -76,8 +76,11 @@ func (ns *Namespace) Format(layout string, v interface{}) (string, error) { // Now returns the current local time. // If `buildTime` flag is set, returns it instead. func (ns *Namespace) Now() _time.Time { - t, _ := htime.Now(ns.buildTime) - return t + // `buildTime` is unset + if ns.buildTime.IsZero() { + return time.Now() + } + return ns.buildTime } // ParseDuration parses a duration string. diff --git a/tpl/time/time_test.go b/tpl/time/time_test.go index 03ff55bbadf..5cadcdb4cba 100644 --- a/tpl/time/time_test.go +++ b/tpl/time/time_test.go @@ -27,7 +27,7 @@ func TestTimeLocation(t *testing.T) { t.Parallel() loc, _ := time.LoadLocation("America/Antigua") - ns := New(translators.GetTranslator("en"), loc, "") + ns := New(translators.GetTranslator("en"), loc, time.Now()) for i, test := range []struct { name string @@ -86,7 +86,7 @@ func TestFormat(t *testing.T) { c.Run("UTC", func(c *qt.C) { c.Parallel() - ns := New(translators.GetTranslator("en"), time.UTC, "") + ns := New(translators.GetTranslator("en"), time.UTC, time.Now()) for i, test := range []struct { layout string @@ -129,7 +129,7 @@ func TestFormat(t *testing.T) { loc, err := time.LoadLocation("America/Los_Angeles") c.Assert(err, qt.IsNil) - ns := New(translators.GetTranslator("en"), loc, "") + ns := New(translators.GetTranslator("en"), loc, time.Now()) d, err := ns.Format(":time_full", "2020-03-09T11:00:00") @@ -143,7 +143,7 @@ func TestFormat(t *testing.T) { func TestDuration(t *testing.T) { t.Parallel() - ns := New(translators.GetTranslator("en"), time.UTC, "") + ns := New(translators.GetTranslator("en"), time.UTC, time.Now()) for i, test := range []struct { unit interface{} @@ -182,29 +182,3 @@ func TestDuration(t *testing.T) { } } } - -func TestNow(t *testing.T) { - c := qt.New(t) - - c.Run("Unset buildTime", func(c *qt.C) { - c.Parallel() - ns := New(translators.GetTranslator("en"), time.UTC, "") - got := ns.Now() - want := time.Now() - - if want.Sub(got) > time.Second { - c.Errorf("'now' failed.\ngot: %s\nwant: %s", got, want) - } - }) - - c.Run("Set buildTime", func(c *qt.C) { - c.Parallel() - ns := New(translators.GetTranslator("en"), time.UTC, "2021-11-06") - got := ns.Now() - want := time.Date(2021, 11, 6, 0, 0, 0, 0, time.Local) - - if got != want { - c.Errorf("buldTime 'now' failed.\ngot: %s\nwant: %s", got, want) - } - }) -}