loading up the forgejo repo on tangled to test page performance
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Refactor timeutil package (#28623)

1. make names more readable
2. remove unused FormatLong/FormatShort
3. use `FormatDate` instead of `Format "2006-01-02"`

authored by

wxiaoguang and committed by
GitHub
e743570f f3999888

+30 -39
+2 -2
models/activities/user_heatmap_test.go
··· 59 59 assert.NoError(t, unittest.PrepareTestDatabase()) 60 60 61 61 // 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() 64 64 65 65 for _, tc := range testCases { 66 66 user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: tc.userID})
+3 -2
models/asymkey/gpg_key_verify.go
··· 107 107 // VerificationToken returns token for the user that will be valid in minutes (time) 108 108 func VerificationToken(user *user_model.User, minutes int) string { 109 109 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) + ":" + 112 113 user.Name + ":" + 113 114 user.Email + ":" + 114 115 strconv.FormatInt(user.ID, 10))
+3 -3
models/issues/comment.go
··· 899 899 // newDeadline = 0 means deleting 900 900 if newDeadlineUnix == 0 { 901 901 commentType = CommentTypeRemovedDeadline 902 - content = issue.DeadlineUnix.Format("2006-01-02") 902 + content = issue.DeadlineUnix.FormatDate() 903 903 } else if issue.DeadlineUnix == 0 { 904 904 // Check if the new date was added or modified 905 905 // If the actual deadline is 0 => deadline added 906 906 commentType = CommentTypeAddedDeadline 907 - content = newDeadlineUnix.Format("2006-01-02") 907 + content = newDeadlineUnix.FormatDate() 908 908 } else { // Otherwise modified 909 909 commentType = CommentTypeModifiedDeadline 910 - content = newDeadlineUnix.Format("2006-01-02") + "|" + issue.DeadlineUnix.Format("2006-01-02") 910 + content = newDeadlineUnix.FormatDate() + "|" + issue.DeadlineUnix.FormatDate() 911 911 } 912 912 913 913 if err := issue.LoadRepo(ctx); err != nil {
+1 -1
models/issues/milestone.go
··· 86 86 return 87 87 } 88 88 89 - m.DeadlineString = m.DeadlineUnix.Format("2006-01-02") 89 + m.DeadlineString = m.DeadlineUnix.FormatDate() 90 90 if m.IsClosed { 91 91 m.IsOverdue = m.ClosedDateUnix >= m.DeadlineUnix 92 92 } else {
+12 -22
modules/timeutil/timestamp.go
··· 13 13 type TimeStamp int64 14 14 15 15 var ( 16 - // mock is NOT concurrency-safe!! 17 - mock time.Time 16 + // mockNow is NOT concurrency-safe!! 17 + mockNow time.Time 18 18 19 19 // Used for IsZero, to check if timestamp is the zero time instant. 20 20 timeZeroUnix = time.Time{}.Unix() 21 21 ) 22 22 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 26 26 } 27 27 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{} 31 31 } 32 32 33 33 // TimeStampNow returns now int64 34 34 func TimeStampNow() TimeStamp { 35 - if !mock.IsZero() { 36 - return TimeStamp(mock.Unix()) 35 + if !mockNow.IsZero() { 36 + return TimeStamp(mockNow.Unix()) 37 37 } 38 38 return TimeStamp(time.Now().Unix()) 39 39 } ··· 89 89 return ts.AsTimeInLocation(loc).Format(f) 90 90 } 91 91 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 103 93 func (ts TimeStamp) FormatDate() string { 104 - return time.Unix(int64(ts), 0).String()[:10] 94 + return ts.Format("2006-01-02") 105 95 } 106 96 107 97 // IsZero is zero time
+5 -5
services/auth/auth_token_test.go
··· 37 37 }) 38 38 39 39 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)) 41 41 42 42 at, token, err := CreateAuthTokenForUserID(db.DefaultContext, 2) 43 43 assert.NoError(t, err) 44 44 assert.NotNil(t, at) 45 45 assert.NotEmpty(t, token) 46 46 47 - timeutil.Unset() 47 + timeutil.MockUnset() 48 48 49 49 at2, err := CheckAuthToken(db.DefaultContext, at.ID+":"+token) 50 50 assert.ErrorIs(t, err, ErrAuthTokenExpired) ··· 83 83 func TestRegenerateAuthToken(t *testing.T) { 84 84 assert.NoError(t, unittest.PrepareTestDatabase()) 85 85 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() 88 88 89 89 at, token, err := CreateAuthTokenForUserID(db.DefaultContext, 2) 90 90 assert.NoError(t, err) 91 91 assert.NotNil(t, at) 92 92 assert.NotEmpty(t, token) 93 93 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)) 95 95 96 96 at2, token2, err := RegenerateAuthToken(db.DefaultContext, at) 97 97 assert.NoError(t, err)
+1 -1
templates/repo/issue/view_content/sidebar.tmpl
··· 392 392 <div {{if ne .Issue.DeadlineUnix 0}} class="gt-hidden"{{end}} id="deadlineForm"> 393 393 <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"> 394 394 {{$.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"> 396 396 <button class="ui icon button"> 397 397 {{if ne .Issue.DeadlineUnix 0}} 398 398 {{svg "octicon-pencil"}}
+1 -1
templates/shared/issuelist.tmpl
··· 114 114 <span class="due-date flex-text-inline" data-tooltip-content="{{ctx.Locale.Tr "repo.issues.due_date"}}"> 115 115 <span{{if .IsOverdue}} class="text red"{{end}}> 116 116 {{svg "octicon-calendar" 14}} 117 - {{DateTime "short" (.DeadlineUnix.Format "2006-01-02")}} 117 + {{DateTime "short" (.DeadlineUnix.FormatDate)}} 118 118 </span> 119 119 </span> 120 120 {{end}}
+2 -2
tests/integration/api_user_heatmap_test.go
··· 24 24 token := getUserToken(t, adminUsername, auth_model.AccessTokenScopeReadUser) 25 25 26 26 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() 29 29 30 30 req := NewRequest(t, "GET", fmt.Sprintf("/api/v1/users/%s/heatmap", normalUsername)). 31 31 AddTokenAuth(token)