From efa5760db5ef39ae084bfccb5b8f756c7b117a2a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B8rn=20Erik=20Pedersen?= Date: Tue, 27 Jul 2021 13:45:05 +0200 Subject: Add timezone support for front matter dates without one Fixes #8810 --- langs/language.go | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) (limited to 'langs') diff --git a/langs/language.go b/langs/language.go index 5f5e8ddef..6f39848cf 100644 --- a/langs/language.go +++ b/langs/language.go @@ -17,6 +17,7 @@ import ( "sort" "strings" "sync" + "time" translators "github.com/bep/gotranslators" "github.com/go-playground/locales" @@ -71,10 +72,13 @@ type Language struct { paramsMu sync.Mutex paramsSet bool - // Used for date formatting etc. We don't want this exported to the + // Used for date formatting etc. We don't want these exported to the // templates. // TODO(bep) do the same for some of the others. translator locales.Translator + + locationInit sync.Once + location *time.Location } func (l *Language) String() string { @@ -244,9 +248,25 @@ func (l *Language) IsSet(key string) bool { return l.Cfg.IsSet(key) } +func (l *Language) getLocation() *time.Location { + l.locationInit.Do(func() { + location, err := time.LoadLocation(l.GetString("timeZone")) + if err != nil { + location = time.UTC + } + l.location = location + }) + + return l.location +} + // Internal access to unexported Language fields. // This construct is to prevent them from leaking to the templates. func GetTranslator(l *Language) locales.Translator { return l.translator } + +func GetLocation(l *Language) *time.Location { + return l.getLocation() +} -- cgit v1.2.3