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 durationRound function #187

Merged
merged 2 commits into from
Sep 30, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
44 changes: 44 additions & 0 deletions date.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,50 @@ func dateAgo(date interface{}) string {
return duration.String()
}

func durationRound(duration interface{}) string {
var d time.Duration
switch duration := duration.(type) {
default:
d = 0
case string:
d, _ = time.ParseDuration(duration)
case int64:
d = time.Duration(duration)
case time.Time:
d = time.Since(duration)
}

u := uint64(d)
neg := d < 0
if neg {
u = -u
}

var (
year = uint64(time.Hour) * 24 * 365
month = uint64(time.Hour) * 24 * 30
day = uint64(time.Hour) * 24
hour = uint64(time.Hour)
minute = uint64(time.Minute)
second = uint64(time.Second)
)
switch {
case u > year:
return strconv.FormatUint(u/year, 10) + "y"
case u > month:
return strconv.FormatUint(u/month, 10) + "mo"
case u > day:
return strconv.FormatUint(u/day, 10) + "d"
case u > hour:
return strconv.FormatUint(u/hour, 10) + "h"
case u > minute:
return strconv.FormatUint(u/minute, 10) + "m"
case u > second:
return strconv.FormatUint(u/second, 10) + "s"
}
return "0s"
}

func toDate(fmt, str string) time.Time {
t, _ := time.ParseInLocation(fmt, str, time.Local)
return t
Expand Down
13 changes: 13 additions & 0 deletions date_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,16 @@ func TestDateInZone(t *testing.T) {
t.Error(err)
}
}

func TestDurationRound(t *testing.T) {
tpl := "{{ durationRound .Time }}"
if err := runtv(tpl, "2h", map[string]interface{}{"Time": "2h5s"}); err != nil {
t.Error(err)
}
if err := runtv(tpl, "1d", map[string]interface{}{"Time": "24h5s"}); err != nil {
t.Error(err)
}
if err := runtv(tpl, "3mo", map[string]interface{}{"Time": "2400h5s"}); err != nil {
t.Error(err)
}
}
11 changes: 6 additions & 5 deletions functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,15 +91,16 @@ var genericMap = map[string]interface{}{
"hello": func() string { return "Hello!" },

// Date functions
"ago": dateAgo,
"date": date,
"dateInZone": dateInZone,
"dateModify": dateModify,
"date_in_zone": dateInZone,
"date_modify": dateModify,
"now": func() time.Time { return time.Now() },
"durationRound": durationRound,
"htmlDate": htmlDate,
"htmlDateInZone": htmlDateInZone,
"dateInZone": dateInZone,
"dateModify": dateModify,
"ago": dateAgo,
"now": func() time.Time { return time.Now() },
"toDate": toDate,
"unixEpoch": unixEpoch,

Expand Down Expand Up @@ -295,5 +296,5 @@ var genericMap = map[string]interface{}{

// URLs:
"urlParse": urlParse,
"urlJoin": urlJoin,
"urlJoin": urlJoin,
}