Skip to content

Commit f94c1b3

Browse files
authored
Improvements for supporting UI Location (#3146)
* improvements for supporting UI Location * improved the comment
1 parent b6d2243 commit f94c1b3

File tree

7 files changed

+28
-25
lines changed

7 files changed

+28
-25
lines changed

cmd/serv.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"code.gitea.io/gitea/modules/log"
1919
"code.gitea.io/gitea/modules/private"
2020
"code.gitea.io/gitea/modules/setting"
21+
"code.gitea.io/gitea/modules/util"
2122

2223
"github.com/Unknwon/com"
2324
"github.com/dgrijalva/jwt-go"
@@ -219,8 +220,8 @@ func runServ(c *cli.Context) error {
219220
fail("Internal error", "GetDeployKey: %v", err)
220221
}
221222

222-
deployKey.Updated = time.Now()
223-
if err = models.UpdateDeployKey(deployKey); err != nil {
223+
deployKey.UpdatedUnix = util.TimeStampNow()
224+
if err = models.UpdateDeployKeyCols(deployKey, "updated_unix"); err != nil {
224225
fail("Internal error", "UpdateDeployKey: %v", err)
225226
}
226227
} else {

models/issue_tracked_time.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package models
77
import (
88
"time"
99

10+
"code.gitea.io/gitea/modules/setting"
1011
api "code.gitea.io/sdk/gitea"
1112

1213
"github.com/go-xorm/builder"
@@ -24,7 +25,7 @@ type TrackedTime struct {
2425

2526
// AfterLoad is invoked from XORM after setting the values of all fields of this object.
2627
func (t *TrackedTime) AfterLoad() {
27-
t.Created = time.Unix(t.CreatedUnix, 0).Local()
28+
t.Created = time.Unix(t.CreatedUnix, 0).In(setting.UILocation)
2829
}
2930

3031
// APIFormat converts TrackedTime to API format

models/ssh_key.go

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -600,20 +600,16 @@ type DeployKey struct {
600600
Fingerprint string
601601
Content string `xorm:"-"`
602602

603-
Created time.Time `xorm:"-"`
604-
CreatedUnix int64 `xorm:"created"`
605-
Updated time.Time `xorm:"-"`
606-
UpdatedUnix int64 `xorm:"updated"`
607-
HasRecentActivity bool `xorm:"-"`
608-
HasUsed bool `xorm:"-"`
603+
CreatedUnix util.TimeStamp `xorm:"created"`
604+
UpdatedUnix util.TimeStamp `xorm:"updated"`
605+
HasRecentActivity bool `xorm:"-"`
606+
HasUsed bool `xorm:"-"`
609607
}
610608

611609
// AfterLoad is invoked from XORM after setting the values of all fields of this object.
612610
func (key *DeployKey) AfterLoad() {
613-
key.Created = time.Unix(key.CreatedUnix, 0).Local()
614-
key.Updated = time.Unix(key.UpdatedUnix, 0).Local()
615-
key.HasUsed = key.Updated.After(key.Created)
616-
key.HasRecentActivity = key.Updated.Add(7 * 24 * time.Hour).After(time.Now())
611+
key.HasUsed = key.UpdatedUnix > key.CreatedUnix
612+
key.HasRecentActivity = key.UpdatedUnix.AddDuration(7*24*time.Hour) > util.TimeStampNow()
617613
}
618614

619615
// GetContent gets associated public key content.
@@ -740,6 +736,12 @@ func GetDeployKeyByRepo(keyID, repoID int64) (*DeployKey, error) {
740736
return key, nil
741737
}
742738

739+
// UpdateDeployKeyCols updates deploy key information in the specified columns.
740+
func UpdateDeployKeyCols(key *DeployKey, cols ...string) error {
741+
_, err := x.ID(key.ID).Cols(cols...).Update(key)
742+
return err
743+
}
744+
743745
// UpdateDeployKey updates deploy key information.
744746
func UpdateDeployKey(key *DeployKey) error {
745747
_, err := x.ID(key.ID).AllCols().Update(key)

models/user.go

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -151,14 +151,6 @@ func (u *User) UpdateDiffViewStyle(style string) error {
151151
return UpdateUserCols(u, "diff_view_style")
152152
}
153153

154-
/*
155-
// AfterLoad is invoked from XORM after setting the values of all fields of this object.
156-
func (u *User) AfterLoad() {
157-
u.Created = time.Unix(u.CreatedUnix, 0).Local()
158-
u.Updated = time.Unix(u.UpdatedUnix, 0).Local()
159-
u.LastLogin = time.Unix(u.LastLoginUnix, 0).Local()
160-
}*/
161-
162154
// getEmail returns an noreply email, if the user has set to keep his
163155
// email address private, otherwise the primary email address.
164156
func (u *User) getEmail() string {

modules/setting/setting.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -531,6 +531,9 @@ var (
531531
IterateBufferSize int
532532

533533
ExternalMarkupParsers []MarkupParser
534+
// UILocation is the location on the UI, so that we can display the time on UI.
535+
// Currently only show the default time.Local, it could be added to app.ini after UI is ready
536+
UILocation = time.Local
534537
)
535538

536539
// DateLang transforms standard language locale name to corresponding value in datetime plugin.

modules/util/time_stamp.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@
44

55
package util
66

7-
import "time"
7+
import (
8+
"time"
9+
10+
"code.gitea.io/gitea/modules/setting"
11+
)
812

913
// TimeStamp defines a timestamp
1014
type TimeStamp int64
@@ -31,13 +35,13 @@ func (ts TimeStamp) Year() int {
3135

3236
// AsTime convert timestamp as time.Time in Local locale
3337
func (ts TimeStamp) AsTime() (tm time.Time) {
34-
tm = time.Unix(int64(ts), 0).Local()
38+
tm = time.Unix(int64(ts), 0).In(setting.UILocation)
3539
return
3640
}
3741

3842
// AsTimePtr convert timestamp as *time.Time in Local locale
3943
func (ts TimeStamp) AsTimePtr() *time.Time {
40-
tm := time.Unix(int64(ts), 0).Local()
44+
tm := time.Unix(int64(ts), 0).In(setting.UILocation)
4145
return &tm
4246
}
4347

routers/api/v1/convert/convert.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ func ToDeployKey(apiLink string, key *models.DeployKey) *api.DeployKey {
164164
Key: key.Content,
165165
URL: apiLink + com.ToStr(key.ID),
166166
Title: key.Name,
167-
Created: key.Created,
167+
Created: key.CreatedUnix.AsTime(),
168168
ReadOnly: true, // All deploy keys are read-only.
169169
}
170170
}

0 commit comments

Comments
 (0)