aboutsummaryrefslogtreecommitdiffhomepage
path: root/template/functions.go
diff options
context:
space:
mode:
authorGravatar Frédéric Guillot <fred@miniflux.net>2018-09-21 18:53:29 -0700
committerGravatar Frédéric Guillot <fred@miniflux.net>2018-09-21 22:23:23 -0700
commitbeb7a0cfcb6a5a76d680707a4c458eee51d87c07 (patch)
treef6e23ffbffcdd58f8438e2d2711acb4fa86bb83b /template/functions.go
parentf244df62936eea307a5fc3f27fb9968527d402ac (diff)
Use unique translation IDs instead of English text as key
Diffstat (limited to 'template/functions.go')
-rw-r--r--template/functions.go46
1 files changed, 45 insertions, 1 deletions
diff --git a/template/functions.go b/template/functions.go
index 9bf1423..aac6c75 100644
--- a/template/functions.go
+++ b/template/functions.go
@@ -6,6 +6,7 @@ package template // import "miniflux.app/template"
import (
"fmt"
+ "math"
"html/template"
"net/mail"
"strings"
@@ -15,7 +16,9 @@ import (
"miniflux.app/config"
"miniflux.app/filter"
"miniflux.app/http/route"
+ "miniflux.app/locale"
"miniflux.app/model"
+ "miniflux.app/timezone"
"miniflux.app/url"
)
@@ -130,4 +133,45 @@ func isEmail(str string) bool {
return false
}
return true
-} \ No newline at end of file
+}
+
+func elapsedTime(language *locale.Language, tz string, t time.Time) string {
+ if t.IsZero() {
+ return language.Get("time_elapsed.not_yet")
+ }
+
+ now := timezone.Now(tz)
+ t = timezone.Convert(tz, t)
+ if now.Before(t) {
+ return language.Get("time_elapsed.not_yet")
+ }
+
+ diff := now.Sub(t)
+ // Duration in seconds
+ s := diff.Seconds()
+ // Duration in days
+ d := int(s / 86400)
+ switch {
+ case s < 60:
+ return language.Get("time_elapsed.now")
+ case s < 3600:
+ minutes := int(diff.Minutes())
+ return language.Plural("time_elapsed.minutes", minutes, minutes)
+ case s < 86400:
+ hours := int(diff.Hours())
+ return language.Plural("time_elapsed.hours", hours, hours)
+ case d == 1:
+ return language.Get("time_elapsed.yesterday")
+ case d < 7:
+ return language.Plural("time_elapsed.days", d, d)
+ case d < 31:
+ weeks := int(math.Ceil(float64(d) / 7))
+ return language.Plural("time_elapsed.weeks", weeks, weeks)
+ case d < 365:
+ months := int(math.Ceil(float64(d) / 30))
+ return language.Plural("time_elapsed.months", months, months)
+ default:
+ years := int(math.Ceil(float64(d) / 365))
+ return language.Plural("time_elapsed.years", years, years)
+ }
+}