How to perform time functions in go template?

686 views
Skip to first unread message

Deepika

unread,
Jun 12, 2020, 11:43:56 AM6/12/20
to golang-nuts
Objective: To subtract 10 minutes from go's time.Time object(".StartsAt") in html template (In specific the prometheus alertmanager's email template: email_template.tmpl)
Code: <tr><td>error_timestamp</td><td>{{ .StartsAt }}</td></tr>

Alertmanager version: 0.20.0

Options tried and corresponding errors :

1. <td>{{ .StartsAt.Add(-10*time.Minute) }}</td>  ::::::: Unexpected operand "("
2. <td>{{ .StartsAt.Add "-10*time.Minute" }}</td>  :::::::  expected integer; found "-10*time.Minute\""
3.  {{ $timestamp := .StartsAt.Add "10m" }} :::::::::::::::: wrong type for value; expected time.Duration; got string
4.  {{ $timestamp :=  Add .StartsAt "10m" }} ::::::::::: Add function not defined
5.  {{ $timestamp :=  .StartsAt.Add -10*time.Minute }} :::::::::::: time function not defined
6.  {{ $timestamp :=  .StartsAt.Add (-10*time.Minute) }} :::::::::::: illegal operand *
7.  {{ $timestamp :=  .StartsAt.Add (time.duration "10m") }} :::::::::::: duration function not defined 
8. {{ $timestamp :=  .StartsAt.Add (time.ParseDuration "10m") }} ::::::::::::  time function not defined
9.  {{ $timestamp :=  .StartsAt.Add 10 }} ::::::::::: No error. But no change in timestamp too.

Tamás Gulácsi

unread,
Jun 12, 2020, 1:12:01 PM6/12/20
to golang-nuts
Either add a TimeAdd function to the template's FuncMap, or use a constant:
 
    {{ $timestamp := .StartsAt.Add 60000000000 }} //10m is 60000000000 ns
Reply all
Reply to author
Forward
0 new messages