From f94c1b3943ebc4249e6240855c5414f735e8b25f Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Thu, 14 Dec 2017 07:45:31 +0800 Subject: [PATCH] Improvements for supporting UI Location (#3146) * improvements for supporting UI Location * improved the comment --- cmd/serv.go | 5 +++-- models/issue_tracked_time.go | 3 ++- models/ssh_key.go | 22 ++++++++++++---------- models/user.go | 8 -------- modules/setting/setting.go | 3 +++ modules/util/time_stamp.go | 10 +++++++--- routers/api/v1/convert/convert.go | 2 +- 7 files changed, 28 insertions(+), 25 deletions(-) diff --git a/cmd/serv.go b/cmd/serv.go index 1ff296d007..d7fe6c6630 100644 --- a/cmd/serv.go +++ b/cmd/serv.go @@ -18,6 +18,7 @@ import ( "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/private" "code.gitea.io/gitea/modules/setting" + "code.gitea.io/gitea/modules/util" "github.com/Unknwon/com" "github.com/dgrijalva/jwt-go" @@ -219,8 +220,8 @@ func runServ(c *cli.Context) error { fail("Internal error", "GetDeployKey: %v", err) } - deployKey.Updated = time.Now() - if err = models.UpdateDeployKey(deployKey); err != nil { + deployKey.UpdatedUnix = util.TimeStampNow() + if err = models.UpdateDeployKeyCols(deployKey, "updated_unix"); err != nil { fail("Internal error", "UpdateDeployKey: %v", err) } } else { diff --git a/models/issue_tracked_time.go b/models/issue_tracked_time.go index 3b2360f68f..c314f8f44f 100644 --- a/models/issue_tracked_time.go +++ b/models/issue_tracked_time.go @@ -7,6 +7,7 @@ package models import ( "time" + "code.gitea.io/gitea/modules/setting" api "code.gitea.io/sdk/gitea" "github.com/go-xorm/builder" @@ -24,7 +25,7 @@ type TrackedTime struct { // AfterLoad is invoked from XORM after setting the values of all fields of this object. func (t *TrackedTime) AfterLoad() { - t.Created = time.Unix(t.CreatedUnix, 0).Local() + t.Created = time.Unix(t.CreatedUnix, 0).In(setting.UILocation) } // APIFormat converts TrackedTime to API format diff --git a/models/ssh_key.go b/models/ssh_key.go index ea313f147a..4d276ebeb7 100644 --- a/models/ssh_key.go +++ b/models/ssh_key.go @@ -600,20 +600,16 @@ type DeployKey struct { Fingerprint string Content string `xorm:"-"` - Created time.Time `xorm:"-"` - CreatedUnix int64 `xorm:"created"` - Updated time.Time `xorm:"-"` - UpdatedUnix int64 `xorm:"updated"` - HasRecentActivity bool `xorm:"-"` - HasUsed bool `xorm:"-"` + CreatedUnix util.TimeStamp `xorm:"created"` + UpdatedUnix util.TimeStamp `xorm:"updated"` + HasRecentActivity bool `xorm:"-"` + HasUsed bool `xorm:"-"` } // AfterLoad is invoked from XORM after setting the values of all fields of this object. func (key *DeployKey) AfterLoad() { - key.Created = time.Unix(key.CreatedUnix, 0).Local() - key.Updated = time.Unix(key.UpdatedUnix, 0).Local() - key.HasUsed = key.Updated.After(key.Created) - key.HasRecentActivity = key.Updated.Add(7 * 24 * time.Hour).After(time.Now()) + key.HasUsed = key.UpdatedUnix > key.CreatedUnix + key.HasRecentActivity = key.UpdatedUnix.AddDuration(7*24*time.Hour) > util.TimeStampNow() } // GetContent gets associated public key content. @@ -740,6 +736,12 @@ func GetDeployKeyByRepo(keyID, repoID int64) (*DeployKey, error) { return key, nil } +// UpdateDeployKeyCols updates deploy key information in the specified columns. +func UpdateDeployKeyCols(key *DeployKey, cols ...string) error { + _, err := x.ID(key.ID).Cols(cols...).Update(key) + return err +} + // UpdateDeployKey updates deploy key information. func UpdateDeployKey(key *DeployKey) error { _, err := x.ID(key.ID).AllCols().Update(key) diff --git a/models/user.go b/models/user.go index 0a2e2e9f92..fa5dc73deb 100644 --- a/models/user.go +++ b/models/user.go @@ -151,14 +151,6 @@ func (u *User) UpdateDiffViewStyle(style string) error { return UpdateUserCols(u, "diff_view_style") } -/* -// AfterLoad is invoked from XORM after setting the values of all fields of this object. -func (u *User) AfterLoad() { - u.Created = time.Unix(u.CreatedUnix, 0).Local() - u.Updated = time.Unix(u.UpdatedUnix, 0).Local() - u.LastLogin = time.Unix(u.LastLoginUnix, 0).Local() -}*/ - // getEmail returns an noreply email, if the user has set to keep his // email address private, otherwise the primary email address. func (u *User) getEmail() string { diff --git a/modules/setting/setting.go b/modules/setting/setting.go index 46ef20f61e..7d86ef3056 100644 --- a/modules/setting/setting.go +++ b/modules/setting/setting.go @@ -531,6 +531,9 @@ var ( IterateBufferSize int ExternalMarkupParsers []MarkupParser + // UILocation is the location on the UI, so that we can display the time on UI. + // Currently only show the default time.Local, it could be added to app.ini after UI is ready + UILocation = time.Local ) // DateLang transforms standard language locale name to corresponding value in datetime plugin. diff --git a/modules/util/time_stamp.go b/modules/util/time_stamp.go index 4a12a2cabe..a03560b24f 100644 --- a/modules/util/time_stamp.go +++ b/modules/util/time_stamp.go @@ -4,7 +4,11 @@ package util -import "time" +import ( + "time" + + "code.gitea.io/gitea/modules/setting" +) // TimeStamp defines a timestamp type TimeStamp int64 @@ -31,13 +35,13 @@ func (ts TimeStamp) Year() int { // AsTime convert timestamp as time.Time in Local locale func (ts TimeStamp) AsTime() (tm time.Time) { - tm = time.Unix(int64(ts), 0).Local() + tm = time.Unix(int64(ts), 0).In(setting.UILocation) return } // AsTimePtr convert timestamp as *time.Time in Local locale func (ts TimeStamp) AsTimePtr() *time.Time { - tm := time.Unix(int64(ts), 0).Local() + tm := time.Unix(int64(ts), 0).In(setting.UILocation) return &tm } diff --git a/routers/api/v1/convert/convert.go b/routers/api/v1/convert/convert.go index 1bfd7f1ff0..e24ac0518c 100644 --- a/routers/api/v1/convert/convert.go +++ b/routers/api/v1/convert/convert.go @@ -164,7 +164,7 @@ func ToDeployKey(apiLink string, key *models.DeployKey) *api.DeployKey { Key: key.Content, URL: apiLink + com.ToStr(key.ID), Title: key.Name, - Created: key.Created, + Created: key.CreatedUnix.AsTime(), ReadOnly: true, // All deploy keys are read-only. } }