diff options
author | Lunny Xiao <xiaolunwen@gmail.com> | 2017-12-11 12:37:04 +0800 |
---|---|---|
committer | Lauris BH <lauris@nix.lv> | 2017-12-11 06:37:04 +0200 |
commit | f2e20c81b66e6a937ecdb686f8d1011371433365 (patch) | |
tree | 490e5af82aefdd25de5d90225b083ecb3ed11e5f /modules | |
parent | c082c3bce35d6d5d829a1e516b9bbf45b6d49bdc (diff) | |
download | gitea-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')
-rw-r--r-- | modules/auth/auth.go | 4 | ||||
-rw-r--r-- | modules/base/tool.go | 22 | ||||
-rw-r--r-- | modules/templates/helper.go | 17 | ||||
-rw-r--r-- | modules/util/time_stamp.go | 57 |
4 files changed, 87 insertions, 13 deletions
diff --git a/modules/auth/auth.go b/modules/auth/auth.go index 89b3e38509..f3aac51899 100644 --- a/modules/auth/auth.go +++ b/modules/auth/auth.go @@ -7,7 +7,6 @@ package auth import ( "reflect" "strings" - "time" "github.com/Unknwon/com" "github.com/go-macaron/binding" @@ -19,6 +18,7 @@ import ( "code.gitea.io/gitea/modules/base" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/setting" + "code.gitea.io/gitea/modules/util" "code.gitea.io/gitea/modules/validation" ) @@ -59,7 +59,7 @@ func SignedInID(ctx *macaron.Context, sess session.Store) int64 { } return 0 } - t.Updated = time.Now() + t.UpdatedUnix = util.TimeStampNow() if err = models.UpdateAccessToken(t); err != nil { log.Error(4, "UpdateAccessToken: %v", err) } 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 diff --git a/modules/templates/helper.go b/modules/templates/helper.go index c8b872d9f5..d6be25cebb 100644 --- a/modules/templates/helper.go +++ b/modules/templates/helper.go @@ -65,14 +65,15 @@ func NewFuncMap() []template.FuncMap { "LoadTimes": func(startTime time.Time) string { return fmt.Sprint(time.Since(startTime).Nanoseconds()/1e6) + "ms" }, - "AvatarLink": base.AvatarLink, - "Safe": Safe, - "SafeJS": SafeJS, - "Str2html": Str2html, - "TimeSince": base.TimeSince, - "RawTimeSince": base.RawTimeSince, - "FileSize": base.FileSize, - "Subtract": base.Subtract, + "AvatarLink": base.AvatarLink, + "Safe": Safe, + "SafeJS": SafeJS, + "Str2html": Str2html, + "TimeSince": base.TimeSince, + "TimeSinceUnix": base.TimeSinceUnix, + "RawTimeSince": base.RawTimeSince, + "FileSize": base.FileSize, + "Subtract": base.Subtract, "Add": func(a, b int) int { return a + b }, diff --git a/modules/util/time_stamp.go b/modules/util/time_stamp.go new file mode 100644 index 0000000000..4a12a2cabe --- /dev/null +++ b/modules/util/time_stamp.go @@ -0,0 +1,57 @@ +// Copyright 2017 The Gitea Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package util + +import "time" + +// TimeStamp defines a timestamp +type TimeStamp int64 + +// TimeStampNow returns now int64 +func TimeStampNow() TimeStamp { + return TimeStamp(time.Now().Unix()) +} + +// Add adds seconds and return sum +func (ts TimeStamp) Add(seconds int64) TimeStamp { + return ts + TimeStamp(seconds) +} + +// AddDuration adds time.Duration and return sum +func (ts TimeStamp) AddDuration(interval time.Duration) TimeStamp { + return ts + TimeStamp(interval/time.Second) +} + +// Year returns the time's year +func (ts TimeStamp) Year() int { + return ts.AsTime().Year() +} + +// AsTime convert timestamp as time.Time in Local locale +func (ts TimeStamp) AsTime() (tm time.Time) { + tm = time.Unix(int64(ts), 0).Local() + return +} + +// AsTimePtr convert timestamp as *time.Time in Local locale +func (ts TimeStamp) AsTimePtr() *time.Time { + tm := time.Unix(int64(ts), 0).Local() + return &tm +} + +// Format formats timestamp as +func (ts TimeStamp) Format(f string) string { + return ts.AsTime().Format(f) +} + +// FormatLong formats as RFC1123Z +func (ts TimeStamp) FormatLong() string { + return ts.Format(time.RFC1123Z) +} + +// FormatShort formats as short +func (ts TimeStamp) FormatShort() string { + return ts.Format("Jan 02, 2006") +} |