From 01c212c2f76fb39dab8fbe697de6afa8d4e1d5b0 Mon Sep 17 00:00:00 2001 From: Young Jin Park Date: Tue, 24 Sep 2019 12:15:38 +0900 Subject: [PATCH 1/2] Add durationRound function --- date.go | 44 ++++++++++++++++++++++++++++++++++++++++++++ date_test.go | 13 +++++++++++++ functions.go | 11 ++++++----- 3 files changed, 63 insertions(+), 5 deletions(-) diff --git a/date.go b/date.go index d1d6155d..fc2565e2 100644 --- a/date.go +++ b/date.go @@ -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) + "m" + 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 diff --git a/date_test.go b/date_test.go index a81ecf96..e41f3a56 100644 --- a/date_test.go +++ b/date_test.go @@ -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, "3m", map[string]interface{}{"Time": "2400h5s"}); err != nil { + t.Error(err) + } +} diff --git a/functions.go b/functions.go index 45e9d5ae..0ae724eb 100644 --- a/functions.go +++ b/functions.go @@ -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, @@ -295,5 +296,5 @@ var genericMap = map[string]interface{}{ // URLs: "urlParse": urlParse, - "urlJoin": urlJoin, + "urlJoin": urlJoin, } From f7af3a549b68975b7288a657b8a7cf1b1f5fd48e Mon Sep 17 00:00:00 2001 From: Young Jin Park Date: Sat, 28 Sep 2019 09:35:07 +0900 Subject: [PATCH 2/2] Change month abbreviation to mo from m --- date.go | 2 +- date_test.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/date.go b/date.go index fc2565e2..1cbdea52 100644 --- a/date.go +++ b/date.go @@ -104,7 +104,7 @@ func durationRound(duration interface{}) string { case u > year: return strconv.FormatUint(u/year, 10) + "y" case u > month: - return strconv.FormatUint(u/month, 10) + "m" + return strconv.FormatUint(u/month, 10) + "mo" case u > day: return strconv.FormatUint(u/day, 10) + "d" case u > hour: diff --git a/date_test.go b/date_test.go index e41f3a56..78bf1c29 100644 --- a/date_test.go +++ b/date_test.go @@ -100,7 +100,7 @@ func TestDurationRound(t *testing.T) { if err := runtv(tpl, "1d", map[string]interface{}{"Time": "24h5s"}); err != nil { t.Error(err) } - if err := runtv(tpl, "3m", map[string]interface{}{"Time": "2400h5s"}); err != nil { + if err := runtv(tpl, "3mo", map[string]interface{}{"Time": "2400h5s"}); err != nil { t.Error(err) } }