Skip to content

Commit f4bf214

Browse files
committed
tpl/time: Add time.Duration and time.ParseDuration template funcs
And with time.Duration with the convenient alias `duration`: ``` {{ mul 60 60 | duration "second" }} ``` Fixes #3828
1 parent 0462c96 commit f4bf214

File tree

3 files changed

+107
-0
lines changed

3 files changed

+107
-0
lines changed

‎tpl/time/init.go‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,20 @@ func init() {
6565
},
6666
)
6767

68+
ns.AddMethodMapping(ctx.Duration,
69+
[]string{"duration"},
70+
[][2]string{
71+
{`{{ mul 60 60 | duration "second" }}`, `1h0m0s`},
72+
},
73+
)
74+
75+
ns.AddMethodMapping(ctx.ParseDuration,
76+
nil,
77+
[][2]string{
78+
{`{{ "1h12m10s" | time.ParseDuration }}`, `1h12m10s`},
79+
},
80+
)
81+
6882
return ns
6983

7084
}

‎tpl/time/time.go‎

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
package time
1515

1616
import (
17+
"fmt"
1718
_time "time"
1819

1920
"github.com/spf13/cast"
@@ -54,3 +55,52 @@ func (ns *Namespace) Format(layout string, v interface{}) (string, error) {
5455
func (ns *Namespace) Now() _time.Time {
5556
return _time.Now()
5657
}
58+
59+
// ParseDuration parses a duration string.
60+
// A duration string is a possibly signed sequence of
61+
// decimal numbers, each with optional fraction and a unit suffix,
62+
// such as "300ms", "-1.5h" or "2h45m".
63+
// Valid time units are "ns", "us" (or "µs"), "ms", "s", "m", "h".
64+
// See https://golang.org/pkg/time/#ParseDuration
65+
func (ns *Namespace) ParseDuration(in interface{}) (_time.Duration, error) {
66+
s, err := cast.ToStringE(in)
67+
if err != nil {
68+
return 0, err
69+
}
70+
71+
return _time.ParseDuration(s)
72+
}
73+
74+
var durationUnits = map[string]_time.Duration{
75+
"nanosecond": _time.Nanosecond,
76+
"ns": _time.Nanosecond,
77+
"microsecond": _time.Microsecond,
78+
"us": _time.Microsecond,
79+
"µs": _time.Microsecond,
80+
"millisecond": _time.Millisecond,
81+
"ms": _time.Millisecond,
82+
"second": _time.Second,
83+
"s": _time.Second,
84+
"minute": _time.Minute,
85+
"m": _time.Minute,
86+
"hour": _time.Hour,
87+
"h": _time.Hour,
88+
}
89+
90+
// Duration converts the given number to a time.Duration.
91+
// Unit is one of nanosecond/ns, microsecond/us/µs, millisecond/ms, second/s, minute/m or hour/h.
92+
func (ns *Namespace) Duration(unit interface{}, number interface{}) (_time.Duration, error) {
93+
unitStr, err := cast.ToStringE(unit)
94+
if err != nil {
95+
return 0, err
96+
}
97+
unitDuration, found := durationUnits[unitStr]
98+
if !found {
99+
return 0, fmt.Errorf("%q is not a valid duration unit", unit)
100+
}
101+
n, err := cast.ToInt64E(number)
102+
if err != nil {
103+
return 0, err
104+
}
105+
return _time.Duration(n) * unitDuration, nil
106+
}

‎tpl/time/time_test.go‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,46 @@ func TestFormat(t *testing.T) {
5555
}
5656
}
5757
}
58+
59+
func TestDuration(t *testing.T) {
60+
t.Parallel()
61+
62+
ns := New()
63+
64+
for i, test := range []struct {
65+
unit interface{}
66+
num interface{}
67+
expect interface{}
68+
}{
69+
{"nanosecond", 10, 10 * time.Nanosecond},
70+
{"ns", 10, 10 * time.Nanosecond},
71+
{"microsecond", 20, 20 * time.Microsecond},
72+
{"us", 20, 20 * time.Microsecond},
73+
{"µs", 20, 20 * time.Microsecond},
74+
{"millisecond", 20, 20 * time.Millisecond},
75+
{"ms", 20, 20 * time.Millisecond},
76+
{"second", 30, 30 * time.Second},
77+
{"s", 30, 30 * time.Second},
78+
{"minute", 20, 20 * time.Minute},
79+
{"m", 20, 20 * time.Minute},
80+
{"hour", 20, 20 * time.Hour},
81+
{"h", 20, 20 * time.Hour},
82+
{"hours", 20, false},
83+
{"hour", "30", 30 * time.Hour},
84+
} {
85+
result, err := ns.Duration(test.unit, test.num)
86+
if b, ok := test.expect.(bool); ok && !b {
87+
if err == nil {
88+
t.Errorf("[%d] Duration didn't return an expected error, got %v", i, result)
89+
}
90+
} else {
91+
if err != nil {
92+
t.Errorf("[%d] Duration failed: %s", i, err)
93+
continue
94+
}
95+
if result != test.expect {
96+
t.Errorf("[%d] Duration got %v but expected %v", i, result, test.expect)
97+
}
98+
}
99+
}
100+
}

0 commit comments

Comments
 (0)