Skip to content

Commit

Permalink
Improvements for supporting UI Location (#3146)
Browse files Browse the repository at this point in the history
* improvements for supporting UI Location

* improved the comment
  • Loading branch information
lunny authored Dec 13, 2017
1 parent b6d2243 commit f94c1b3
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 25 deletions.
5 changes: 3 additions & 2 deletions cmd/serv.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 {
Expand Down
3 changes: 2 additions & 1 deletion models/issue_tracked_time.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand Down
22 changes: 12 additions & 10 deletions models/ssh_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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)
Expand Down
8 changes: 0 additions & 8 deletions models/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
3 changes: 3 additions & 0 deletions modules/setting/setting.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
10 changes: 7 additions & 3 deletions modules/util/time_stamp.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@

package util

import "time"
import (
"time"

"code.gitea.io/gitea/modules/setting"
)

// TimeStamp defines a timestamp
type TimeStamp int64
Expand All @@ -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
}

Expand Down
2 changes: 1 addition & 1 deletion routers/api/v1/convert/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
}
}
Expand Down

0 comments on commit f94c1b3

Please sign in to comment.