From f2e20c81b66e6a937ecdb686f8d1011371433365 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Mon, 11 Dec 2017 12:37:04 +0800 Subject: 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 --- modules/base/tool.go | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) (limited to 'modules/base') 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(`%s`, + then.Format(setting.TimeFormat), + timeSinceUnix(int64(then), int64(now), lang))) +} + // Storage space size types const ( Byte = 1 -- cgit v1.2.3