Skip to content

Commit dc1e1a2

Browse files
Add date and tz functions to templates (#3812)
* Add date and tz functions to templates This commit adds the date and tz functions to templates. This means users can now format time in a specified format and also change the timezone to their specific locale. An example of how these functions work, and can be composed together, can be seen here: {{ .StartsAt | tz "Europe/Paris" | date "15:04:05 MST" }} Signed-off-by: George Robinson <[email protected]> --------- Signed-off-by: George Robinson <[email protected]>
1 parent cb9724d commit dc1e1a2

File tree

3 files changed

+41
-6
lines changed

3 files changed

+41
-6
lines changed

docs/notifications.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,3 +95,5 @@ templating.
9595
| join | sep string, s []string | [strings.Join](http://golang.org/pkg/strings/#Join), concatenates the elements of s to create a single string. The separator string sep is placed between elements in the resulting string. (note: argument order inverted for easier pipelining in templates.) |
9696
| safeHtml | text string | [html/template.HTML](https://golang.org/pkg/html/template/#HTML), Marks string as HTML not requiring auto-escaping. |
9797
| stringSlice | ...string | Returns the passed strings as a slice of strings. |
98+
| date | string, time.Time | Returns the text representation of the time in the specified format. For documentation on formats refer to [pkg.go.dev/time](https://pkg.go.dev/time#pkg-constants). |
99+
| tz | string, time.Time | Returns the time in the timezone. For example, Europe/Paris. |

template/template.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,18 @@ var DefaultFuncs = FuncMap{
192192
"stringSlice": func(s ...string) []string {
193193
return s
194194
},
195+
// date returns the text representation of the time in the specified format.
196+
"date": func(fmt string, t time.Time) string {
197+
return t.Format(fmt)
198+
},
199+
// tz returns the time in the timezone.
200+
"tz": func(name string, t time.Time) (time.Time, error) {
201+
loc, err := time.LoadLocation(name)
202+
if err != nil {
203+
return time.Time{}, err
204+
}
205+
return t.In(loc), nil
206+
},
195207
}
196208

197209
// Pair is a key/value string pair.

template/template_test.go

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -473,10 +473,11 @@ func TestTemplateFuncs(t *testing.T) {
473473
require.NoError(t, err)
474474

475475
for _, tc := range []struct {
476-
title string
477-
in string
478-
data interface{}
479-
exp string
476+
title string
477+
in string
478+
data interface{}
479+
exp string
480+
expErr string
480481
}{{
481482
title: "Template using toUpper",
482483
in: `{{ "abc" | toUpper }}`,
@@ -506,6 +507,21 @@ func TestTemplateFuncs(t *testing.T) {
506507
title: "Template using reReplaceAll",
507508
in: `{{ reReplaceAll "ab" "AB" "abc" }}`,
508509
exp: "ABc",
510+
}, {
511+
title: "Template using date",
512+
in: `{{ . | date "2006-01-02" }}`,
513+
data: time.Date(2024, 1, 1, 8, 15, 30, 0, time.UTC),
514+
exp: "2024-01-01",
515+
}, {
516+
title: "Template using tz",
517+
in: `{{ . | tz "Europe/Paris" }}`,
518+
data: time.Date(2024, 1, 1, 8, 15, 30, 0, time.UTC),
519+
exp: "2024-01-01 09:15:30 +0100 CET",
520+
}, {
521+
title: "Template using invalid tz",
522+
in: `{{ . | tz "Invalid/Timezone" }}`,
523+
data: time.Date(2024, 1, 1, 8, 15, 30, 0, time.UTC),
524+
expErr: "template: :1:7: executing \"\" at <tz \"Invalid/Timezone\">: error calling tz: unknown time zone Invalid/Timezone",
509525
}} {
510526
tc := tc
511527
t.Run(tc.title, func(t *testing.T) {
@@ -515,8 +531,13 @@ func TestTemplateFuncs(t *testing.T) {
515531
go func() {
516532
defer wg.Done()
517533
got, err := tmpl.ExecuteTextString(tc.in, tc.data)
518-
require.NoError(t, err)
519-
require.Equal(t, tc.exp, got)
534+
if tc.expErr == "" {
535+
require.NoError(t, err)
536+
require.Equal(t, tc.exp, got)
537+
} else {
538+
require.EqualError(t, err, tc.expErr)
539+
require.Empty(t, got)
540+
}
520541
}()
521542
}
522543
wg.Wait()

0 commit comments

Comments
 (0)