hugo
hugo copied to clipboard
Regression in TOML LocalDate Format Support
What version of Hugo are you using (hugo version
)?
- Regression introduced in
hugo v0.87.0-B0C541E4 linux/amd64 BuildDate=2021-08-03T10:57:28Z VendorInfo=gohugoio
- Regression exists in
hugo v0.99.1-d524067382e60ce2a2248c3133a1b3af206b6ef1 linux/amd64 BuildDate=2022-05-18T11:18:14Z VendorInfo=gohugoio
- Last confirmed release where this pattern works is
hugo v0.86.0-41C6C52E linux/amd64 BuildDate=2021-07-21T09:53:14Z VendorInfo=gohugoio
Issue
This is quite a minor issue as there is a solution. Feel free to close this if I've misunderstood and this isn't a valid regression.
In Hugo before v0.87 you could use the .Format
method on a toml.LocalDate
date. Since then with the new TOML backend and updated .Format
functionality it errors.
For hugo <= 0.86
, given the toml:
date = 2021-10-19
This template renders correctly:
{{ with index . "date" }}
<dt class="cite__key">Date</dt>
<dd class="cite__value">
<time
datetime='{{ .Format "2006-01-02" }}'>
{{ .Format "2006-01-02" }}
</time>
</dd>
{{end}}
From hugo >= 0.87
it errors with:
Error: Error building site: failed to render pages: render of "page" failed: "/path/to/hugo/template.html:42:25": execute of template failed: template: hugo/template.html:42:25: executing "main" at <.Format>: can't evaluate field Format in type toml.LocalDate
However, calling time.Format
directly and passing in toml.LocalDate
as an argument does correctly render.
{{ with index . "date" }}
<dt class="cite__key">Date</dt>
<dd class="cite__value">
<time
datetime='{{ time.Format "2006-01-02" . }}'>
{{ time.Format "2006-01-02" . }}
</time>
</dd>
{{end}}
Shouldn't toml.LocalDate
(which can be coerced into a time.Time
) be treated as a just like regular a TOML Offset Date-Time (which I think are automatically time.Time
in go-toml
).
I don't think we can/want to fix this, though.
When using toml.LocalDate
and toml.LocalDateTime
values in the four predefined front matter date fields, the values are converted to time.Time
, so the .Format
method works as expected.
This issue is limited to custom front matter date fields, and date values from local and remote data files.
As noted here, it would be nice if pelletier/go-toml had an option to unmarshal toml.LocalDate
and toml.LocalDateTime
to time.Time
in UTC. Or we walk the tree and do it ourselves.
We've had to make adjustments here and there to accommodate toml.LocalDate
and toml.LocalDateTime
values:
- https://github.com/gohugoio/hugo/pull/9981
- https://github.com/gohugoio/hugo/pull/11706
It would be nice if we could just handle it while unmarshaling.
I've clarified the time.Format documentation here.