.AddDate works for "now" but not for a time.Time variable


#1

Hello,

  • I am using (now.AddDate 1 0 0) without problem.
  • Documentation says .AddDate works for a time.Time variable.
  • When I try to apply .AddDate to a regular time.Time variable, i get a Hugo error.
  • I search on a lot of changelog but could’nt find anything about a change in this area.

Any light ? Idea ?

# config.toml
params]
    [params.global]
        myvariable  = "2019-05-01"
{{ $_x     := $.Page.Site.Params.global.myvariable | time }}
{{ $_x }}

this prints 2019-05-01 00:00:00 +0000 UTC as expected

When i try {{ $_x.AddDate 1 0 0 }} i get this error :slight_smile:

ERROR 2018/12/18 17:20:50 Failed to render pages: render of "page" failed: "/Users/didiergeorgieff/Documents/Git/lagouille.com/layouts/portfolio/single.html:25:6": execute of template failed: template: portfolio/single.html:24:3: executing "portfolio/single.html" at <partial "portfolio-s...>: error calling partial: "/Users/didiergeorgieff/Documents/Git/lagouille.com/layouts/partials/portfolio-single.html:25:6": execute of template failed: template:partials/portfolio-single.html:26:19: executing "partials/portfolio-single.html" at <partial "saison.html...>: error calling partial: "/Users/didiergeorgieff/Documents/Git/lagouille.com/layouts/partials/saison.html:25:6": execute of template failed: template: partials/saison.html:25:6: executing "partials/saison.html" at <$_x.adddate>: can't evaluate field adddate in type time.Time
github.com/gohugoio/hugo/hugolib.(*Site).renderPages
        /private/tmp/hugo-20181128-75639-15xr3tm/hugo-0.52/src/github.com/gohugoio/hugo/hugolib/site_render.go:65
github.com/gohugoio/hugo/hugolib.(*Site).render
        /private/tmp/hugo-20181128-75639-15xr3tm/hugo-0.52/src/github.com/gohugoio/hugo/hugolib/site.go:1082
github.com/gohugoio/hugo/hugolib.(*HugoSites).render
        /private/tmp/hugo-20181128-75639-15xr3tm/hugo-0.52/src/github.com/gohugoio/hugo/hugolib/hugo_sites_build.go:302
github.com/gohugoio/hugo/hugolib.(*HugoSites).Build
        /private/tmp/hugo-20181128-75639-15xr3tm/hugo-0.52/src/github.com/gohugoio/hugo/hugolib/hugo_sites_build.go:97
github.com/gohugoio/hugo/commands.(*commandeer).rebuildSites
        /private/tmp/hugo-20181128-75639-15xr3tm/hugo-0.52/src/github.com/gohugoio/hugo/commands/hugo.go:660
github.com/gohugoio/hugo/commands.(*commandeer).handleEvents
        /private/tmp/hugo-20181128-75639-15xr3tm/hugo-0.52/src/github.com/gohugoio/hugo/commands/hugo.go:942
github.com/gohugoio/hugo/commands.(*commandeer).newWatcher.func1
        /private/tmp/hugo-20181128-75639-15xr3tm/hugo-0.52/src/github.com/gohugoio/hugo/commands/hugo.go:730
runtime.goexit
        /usr/local/Cellar/go/1.11.2/libexec/src/runtime/asm_amd64.s:1333

#2

It’s most likely a stupid bug, see https://github.com/gohugoio/hugo/issues/5538

That said, if you remove the quotes => myvariable = 2019-05-01 you should not need the time func and this should work fine.


#3

Yes, it’s a bug, but a current workaround is to split the declaration in 2:

{{ $time := $.Params.MyDate }}
{{ $time = $time | time }}
PARAMS TIME: {{ $time.AddDate 1 0 0 }}

#4

Perfect !! Thanks a lot for the reactivity (and the few other stuff :slight_smile: )


#5

Note that this also works:

{{ $time := $.Params.MyDate | time }}
{{ $time = $time.AddDate 0 1 0 }}
PARAMS TIME: {{ $time.Format "2006-01-02" }}

But this fails:

{{ $_x :=  $.Params.MyDate | time }}
{{ $_x.AddDate 1 0 0 }}

Which I guess is a more unusual construct, which is the reason why we have not seen this issue before.