Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for time.Duration to durationRound and duration #420

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions date.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ func duration(sec interface{}) string {
switch value := sec.(type) {
default:
n = 0
case time.Duration:
n = int64(value / time.Second)
case string:
n, _ = strconv.ParseInt(value, 10, 64)
case int64:
Expand All @@ -99,6 +101,8 @@ func durationRound(duration interface{}) string {
switch duration := duration.(type) {
default:
d = 0
case time.Duration:
d = duration
case string:
d, _ = time.ParseDuration(duration)
case int64:
Expand Down
6 changes: 6 additions & 0 deletions date_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,9 @@ func TestDuration(t *testing.T) {
if err := runtv(tpl, "1m1s", map[string]interface{}{"Secs": "61"}); err != nil {
t.Error(err)
}
if err := runtv(tpl, "1m15s", map[string]interface{}{"Secs": time.Duration(time.Second * 75)}); err != nil {
t.Error(err)
}
if err := runtv(tpl, "1h0m0s", map[string]interface{}{"Secs": "3600"}); err != nil {
t.Error(err)
}
Expand All @@ -108,6 +111,9 @@ func TestDuration(t *testing.T) {

func TestDurationRound(t *testing.T) {
tpl := "{{ durationRound .Time }}"
if err := runtv(tpl, "1m", map[string]interface{}{"Time": time.Duration(time.Second * 75)}); err != nil {
t.Error(err)
}
if err := runtv(tpl, "2h", map[string]interface{}{"Time": "2h5s"}); err != nil {
t.Error(err)
}
Expand Down