Skip to content

Commit b000ca2

Browse files
wxiaoguangpull[bot]
authored andcommitted
Refactor timeutil package (#28623)
1. make names more readable 2. remove unused FormatLong/FormatShort 3. use `FormatDate` instead of `Format "2006-01-02"`
1 parent fa05dd2 commit b000ca2

File tree

9 files changed

+30
-39
lines changed

9 files changed

+30
-39
lines changed

models/activities/user_heatmap_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@ func TestGetUserHeatmapDataByUser(t *testing.T) {
5959
assert.NoError(t, unittest.PrepareTestDatabase())
6060

6161
// Mock time
62-
timeutil.Set(time.Date(2021, 1, 1, 0, 0, 0, 0, time.UTC))
63-
defer timeutil.Unset()
62+
timeutil.MockSet(time.Date(2021, 1, 1, 0, 0, 0, 0, time.UTC))
63+
defer timeutil.MockUnset()
6464

6565
for _, tc := range testCases {
6666
user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: tc.userID})

models/asymkey/gpg_key_verify.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,9 @@ func VerifyGPGKey(ctx context.Context, ownerID int64, keyID, token, signature st
107107
// VerificationToken returns token for the user that will be valid in minutes (time)
108108
func VerificationToken(user *user_model.User, minutes int) string {
109109
return base.EncodeSha256(
110-
time.Now().Truncate(1*time.Minute).Add(time.Duration(minutes)*time.Minute).Format(time.RFC1123Z) + ":" +
111-
user.CreatedUnix.FormatLong() + ":" +
110+
time.Now().Truncate(1*time.Minute).Add(time.Duration(minutes)*time.Minute).Format(
111+
time.RFC1123Z) + ":" +
112+
user.CreatedUnix.Format(time.RFC1123Z) + ":" +
112113
user.Name + ":" +
113114
user.Email + ":" +
114115
strconv.FormatInt(user.ID, 10))

models/issues/comment.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -899,15 +899,15 @@ func createDeadlineComment(ctx context.Context, doer *user_model.User, issue *Is
899899
// newDeadline = 0 means deleting
900900
if newDeadlineUnix == 0 {
901901
commentType = CommentTypeRemovedDeadline
902-
content = issue.DeadlineUnix.Format("2006-01-02")
902+
content = issue.DeadlineUnix.FormatDate()
903903
} else if issue.DeadlineUnix == 0 {
904904
// Check if the new date was added or modified
905905
// If the actual deadline is 0 => deadline added
906906
commentType = CommentTypeAddedDeadline
907-
content = newDeadlineUnix.Format("2006-01-02")
907+
content = newDeadlineUnix.FormatDate()
908908
} else { // Otherwise modified
909909
commentType = CommentTypeModifiedDeadline
910-
content = newDeadlineUnix.Format("2006-01-02") + "|" + issue.DeadlineUnix.Format("2006-01-02")
910+
content = newDeadlineUnix.FormatDate() + "|" + issue.DeadlineUnix.FormatDate()
911911
}
912912

913913
if err := issue.LoadRepo(ctx); err != nil {

models/issues/milestone.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ func (m *Milestone) AfterLoad() {
8686
return
8787
}
8888

89-
m.DeadlineString = m.DeadlineUnix.Format("2006-01-02")
89+
m.DeadlineString = m.DeadlineUnix.FormatDate()
9090
if m.IsClosed {
9191
m.IsOverdue = m.ClosedDateUnix >= m.DeadlineUnix
9292
} else {

modules/timeutil/timestamp.go

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -13,27 +13,27 @@ import (
1313
type TimeStamp int64
1414

1515
var (
16-
// mock is NOT concurrency-safe!!
17-
mock time.Time
16+
// mockNow is NOT concurrency-safe!!
17+
mockNow time.Time
1818

1919
// Used for IsZero, to check if timestamp is the zero time instant.
2020
timeZeroUnix = time.Time{}.Unix()
2121
)
2222

23-
// Set sets the time to a mocked time.Time
24-
func Set(now time.Time) {
25-
mock = now
23+
// MockSet sets the time to a mocked time.Time
24+
func MockSet(now time.Time) {
25+
mockNow = now
2626
}
2727

28-
// Unset will unset the mocked time.Time
29-
func Unset() {
30-
mock = time.Time{}
28+
// MockUnset will unset the mocked time.Time
29+
func MockUnset() {
30+
mockNow = time.Time{}
3131
}
3232

3333
// TimeStampNow returns now int64
3434
func TimeStampNow() TimeStamp {
35-
if !mock.IsZero() {
36-
return TimeStamp(mock.Unix())
35+
if !mockNow.IsZero() {
36+
return TimeStamp(mockNow.Unix())
3737
}
3838
return TimeStamp(time.Now().Unix())
3939
}
@@ -89,19 +89,9 @@ func (ts TimeStamp) FormatInLocation(f string, loc *time.Location) string {
8989
return ts.AsTimeInLocation(loc).Format(f)
9090
}
9191

92-
// FormatLong formats as RFC1123Z
93-
func (ts TimeStamp) FormatLong() string {
94-
return ts.Format(time.RFC1123Z)
95-
}
96-
97-
// FormatShort formats as short
98-
func (ts TimeStamp) FormatShort() string {
99-
return ts.Format("Jan 02, 2006")
100-
}
101-
102-
// FormatDate formats a date in YYYY-MM-DD server time zone
92+
// FormatDate formats a date in YYYY-MM-DD
10393
func (ts TimeStamp) FormatDate() string {
104-
return time.Unix(int64(ts), 0).String()[:10]
94+
return ts.Format("2006-01-02")
10595
}
10696

10797
// IsZero is zero time

services/auth/auth_token_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,14 @@ func TestCheckAuthToken(t *testing.T) {
3737
})
3838

3939
t.Run("Expired", func(t *testing.T) {
40-
timeutil.Set(time.Date(2023, 1, 1, 0, 0, 0, 0, time.UTC))
40+
timeutil.MockSet(time.Date(2023, 1, 1, 0, 0, 0, 0, time.UTC))
4141

4242
at, token, err := CreateAuthTokenForUserID(db.DefaultContext, 2)
4343
assert.NoError(t, err)
4444
assert.NotNil(t, at)
4545
assert.NotEmpty(t, token)
4646

47-
timeutil.Unset()
47+
timeutil.MockUnset()
4848

4949
at2, err := CheckAuthToken(db.DefaultContext, at.ID+":"+token)
5050
assert.ErrorIs(t, err, ErrAuthTokenExpired)
@@ -83,15 +83,15 @@ func TestCheckAuthToken(t *testing.T) {
8383
func TestRegenerateAuthToken(t *testing.T) {
8484
assert.NoError(t, unittest.PrepareTestDatabase())
8585

86-
timeutil.Set(time.Date(2023, 1, 1, 0, 0, 0, 0, time.UTC))
87-
defer timeutil.Unset()
86+
timeutil.MockSet(time.Date(2023, 1, 1, 0, 0, 0, 0, time.UTC))
87+
defer timeutil.MockUnset()
8888

8989
at, token, err := CreateAuthTokenForUserID(db.DefaultContext, 2)
9090
assert.NoError(t, err)
9191
assert.NotNil(t, at)
9292
assert.NotEmpty(t, token)
9393

94-
timeutil.Set(time.Date(2023, 1, 1, 0, 0, 1, 0, time.UTC))
94+
timeutil.MockSet(time.Date(2023, 1, 1, 0, 0, 1, 0, time.UTC))
9595

9696
at2, token2, err := RegenerateAuthToken(db.DefaultContext, at)
9797
assert.NoError(t, err)

templates/repo/issue/view_content/sidebar.tmpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,7 @@
392392
<div {{if ne .Issue.DeadlineUnix 0}} class="gt-hidden"{{end}} id="deadlineForm">
393393
<form class="ui fluid action input issue-due-form" action="{{AppSubUrl}}/{{PathEscape .Repository.Owner.Name}}/{{PathEscape .Repository.Name}}/issues/{{.Issue.Index}}/deadline" method="post" id="update-issue-deadline-form">
394394
{{$.CsrfTokenHtml}}
395-
<input required placeholder="{{ctx.Locale.Tr "repo.issues.due_date_form"}}" {{if gt .Issue.DeadlineUnix 0}}value="{{.Issue.DeadlineUnix.Format "2006-01-02"}}"{{end}} type="date" name="deadlineDate" id="deadlineDate">
395+
<input required placeholder="{{ctx.Locale.Tr "repo.issues.due_date_form"}}" {{if gt .Issue.DeadlineUnix 0}}value="{{.Issue.DeadlineUnix.FormatDate}}"{{end}} type="date" name="deadlineDate" id="deadlineDate">
396396
<button class="ui icon button">
397397
{{if ne .Issue.DeadlineUnix 0}}
398398
{{svg "octicon-pencil"}}

templates/shared/issuelist.tmpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@
114114
<span class="due-date flex-text-inline" data-tooltip-content="{{ctx.Locale.Tr "repo.issues.due_date"}}">
115115
<span{{if .IsOverdue}} class="text red"{{end}}>
116116
{{svg "octicon-calendar" 14}}
117-
{{DateTime "short" (.DeadlineUnix.Format "2006-01-02")}}
117+
{{DateTime "short" (.DeadlineUnix.FormatDate)}}
118118
</span>
119119
</span>
120120
{{end}}

tests/integration/api_user_heatmap_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ func TestUserHeatmap(t *testing.T) {
2424
token := getUserToken(t, adminUsername, auth_model.AccessTokenScopeReadUser)
2525

2626
fakeNow := time.Date(2011, 10, 20, 0, 0, 0, 0, time.Local)
27-
timeutil.Set(fakeNow)
28-
defer timeutil.Unset()
27+
timeutil.MockSet(fakeNow)
28+
defer timeutil.MockUnset()
2929

3030
req := NewRequest(t, "GET", fmt.Sprintf("/api/v1/users/%s/heatmap", normalUsername)).
3131
AddTokenAuth(token)

0 commit comments

Comments
 (0)