Skip to content

Commit c99a052

Browse files
committed
added timezone to time function
1 parent 9e9dec1 commit c99a052

File tree

3 files changed

+89
-13
lines changed

3 files changed

+89
-13
lines changed

funcmap_test.go

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,21 @@ package funcmap
66

77
import (
88
"bytes"
9-
"testing"
109
"text/template"
1110
)
1211

13-
func TestSomething(t *testing.T) {
14-
buf := new(bytes.Buffer)
15-
tmpl, _ := template.New("_").Funcs(Funcs).Parse(testT)
16-
err := tmpl.Execute(buf, map[string]interface{}{
17-
"Num": float64(32.5),
18-
})
12+
// helper function parses and applies the template and returns the
13+
// results in string format.
14+
func execute(text string, data map[string]interface{}) (string, error) {
15+
var buf bytes.Buffer
16+
t, err := template.New("_").Funcs(Funcs).Parse(text)
1917
if err != nil {
20-
t.Error(err)
18+
return buf.String(), err
2119
}
22-
println(buf.String())
20+
err = t.Execute(&buf, data)
21+
return buf.String(), err
2322
}
2423

25-
var testT = `{{ printf "hello %s %s world" (upper "foo" ) (lower .Num ) }}`
26-
2724
// https://github.com/leekchan/gtf
2825
// https://github.com/Masterminds/sprig
2926

time.go

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,31 @@
44

55
package funcmap
66

7-
import "time"
7+
import (
8+
"fmt"
9+
"time"
10+
)
811

912
// Time converts the textual representation of the datetime
1013
// string into a time.Time interface.
11-
func Time(v interface{}) (interface{}, error) {
14+
func Time(v interface{}, args ...interface{}) (interface{}, error) {
1215
t, err := toTimeE(v)
1316
if err != nil {
1417
return nil, err
1518
}
1619

20+
if len(args) > 0 {
21+
locStr, err := toStringE(args[0])
22+
if err != nil {
23+
return nil, err
24+
}
25+
loc, err := time.LoadLocation(locStr)
26+
if err != nil {
27+
return nil, err
28+
}
29+
return t.In(loc), nil
30+
}
31+
fmt.Println(t)
1732
return t, nil
1833
}
1934

@@ -22,6 +37,7 @@ func Time(v interface{}) (interface{}, error) {
2237
// the time.Time value. These are formatted with the layout
2338
// string
2439
func TimeFormat(layout string, v interface{}) (string, error) {
40+
println("TIMEFORMAT")
2541
t, err := toTimeE(v)
2642
if err != nil {
2743
return "", err

time_test.go

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,66 @@
33
// license that can be found in the LICENSE file.
44

55
package funcmap
6+
7+
import (
8+
"testing"
9+
)
10+
11+
func TestTime(t *testing.T) {
12+
tests := []struct {
13+
data map[string]interface{}
14+
text string
15+
want string
16+
}{
17+
{
18+
data: map[string]interface{}{"Timestamp": "2021-09-03 07:13:02 -0700 MST"},
19+
text: `{{ time .Timestamp }}`,
20+
want: "2021-09-03 07:13:02 -0700 MST",
21+
},
22+
{
23+
data: map[string]interface{}{"Timestamp": 1630678382},
24+
text: `{{ time .Timestamp "UTC" }}`,
25+
want: "2021-09-03 14:13:02 +0000 UTC",
26+
},
27+
{
28+
data: map[string]interface{}{"Timestamp": 1630678382},
29+
text: `{{ time .Timestamp "MST" }}`,
30+
want: "2021-09-03 07:13:02 -0700 MST",
31+
},
32+
}
33+
for _, test := range tests {
34+
out, err := execute(test.text, test.data)
35+
if err != nil {
36+
t.Error(err)
37+
} else if out != test.want {
38+
t.Errorf("Want template result %s, got %s", test.want, out)
39+
}
40+
}
41+
}
42+
43+
func TestTimeFormat(t *testing.T) {
44+
tests := []struct {
45+
data map[string]interface{}
46+
text string
47+
want string
48+
}{
49+
{
50+
data: map[string]interface{}{"Timestamp": "2021-09-03 07:13:02 -0700 MST"},
51+
text: `{{ dateFormat "2006-01-02" .Timestamp }}`,
52+
want: "2021-09-03",
53+
},
54+
{
55+
data: map[string]interface{}{"Timestamp": 1630678382},
56+
text: `{{ dateFormat "2006-01-02" .Timestamp }}`,
57+
want: "2021-09-03",
58+
},
59+
}
60+
for _, test := range tests {
61+
out, err := execute(test.text, test.data)
62+
if err != nil {
63+
t.Error(err)
64+
} else if out != test.want {
65+
t.Errorf("Want template result %s, got %s", test.want, out)
66+
}
67+
}
68+
}

0 commit comments

Comments
 (0)