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

feat: add time helpers #3720

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
71 changes: 71 additions & 0 deletions template/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,16 @@ package template

import (
"bytes"
"fmt"
tmplhtml "html/template"
"io"
"math"
"net/url"
"path"
"path/filepath"
"regexp"
"sort"
"strconv"
"strings"
tmpltext "text/template"
"time"
Expand Down Expand Up @@ -192,6 +195,74 @@ var DefaultFuncs = FuncMap{
"stringSlice": func(s ...string) []string {
return s
},
"since": time.Since,
"humanizeDuration": func(i interface{}) (string, error) {
v, err := convertToFloat(i)
if err != nil {
return "", err
}

if math.IsNaN(v) || math.IsInf(v, 0) {
return fmt.Sprintf("%.4g", v), nil
}
if v == 0 {
return fmt.Sprintf("%.4gs", v), nil
}
if math.Abs(v) >= 1 {
sign := ""
if v < 0 {
sign = "-"
v = -v
}
duration := int64(v)
seconds := duration % 60
minutes := (duration / 60) % 60
hours := (duration / 60 / 60) % 24
days := duration / 60 / 60 / 24
// For days to minutes, we display seconds as an integer.
if days != 0 {
return fmt.Sprintf("%s%dd %dh %dm %ds", sign, days, hours, minutes, seconds), nil
}
if hours != 0 {
return fmt.Sprintf("%s%dh %dm %ds", sign, hours, minutes, seconds), nil
}
if minutes != 0 {
return fmt.Sprintf("%s%dm %ds", sign, minutes, seconds), nil
}
// For seconds, we display 4 significant digits.
return fmt.Sprintf("%s%.4gs", sign, v), nil
}
prefix := ""
for _, p := range []string{"m", "u", "n", "p", "f", "a", "z", "y"} {
if math.Abs(v) >= 1 {
break
}
prefix = p
v *= 1000
}
return fmt.Sprintf("%.4g%ss", v, prefix), nil
},
}

func convertToFloat(i interface{}) (float64, error) {
switch v := i.(type) {
case float64:
return v, nil
case string:
return strconv.ParseFloat(v, 64)
case int:
return float64(v), nil
case uint:
return float64(v), nil
case int64:
return float64(v), nil
case uint64:
return float64(v), nil
case time.Duration:
return v.Seconds(), nil
default:
return 0, fmt.Errorf("can't convert %T to float", v)
}
}

// Pair is a key/value string pair.
Expand Down
47 changes: 47 additions & 0 deletions template/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,53 @@ func TestTemplateExpansion(t *testing.T) {
},
exp: "[key2 key4]",
},
{
title: "Template using HumanizeDuration - seconds - float64",
in: "{{ range . }}{{ humanizeDuration . }}:{{ end }}",
data: []float64{0, 1, 60, 3600, 86400, 86400 + 3600, -(86400*2 + 3600*3 + 60*4 + 5), 899.99},
exp: "0s:1s:1m 0s:1h 0m 0s:1d 0h 0m 0s:1d 1h 0m 0s:-2d 3h 4m 5s:14m 59s:",
},
{
title: "Template using HumanizeDuration - seconds - string.",
in: "{{ range . }}{{ humanizeDuration . }}:{{ end }}",
data: []string{"0", "1", "60", "3600", "86400"},
exp: "0s:1s:1m 0s:1h 0m 0s:1d 0h 0m 0s:",
},
{
title: "Template using HumanizeDuration - subsecond and fractional seconds - float64.",
in: "{{ range . }}{{ humanizeDuration . }}:{{ end }}",
data: []float64{.1, .0001, .12345, 60.1, 60.5, 1.2345, 12.345},
exp: "100ms:100us:123.5ms:1m 0s:1m 0s:1.234s:12.35s:",
},
{
title: "Template using HumanizeDuration - subsecond and fractional seconds - string.",
in: "{{ range . }}{{ humanizeDuration . }}:{{ end }}",
data: []string{".1", ".0001", ".12345", "60.1", "60.5", "1.2345", "12.345"},
exp: "100ms:100us:123.5ms:1m 0s:1m 0s:1.234s:12.35s:",
},
{
title: "Template using HumanizeDuration - string with error.",
in: `{{ humanizeDuration "one" }}`,
fail: true,
},
{
title: "Template using HumanizeDuration - int.",
in: "{{ range . }}{{ humanizeDuration . }}:{{ end }}",
data: []int{0, -1, 1, 1234567},
exp: "0s:-1s:1s:14d 6h 56m 7s:",
},
{
title: "Template using HumanizeDuration - uint.",
in: "{{ range . }}{{ humanizeDuration . }}:{{ end }}",
data: []uint{0, 1, 1234567},
exp: "0s:1s:14d 6h 56m 7s:",
},
{
title: "Template using since",
in: "{{ . | since | humanizeDuration }}",
data: time.Now().Add(-1 * time.Hour),
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tbh I am not sure how to test this one properly without mocking time.Since, which as far as I know cannot be done without some kinds of workaround, this one looks ugly and flaky (as it relies on local time and will fail if the time between creating this variable and rendering is more than 1 second), but it kinda works, let me know if there's a better way to do it

exp: "1h 0m 0s",
},
} {
tc := tc
t.Run(tc.title, func(t *testing.T) {
Expand Down