diff options
Diffstat (limited to 'common')
-rw-r--r-- | common/htime/time.go | 12 | ||||
-rw-r--r-- | common/loggers/loggers.go | 5 |
2 files changed, 15 insertions, 2 deletions
diff --git a/common/htime/time.go b/common/htime/time.go index 552608b6f..d854e9312 100644 --- a/common/htime/time.go +++ b/common/htime/time.go @@ -17,6 +17,7 @@ import ( "strings" "time" + "github.com/bep/clock" "github.com/spf13/cast" toml "github.com/pelletier/go-toml/v2" @@ -74,6 +75,7 @@ var ( "November", "December", } + Clock = clock.Start(time.Now()) ) func NewTimeFormatter(ltr locales.Translator) TimeFormatter { @@ -148,3 +150,13 @@ 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. +// 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) +} diff --git a/common/loggers/loggers.go b/common/loggers/loggers.go index 14c76ae45..6b73c9f32 100644 --- a/common/loggers/loggers.go +++ b/common/loggers/loggers.go @@ -24,6 +24,7 @@ import ( "runtime" "time" + "github.com/gohugoio/hugo/common/htime" "github.com/gohugoio/hugo/common/terminal" jww "github.com/spf13/jwalterweatherman" @@ -176,7 +177,7 @@ func (l *logger) Out() io.Writer { // PrintTimerIfDelayed prints a time statement to the FEEDBACK logger // if considerable time is spent. func (l *logger) PrintTimerIfDelayed(start time.Time, name string) { - elapsed := time.Since(start) + elapsed := htime.Since(start) milli := int(1000 * elapsed.Seconds()) if milli < 500 { return @@ -185,7 +186,7 @@ func (l *logger) PrintTimerIfDelayed(start time.Time, name string) { } func (l *logger) PrintTimer(start time.Time, name string) { - elapsed := time.Since(start) + elapsed := htime.Since(start) milli := int(1000 * elapsed.Seconds()) l.Printf("%s in %v ms", name, milli) } |