✨ Added site TimeZone support
This commit is contained in:
parent
0f909007d8
commit
5b032f2c99
|
@ -1,10 +1,13 @@
|
|||
package domain
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"golang.org/x/text/language"
|
||||
)
|
||||
|
||||
type Site struct {
|
||||
Language language.Tag
|
||||
TimeZone *time.Location
|
||||
Title string
|
||||
}
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue