Add timezone support for front matter dates without one

Fixes #8810
This commit is contained in:
Bjørn Erik Pedersen 2021-07-27 13:45:05 +02:00
parent a57dda854b
commit efa5760db5
10 changed files with 196 additions and 73 deletions

View file

@ -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()
}