Added site TimeZone support

This commit is contained in:
Maxim Lebedev 2023-11-08 08:08:17 +06:00
parent 0f909007d8
commit 5b032f2c99
Signed by: toby3d
GPG Key ID: 1F14E25B7C119FC5
2 changed files with 38 additions and 3 deletions

View File

@ -1,10 +1,13 @@
package domain
import (
"time"
"golang.org/x/text/language"
)
type Site struct {
Language language.Tag
TimeZone *time.Location
Title string
}

View File

@ -4,6 +4,7 @@ import (
"context"
"fmt"
"io/fs"
"time"
"github.com/adrg/frontmatter"
"golang.org/x/text/language"
@ -15,9 +16,14 @@ import (
type (
Site struct {
Title string `yaml:"title"`
Params map[string]any `yaml:",inline"`
Content []byte `yaml:"-"`
Title string `yaml:"title"`
TimeZone TimeZone `yaml:"timeZone"`
Params map[string]any `yaml:",inline"`
Content []byte `yaml:"-"`
}
TimeZone struct {
*time.Location `yaml:"-"`
}
fileSystemSiteRepository struct {
@ -58,5 +64,31 @@ func (repo *fileSystemSiteRepository) Get(ctx context.Context, lang language.Tag
return &domain.Site{
Language: lang,
Title: data.Title,
TimeZone: data.TimeZone.Location,
}, nil
}
func (tz *TimeZone) UnmarshalYAML(value *yaml.Node) error {
if value.IsZero() {
tz.Location = time.UTC
return nil
}
loc, err := time.LoadLocation(value.Value)
if err != nil {
return fmt.Errorf("cannot parse timeZone value '%v': %w", value, err)
}
tz.Location = loc
return nil
}
func (tz TimeZone) MarshalYAML() (any, error) {
if tz.Location == nil {
return time.UTC.String(), nil
}
return tz.Location.String(), nil
}