Skip to content

Commit

Permalink
fix buildTime type
Browse files Browse the repository at this point in the history
`string` to `time.Time`
  • Loading branch information
satotake committed Apr 26, 2022
1 parent 73c39bd commit dd18d52
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 47 deletions.
4 changes: 2 additions & 2 deletions commands/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand Down
21 changes: 12 additions & 9 deletions common/htime/time.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
package htime

import (
"fmt"
"strings"
"time"

Expand Down Expand Up @@ -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)
}
2 changes: 1 addition & 1 deletion helpers/content.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
10 changes: 9 additions & 1 deletion tpl/time/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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,
Expand Down
11 changes: 7 additions & 4 deletions tpl/time/time.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
Expand Down Expand Up @@ -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.
Expand Down
34 changes: 4 additions & 30 deletions tpl/time/time_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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")

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

0 comments on commit dd18d52

Please sign in to comment.