Handle toml.LocalDate and toml.LocalDateTime in front matter
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Wed, 28 Jul 2021 16:02:42 +0000 (18:02 +0200)
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Wed, 28 Jul 2021 16:02:42 +0000 (18:02 +0200)
See #8801

common/htime/time.go
hugolib/dates_test.go
resources/page/pagemeta/page_frontmatter.go
tpl/time/time.go

index cd294b3a2fe85569f1a9472baf3b3b0b747287e0..f5a19b961e8a8b3be977f8c531a62b160677695a 100644 (file)
@@ -17,6 +17,10 @@ import (
        "strings"
        "time"
 
+       "github.com/spf13/cast"
+
+       toml "github.com/pelletier/go-toml/v2"
+
        "github.com/go-playground/locales"
 )
 
@@ -125,3 +129,13 @@ func (f TimeFormatter) Format(t time.Time, layout string) string {
 
        return s
 }
+
+func ToTimeInDefaultLocationE(i interface{}, location *time.Location) (tim time.Time, err error) {
+       switch vv := i.(type) {
+       case toml.LocalDate:
+               return vv.AsTime(location), nil
+       case toml.LocalDateTime:
+               return vv.AsTime(location), nil
+       }
+       return cast.ToTimeInDefaultLocationE(i, location)
+}
index 6fe3adfa73d99fc2441bd9b4e9e87588f854d10d..c9a33e0f8b0c9887311643fbf78f9799be0c9f32 100644 (file)
@@ -104,6 +104,7 @@ expiryDate=%s
                        if quoted {
                                return fmt.Sprintf("%q", d)
                        }
+
                        return d
                }
 
@@ -124,7 +125,6 @@ expiryDate=%s
                "short-date-yaml-qouted.nn.md", createPageContent(pageTemplYaml, shortDateTempl, true),
                "long-date-yaml-unqouted.en.md", createPageContent(pageTemplYaml, longDateTempl, false),
                "long-date-yaml-unqouted.nn.md", createPageContent(pageTemplYaml, longDateTempl, false),
-
                // TOML
                "short-date-toml-unqouted.en.md", createPageContent(pageTemplTOML, shortDateTempl, false),
                "short-date-toml-unqouted.nn.md", createPageContent(pageTemplTOML, shortDateTempl, false),
@@ -180,8 +180,8 @@ ExpiryDate: 2099-07-13 15:28:01 +0000 UTC`
        // TOML
        // These fails: TOML (Burnt Sushi) defaults to local timezone.
        // TODO(bep) check go-toml
-       //      b.AssertFileContent("public/en/short-date-toml-unqouted/index.html", expectShortDateEn)
-       //  b.AssertFileContent("public/nn/short-date-toml-unqouted/index.html", expectShortDateNn)
+       b.AssertFileContent("public/en/short-date-toml-unqouted/index.html", expectShortDateEn)
+       b.AssertFileContent("public/nn/short-date-toml-unqouted/index.html", expectShortDateNn)
        b.AssertFileContent("public/en/short-date-toml-qouted/index.html", expectShortDateEn)
        b.AssertFileContent("public/nn/short-date-toml-qouted/index.html", expectShortDateNn)
 
index 8e03c5f88ab7cf99256d5db6ae24b35ed145862f..d473c69cec5b287ebc073badb33ba6184acf09e8 100644 (file)
@@ -17,6 +17,7 @@ import (
        "strings"
        "time"
 
+       "github.com/gohugoio/hugo/common/htime"
        "github.com/gohugoio/hugo/common/paths"
 
        "github.com/gohugoio/hugo/common/loggers"
@@ -130,7 +131,7 @@ func dateAndSlugFromBaseFilename(location *time.Location, name string) (time.Tim
                return time.Time{}, ""
        }
 
-       d, err := cast.ToTimeInDefaultLocationE(withoutExt[:10], location)
+       d, err := htime.ToTimeInDefaultLocationE(withoutExt[:10], location)
        if err != nil {
                return time.Time{}, ""
        }
@@ -371,7 +372,7 @@ func (f *frontmatterFieldHandlers) newDateFieldHandler(key string, setter func(d
                        return false, nil
                }
 
-               date, err := cast.ToTimeInDefaultLocationE(v, d.Location)
+               date, err := htime.ToTimeInDefaultLocationE(v, d.Location)
                if err != nil {
                        return false, nil
                }
index a59d85b060071a9163f3222a520ce135e2a6b81a..f91bf1047b6a51c71b8080e95f080fb4c1861844 100644 (file)
@@ -55,7 +55,7 @@ func (ns *Namespace) AsTime(v interface{}, args ...interface{}) (interface{}, er
                }
        }
 
-       return cast.ToTimeInDefaultLocationE(v, loc)
+       return htime.ToTimeInDefaultLocationE(v, loc)
 
 }