diff options
author | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2021-07-27 13:45:05 +0200 |
---|---|---|
committer | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2021-07-27 19:02:48 +0200 |
commit | efa5760db5ef39ae084bfccb5b8f756c7b117a2a (patch) | |
tree | 7c70d6665f34ca25a443f5a51ce956a3fa729282 /resources | |
parent | a57dda854b5efd3429af5f0b1564fc9d9d5439b9 (diff) |
Add timezone support for front matter dates without one
Fixes #8810
Diffstat (limited to 'resources')
-rw-r--r-- | resources/page/pagemeta/page_frontmatter.go | 13 | ||||
-rw-r--r-- | resources/page/pagemeta/page_frontmatter_test.go | 3 |
2 files changed, 9 insertions, 7 deletions
diff --git a/resources/page/pagemeta/page_frontmatter.go b/resources/page/pagemeta/page_frontmatter.go index 3184b444d..8e03c5f88 100644 --- a/resources/page/pagemeta/page_frontmatter.go +++ b/resources/page/pagemeta/page_frontmatter.go @@ -70,6 +70,9 @@ type FrontMatterDescriptor struct { // This is the Page's Slug etc. PageURLs *URLPath + + // The Location to use to parse dates without time zone info. + Location *time.Location } var dateFieldAliases = map[string][]string{ @@ -119,7 +122,7 @@ func (f FrontMatterHandler) IsDateKey(key string) bool { // A Zero date is a signal that the name can not be parsed. // This follows the format as outlined in Jekyll, https://jekyllrb.com/docs/posts/: // "Where YEAR is a four-digit number, MONTH and DAY are both two-digit numbers" -func dateAndSlugFromBaseFilename(name string) (time.Time, string) { +func dateAndSlugFromBaseFilename(location *time.Location, name string) (time.Time, string) { withoutExt, _ := paths.FileAndExt(name) if len(withoutExt) < 10 { @@ -127,9 +130,7 @@ func dateAndSlugFromBaseFilename(name string) (time.Time, string) { return time.Time{}, "" } - // Note: Hugo currently have no custom timezone support. - // We will have to revisit this when that is in place. - d, err := time.Parse("2006-01-02", withoutExt[:10]) + d, err := cast.ToTimeInDefaultLocationE(withoutExt[:10], location) if err != nil { return time.Time{}, "" } @@ -370,7 +371,7 @@ func (f *frontmatterFieldHandlers) newDateFieldHandler(key string, setter func(d return false, nil } - date, err := cast.ToTimeE(v) + date, err := cast.ToTimeInDefaultLocationE(v, d.Location) if err != nil { return false, nil } @@ -388,7 +389,7 @@ func (f *frontmatterFieldHandlers) newDateFieldHandler(key string, setter func(d func (f *frontmatterFieldHandlers) newDateFilenameHandler(setter func(d *FrontMatterDescriptor, t time.Time)) frontMatterFieldHandler { return func(d *FrontMatterDescriptor) (bool, error) { - date, slug := dateAndSlugFromBaseFilename(d.BaseFilename) + date, slug := dateAndSlugFromBaseFilename(d.Location, d.BaseFilename) if date.IsZero() { return false, nil } diff --git a/resources/page/pagemeta/page_frontmatter_test.go b/resources/page/pagemeta/page_frontmatter_test.go index 6e3833b0f..653ced59e 100644 --- a/resources/page/pagemeta/page_frontmatter_test.go +++ b/resources/page/pagemeta/page_frontmatter_test.go @@ -53,7 +53,7 @@ func TestDateAndSlugFromBaseFilename(t *testing.T) { expecteFDate, err := time.Parse("2006-01-02", test.date) c.Assert(err, qt.IsNil) - gotDate, gotSlug := dateAndSlugFromBaseFilename(test.name) + gotDate, gotSlug := dateAndSlugFromBaseFilename(time.UTC, test.name) c.Assert(gotDate, qt.Equals, expecteFDate) c.Assert(gotSlug, qt.Equals, test.slug) @@ -67,6 +67,7 @@ func newTestFd() *FrontMatterDescriptor { Params: make(map[string]interface{}), Dates: &resource.Dates{}, PageURLs: &URLPath{}, + Location: time.UTC, } } |