Format date as milliseconds using template functions

I am trying to implement a calendar using bootstrap-calendar which requires event times in Unix times as milliseconds.

The dateFormat template function casts a timestamp string to a time.Time then returns the string representation according to the layout provided. Is it possible to cast a timestamp to a time.Time without formatting it so I can use the fields and functions on the type?

eg. .Params.eventstart.sec to get the sec field of the time.Time type
or .Params.eventstart.Unix to call the Unix function

I can only find references to $.Now.Unix

You gen get to any time.Time attribute and func that is exported (i.e. starts with capital letter), e.g.:

https://golang.org/pkg/time/#Time.UnixNano.

If .Params.eventstart.Unix fails it must mean that evenstart isn’t really a proper date. You should have a look at your config format of choice for a solution to that (TOML, JSON, YAML).

That’s what I am asking though. .Params.eventstart is a timestamp, a string in the front matter. What I’m asking for is a way to cast it to a time.Time struct using template functions because I can’t find a way to do it.

If I remember correctly, we have no func for that … but I would make it “not to be a string in front matter”.

That said, I would welcome a time template func (name in line with int and string funcs).

Yeah I’ve been playing around with YAML tags but it has made no difference

---
date: 2016-05-10T21:32:29Z
start: !!timestamp 2016-05-28T10:30:00.00+10:00
end: !!timestamp 2016-05-28T14:30:00.00+10:00

+1 for the time func, though I may have stuffed up the merge request
https://github.com/spf13/hugo/issues/2328

On a side note, I just realised Javascript/ECMAscript standards use a time value of Unix time in milliseconds. It would be nice if Go provided a UnixMilli() natively to save converting between Unix() or UnixNano().

I think the Go people have bigger time struggles than to worry about converting between nano and milliseconds, see:

https://github.com/golang/go/issues/16606

This is the 5th time or something in a week that Apple has delayed the 1.7 Go release by changing the Gettimeofday syscall…