summaryrefslogtreecommitdiffstats
path: root/modules/base/tool.go
diff options
context:
space:
mode:
authorLunny Xiao <xiaolunwen@gmail.com>2017-12-11 12:37:04 +0800
committerLauris BH <lauris@nix.lv>2017-12-11 06:37:04 +0200
commitf2e20c81b66e6a937ecdb686f8d1011371433365 (patch)
tree490e5af82aefdd25de5d90225b083ecb3ed11e5f /modules/base/tool.go
parentc082c3bce35d6d5d829a1e516b9bbf45b6d49bdc (diff)
downloadgitea-f2e20c81b66e6a937ecdb686f8d1011371433365.tar.gz
gitea-f2e20c81b66e6a937ecdb686f8d1011371433365.zip
Refactor struct's time to remove unnecessary memory usage (#3142)
* refactor struct's time to remove unnecessary memory usage * use AsTimePtr simple code * fix tests * fix time compare * fix template on gpg * use AddDuration instead of Add
Diffstat (limited to 'modules/base/tool.go')
-rw-r--r--modules/base/tool.go22
1 files changed, 19 insertions, 3 deletions
diff --git a/modules/base/tool.go b/modules/base/tool.go
index 1316b8fad3..347241e6bb 100644
--- a/modules/base/tool.go
+++ b/modules/base/tool.go
@@ -26,6 +26,7 @@ import (
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/setting"
+ "code.gitea.io/gitea/modules/util"
"github.com/Unknwon/com"
"github.com/Unknwon/i18n"
"github.com/gogits/chardet"
@@ -357,11 +358,15 @@ func timeSincePro(then, now time.Time, lang string) string {
}
func timeSince(then, now time.Time, lang string) string {
+ return timeSinceUnix(then.Unix(), now.Unix(), lang)
+}
+
+func timeSinceUnix(then, now int64, lang string) string {
lbl := "tool.ago"
- diff := now.Unix() - then.Unix()
- if then.After(now) {
+ diff := now - then
+ if then > now {
lbl = "tool.from_now"
- diff = then.Unix() - now.Unix()
+ diff = then - now
}
if diff <= 0 {
return i18n.Tr(lang, "tool.now")
@@ -387,6 +392,17 @@ func htmlTimeSince(then, now time.Time, lang string) template.HTML {
timeSince(then, now, lang)))
}
+// TimeSinceUnix calculates the time interval and generate user-friendly string.
+func TimeSinceUnix(then util.TimeStamp, lang string) template.HTML {
+ return htmlTimeSinceUnix(then, util.TimeStamp(time.Now().Unix()), lang)
+}
+
+func htmlTimeSinceUnix(then, now util.TimeStamp, lang string) template.HTML {
+ return template.HTML(fmt.Sprintf(`<span class="time-since" title="%s">%s</span>`,
+ then.Format(setting.TimeFormat),
+ timeSinceUnix(int64(then), int64(now), lang)))
+}
+
// Storage space size types
const (
Byte = 1