Use configured timeZone for the clock

And some other related adjustments.

Updates #8787
This commit is contained in:
Bjørn Erik Pedersen 2022-05-07 14:10:32 +02:00
parent e77ca3c105
commit 35c88a7f90
9 changed files with 54 additions and 34 deletions

View file

@ -98,7 +98,6 @@ type commandeer struct {
serverPorts []serverPortListener
languagesConfigured bool
languages langs.Languages
doLiveReload bool
renderStaticToDisk bool
@ -168,16 +167,17 @@ func (c *commandeer) initFs(fs *hugofs.Fs) error {
return nil
}
func (c *commandeer) initClock() error {
func (c *commandeer) initClock(loc *time.Location) error {
bt := c.Cfg.GetString("clock")
if bt == "" {
return nil
}
t, err := cast.StringToDateInDefaultLocation(bt, nil)
t, err := cast.StringToDateInDefaultLocation(bt, loc)
if err != nil {
return fmt.Errorf(`failed to parse "clock" flag: %s`, err)
}
htime.Clock = clock.Start(t)
return nil
}
@ -359,14 +359,15 @@ func (c *commandeer) loadConfig() error {
c.configFiles = configFiles
err = c.initClock()
if err != nil {
return err
var ok bool
c.languages, ok = c.Cfg.Get("languagesSorted").(langs.Languages)
if !ok {
panic("languages not configured")
}
if l, ok := c.Cfg.Get("languagesSorted").(langs.Languages); ok {
c.languagesConfigured = true
c.languages = l
err = c.initClock(langs.GetLocation(c.languages[0]))
if err != nil {
return err
}
// Set some commonly used flags

View file

@ -281,7 +281,7 @@ func (cc *hugoBuilderCommon) handleCommonBuilderFlags(cmd *cobra.Command) {
cmd.PersistentFlags().StringVarP(&cc.environment, "environment", "e", "", "build environment")
cmd.PersistentFlags().StringP("themesDir", "", "", "filesystem path to themes directory")
cmd.PersistentFlags().StringP("ignoreVendorPaths", "", "", "ignores any _vendor for module paths matching the given Glob pattern")
cmd.PersistentFlags().StringVar(&cc.clock, "clock", "", "set clock inside hugo, e.g. --clock 2021-11-06T22:30:00.00+09:00")
cmd.PersistentFlags().StringVar(&cc.clock, "clock", "", "set the clock used by Hugo, e.g. --clock 2021-11-06T22:30:00.00+09:00")
}
func (cc *hugoBuilderCommon) handleFlags(cmd *cobra.Command) {

View file

@ -21,7 +21,9 @@ import (
"strings"
"testing"
"github.com/bep/clock"
qt "github.com/frankban/quicktest"
"github.com/gohugoio/hugo/common/htime"
"github.com/gohugoio/hugo/hugofs"
"github.com/spf13/afero"
"golang.org/x/tools/txtar"
@ -29,6 +31,7 @@ import (
// Issue #5662
func TestHugoWithContentDirOverride(t *testing.T) {
t.Parallel()
c := qt.New(t)
files := `
@ -50,6 +53,7 @@ Page: {{ .Title }}|
// Issue #9794
func TestHugoStaticFilesMultipleStaticAndManyFolders(t *testing.T) {
t.Parallel()
c := qt.New(t)
files := `
@ -95,12 +99,15 @@ Home.
// Issue #8787
func TestHugoListCommandsWithClockFlag(t *testing.T) {
t.Cleanup(func() { htime.Clock = clock.System() })
c := qt.New(t)
files := `
-- config.toml --
baseURL = "https://example.org"
title = "Hugo Commands"
timeZone = "UTC"
-- content/past.md --
---
title: "Past"
@ -115,7 +122,9 @@ date: 2200-11-06
Page: {{ .Title }}|
`
s := newTestHugoCmdBuilder(c, files, []string{"list", "future"}).Build()
s := newTestHugoCmdBuilder(c, files, []string{"list", "future"})
s.captureOut = true
s.Build()
p := filepath.Join("content", "future.md")
s.AssertStdout(p + ",2200-11-06T00:00:00Z")
@ -130,6 +139,8 @@ type testHugoCmdBuilder struct {
dir string
files string
args []string
captureOut bool
out string
}
@ -156,12 +167,17 @@ func (s *testHugoCmdBuilder) Build() *testHugoCmdBuilder {
args := append(s.args, "-s="+s.dir, "--quiet")
cmd.SetArgs(args)
if s.captureOut {
out, err := captureStdout(func() error {
_, err := cmd.ExecuteC()
return err
})
s.Assert(err, qt.IsNil)
s.out = out
} else {
_, err := cmd.ExecuteC()
s.Assert(err, qt.IsNil)
}
return s
}

View file

@ -166,11 +166,6 @@ func (sc *serverCmd) server(cmd *cobra.Command, args []string) error {
c.Set("watch", true)
}
// TODO(bep) yes, we should fix.
if !c.languagesConfigured {
return nil
}
// We can only do this once.
serverCfgInit.Do(func() {
c.serverPorts = make([]serverPortListener, 1)

View file

@ -75,7 +75,8 @@ var (
"November",
"December",
}
Clock = clock.Start(time.Now())
Clock = clock.System()
)
func NewTimeFormatter(ltr locales.Translator) TimeFormatter {
@ -151,12 +152,12 @@ func ToTimeInDefaultLocationE(i any, location *time.Location) (tim time.Time, er
return cast.ToTimeInDefaultLocationE(i, location)
}
// Now returns time.Now() or time value based on`clock` flag.
// Now returns time.Now() or time value based on the `clock` flag.
// Use this function to fake time inside hugo.
func Now() time.Time {
return Clock.Now()
}
func Since(t time.Time) time.Duration {
return Clock.Now().Sub(t)
return Clock.Since(t)
}

2
go.mod
View file

@ -91,7 +91,7 @@ require (
github.com/aws/aws-sdk-go-v2/service/sso v1.4.0 // indirect
github.com/aws/aws-sdk-go-v2/service/sts v1.7.0 // indirect
github.com/aws/smithy-go v1.8.0 // indirect
github.com/bep/clock v0.1.0 // indirect
github.com/bep/clock v0.3.0 // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.1 // indirect
github.com/dlclark/regexp2 v1.4.0 // indirect
github.com/go-openapi/jsonpointer v0.19.5 // indirect

8
go.sum
View file

@ -214,6 +214,14 @@ github.com/aws/smithy-go v1.8.0/go.mod h1:SObp3lf9smib00L/v3U2eAKG8FyQ7iLrJnQiAm
github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA=
github.com/bep/clock v0.1.0 h1:sdvJ08SsgwTY/ejA705YKBlktFfj2uVpmQSSQspZJ2c=
github.com/bep/clock v0.1.0/go.mod h1:shVP9tZ3cXpbVj60SnlU1pMwKjFxECBRm9vZfpoA0Gs=
github.com/bep/clock v0.2.0 h1:Uiv+P2wRVBy/g9Gybh645PgEOSC1BCXIQ7DOIc2bE64=
github.com/bep/clock v0.2.0/go.mod h1:6Gz2lapnJ9vxpvPxQ2u6FcXFRoj4kkiqQ6pm0ERZlwk=
github.com/bep/clock v0.2.1-0.20220507123307-c7e9c1bdae6f h1:dZlHVwWUCnS4P1Yk57qppK81fAUVV8DzDAo1lZA85hk=
github.com/bep/clock v0.2.1-0.20220507123307-c7e9c1bdae6f/go.mod h1:6Gz2lapnJ9vxpvPxQ2u6FcXFRoj4kkiqQ6pm0ERZlwk=
github.com/bep/clock v0.2.1-0.20220507124130-0a9d9c79927c h1:EPIuutNKs0tBieB1Z46GhNhiaYCs8bqSwwuo08xJHqs=
github.com/bep/clock v0.2.1-0.20220507124130-0a9d9c79927c/go.mod h1:6Gz2lapnJ9vxpvPxQ2u6FcXFRoj4kkiqQ6pm0ERZlwk=
github.com/bep/clock v0.3.0 h1:vfOA6+wVb6pPQEiXow9f/too92vNTLe9MuwO13PfI0M=
github.com/bep/clock v0.3.0/go.mod h1:6Gz2lapnJ9vxpvPxQ2u6FcXFRoj4kkiqQ6pm0ERZlwk=
github.com/bep/debounce v1.2.0 h1:wXds8Kq8qRfwAOpAxHrJDbCXgC5aHSzgQb/0gKsHQqo=
github.com/bep/debounce v1.2.0/go.mod h1:H8yggRPQKLUhUoqrJC1bO2xNya7vanpDl7xR3ISbCJ0=
github.com/bep/gitmap v1.1.2 h1:zk04w1qc1COTZPPYWDQHvns3y1afOsdRfraFQ3qI840=

View file

@ -1585,7 +1585,7 @@ func TestShouldBuild(t *testing.T) {
func TestShouldBuildWithClock(t *testing.T) {
htime.Clock = clock.Start(time.Date(2021, 11, 17, 20, 34, 58, 651387237, time.UTC))
t.Cleanup(func() { htime.Clock = clock.Start(time.Now()) })
t.Cleanup(func() { htime.Clock = clock.System() })
past := time.Date(2009, 11, 17, 20, 34, 58, 651387237, time.UTC)
future := time.Date(2037, 11, 17, 20, 34, 58, 651387237, time.UTC)
zero := time.Time{}

View file

@ -28,7 +28,6 @@ func init() {
if d.Language == nil {
panic("Language must be set")
}
ctx := New(langs.GetTimeFormatter(d.Language), langs.GetLocation(d.Language))
ns := &internal.TemplateFuncsNamespace{