mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2024-11-22 05:15:33 +03:00
Refactor timeutil package (#28623)
1. make names more readable 2. remove unused FormatLong/FormatShort 3. use `FormatDate` instead of `Format "2006-01-02"`
This commit is contained in:
parent
f3999888c0
commit
e743570f65
9 changed files with 30 additions and 39 deletions
|
@ -59,8 +59,8 @@ func TestGetUserHeatmapDataByUser(t *testing.T) {
|
||||||
assert.NoError(t, unittest.PrepareTestDatabase())
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
||||||
|
|
||||||
// Mock time
|
// Mock time
|
||||||
timeutil.Set(time.Date(2021, 1, 1, 0, 0, 0, 0, time.UTC))
|
timeutil.MockSet(time.Date(2021, 1, 1, 0, 0, 0, 0, time.UTC))
|
||||||
defer timeutil.Unset()
|
defer timeutil.MockUnset()
|
||||||
|
|
||||||
for _, tc := range testCases {
|
for _, tc := range testCases {
|
||||||
user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: tc.userID})
|
user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: tc.userID})
|
||||||
|
|
|
@ -107,8 +107,9 @@ func VerifyGPGKey(ctx context.Context, ownerID int64, keyID, token, signature st
|
||||||
// VerificationToken returns token for the user that will be valid in minutes (time)
|
// VerificationToken returns token for the user that will be valid in minutes (time)
|
||||||
func VerificationToken(user *user_model.User, minutes int) string {
|
func VerificationToken(user *user_model.User, minutes int) string {
|
||||||
return base.EncodeSha256(
|
return base.EncodeSha256(
|
||||||
time.Now().Truncate(1*time.Minute).Add(time.Duration(minutes)*time.Minute).Format(time.RFC1123Z) + ":" +
|
time.Now().Truncate(1*time.Minute).Add(time.Duration(minutes)*time.Minute).Format(
|
||||||
user.CreatedUnix.FormatLong() + ":" +
|
time.RFC1123Z) + ":" +
|
||||||
|
user.CreatedUnix.Format(time.RFC1123Z) + ":" +
|
||||||
user.Name + ":" +
|
user.Name + ":" +
|
||||||
user.Email + ":" +
|
user.Email + ":" +
|
||||||
strconv.FormatInt(user.ID, 10))
|
strconv.FormatInt(user.ID, 10))
|
||||||
|
|
|
@ -899,15 +899,15 @@ func createDeadlineComment(ctx context.Context, doer *user_model.User, issue *Is
|
||||||
// newDeadline = 0 means deleting
|
// newDeadline = 0 means deleting
|
||||||
if newDeadlineUnix == 0 {
|
if newDeadlineUnix == 0 {
|
||||||
commentType = CommentTypeRemovedDeadline
|
commentType = CommentTypeRemovedDeadline
|
||||||
content = issue.DeadlineUnix.Format("2006-01-02")
|
content = issue.DeadlineUnix.FormatDate()
|
||||||
} else if issue.DeadlineUnix == 0 {
|
} else if issue.DeadlineUnix == 0 {
|
||||||
// Check if the new date was added or modified
|
// Check if the new date was added or modified
|
||||||
// If the actual deadline is 0 => deadline added
|
// If the actual deadline is 0 => deadline added
|
||||||
commentType = CommentTypeAddedDeadline
|
commentType = CommentTypeAddedDeadline
|
||||||
content = newDeadlineUnix.Format("2006-01-02")
|
content = newDeadlineUnix.FormatDate()
|
||||||
} else { // Otherwise modified
|
} else { // Otherwise modified
|
||||||
commentType = CommentTypeModifiedDeadline
|
commentType = CommentTypeModifiedDeadline
|
||||||
content = newDeadlineUnix.Format("2006-01-02") + "|" + issue.DeadlineUnix.Format("2006-01-02")
|
content = newDeadlineUnix.FormatDate() + "|" + issue.DeadlineUnix.FormatDate()
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := issue.LoadRepo(ctx); err != nil {
|
if err := issue.LoadRepo(ctx); err != nil {
|
||||||
|
|
|
@ -86,7 +86,7 @@ func (m *Milestone) AfterLoad() {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
m.DeadlineString = m.DeadlineUnix.Format("2006-01-02")
|
m.DeadlineString = m.DeadlineUnix.FormatDate()
|
||||||
if m.IsClosed {
|
if m.IsClosed {
|
||||||
m.IsOverdue = m.ClosedDateUnix >= m.DeadlineUnix
|
m.IsOverdue = m.ClosedDateUnix >= m.DeadlineUnix
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -13,27 +13,27 @@ import (
|
||||||
type TimeStamp int64
|
type TimeStamp int64
|
||||||
|
|
||||||
var (
|
var (
|
||||||
// mock is NOT concurrency-safe!!
|
// mockNow is NOT concurrency-safe!!
|
||||||
mock time.Time
|
mockNow time.Time
|
||||||
|
|
||||||
// Used for IsZero, to check if timestamp is the zero time instant.
|
// Used for IsZero, to check if timestamp is the zero time instant.
|
||||||
timeZeroUnix = time.Time{}.Unix()
|
timeZeroUnix = time.Time{}.Unix()
|
||||||
)
|
)
|
||||||
|
|
||||||
// Set sets the time to a mocked time.Time
|
// MockSet sets the time to a mocked time.Time
|
||||||
func Set(now time.Time) {
|
func MockSet(now time.Time) {
|
||||||
mock = now
|
mockNow = now
|
||||||
}
|
}
|
||||||
|
|
||||||
// Unset will unset the mocked time.Time
|
// MockUnset will unset the mocked time.Time
|
||||||
func Unset() {
|
func MockUnset() {
|
||||||
mock = time.Time{}
|
mockNow = time.Time{}
|
||||||
}
|
}
|
||||||
|
|
||||||
// TimeStampNow returns now int64
|
// TimeStampNow returns now int64
|
||||||
func TimeStampNow() TimeStamp {
|
func TimeStampNow() TimeStamp {
|
||||||
if !mock.IsZero() {
|
if !mockNow.IsZero() {
|
||||||
return TimeStamp(mock.Unix())
|
return TimeStamp(mockNow.Unix())
|
||||||
}
|
}
|
||||||
return TimeStamp(time.Now().Unix())
|
return TimeStamp(time.Now().Unix())
|
||||||
}
|
}
|
||||||
|
@ -89,19 +89,9 @@ func (ts TimeStamp) FormatInLocation(f string, loc *time.Location) string {
|
||||||
return ts.AsTimeInLocation(loc).Format(f)
|
return ts.AsTimeInLocation(loc).Format(f)
|
||||||
}
|
}
|
||||||
|
|
||||||
// FormatLong formats as RFC1123Z
|
// FormatDate formats a date in YYYY-MM-DD
|
||||||
func (ts TimeStamp) FormatLong() string {
|
|
||||||
return ts.Format(time.RFC1123Z)
|
|
||||||
}
|
|
||||||
|
|
||||||
// FormatShort formats as short
|
|
||||||
func (ts TimeStamp) FormatShort() string {
|
|
||||||
return ts.Format("Jan 02, 2006")
|
|
||||||
}
|
|
||||||
|
|
||||||
// FormatDate formats a date in YYYY-MM-DD server time zone
|
|
||||||
func (ts TimeStamp) FormatDate() string {
|
func (ts TimeStamp) FormatDate() string {
|
||||||
return time.Unix(int64(ts), 0).String()[:10]
|
return ts.Format("2006-01-02")
|
||||||
}
|
}
|
||||||
|
|
||||||
// IsZero is zero time
|
// IsZero is zero time
|
||||||
|
|
|
@ -37,14 +37,14 @@ func TestCheckAuthToken(t *testing.T) {
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("Expired", func(t *testing.T) {
|
t.Run("Expired", func(t *testing.T) {
|
||||||
timeutil.Set(time.Date(2023, 1, 1, 0, 0, 0, 0, time.UTC))
|
timeutil.MockSet(time.Date(2023, 1, 1, 0, 0, 0, 0, time.UTC))
|
||||||
|
|
||||||
at, token, err := CreateAuthTokenForUserID(db.DefaultContext, 2)
|
at, token, err := CreateAuthTokenForUserID(db.DefaultContext, 2)
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
assert.NotNil(t, at)
|
assert.NotNil(t, at)
|
||||||
assert.NotEmpty(t, token)
|
assert.NotEmpty(t, token)
|
||||||
|
|
||||||
timeutil.Unset()
|
timeutil.MockUnset()
|
||||||
|
|
||||||
at2, err := CheckAuthToken(db.DefaultContext, at.ID+":"+token)
|
at2, err := CheckAuthToken(db.DefaultContext, at.ID+":"+token)
|
||||||
assert.ErrorIs(t, err, ErrAuthTokenExpired)
|
assert.ErrorIs(t, err, ErrAuthTokenExpired)
|
||||||
|
@ -83,15 +83,15 @@ func TestCheckAuthToken(t *testing.T) {
|
||||||
func TestRegenerateAuthToken(t *testing.T) {
|
func TestRegenerateAuthToken(t *testing.T) {
|
||||||
assert.NoError(t, unittest.PrepareTestDatabase())
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
||||||
|
|
||||||
timeutil.Set(time.Date(2023, 1, 1, 0, 0, 0, 0, time.UTC))
|
timeutil.MockSet(time.Date(2023, 1, 1, 0, 0, 0, 0, time.UTC))
|
||||||
defer timeutil.Unset()
|
defer timeutil.MockUnset()
|
||||||
|
|
||||||
at, token, err := CreateAuthTokenForUserID(db.DefaultContext, 2)
|
at, token, err := CreateAuthTokenForUserID(db.DefaultContext, 2)
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
assert.NotNil(t, at)
|
assert.NotNil(t, at)
|
||||||
assert.NotEmpty(t, token)
|
assert.NotEmpty(t, token)
|
||||||
|
|
||||||
timeutil.Set(time.Date(2023, 1, 1, 0, 0, 1, 0, time.UTC))
|
timeutil.MockSet(time.Date(2023, 1, 1, 0, 0, 1, 0, time.UTC))
|
||||||
|
|
||||||
at2, token2, err := RegenerateAuthToken(db.DefaultContext, at)
|
at2, token2, err := RegenerateAuthToken(db.DefaultContext, at)
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
|
|
|
@ -392,7 +392,7 @@
|
||||||
<div {{if ne .Issue.DeadlineUnix 0}} class="gt-hidden"{{end}} id="deadlineForm">
|
<div {{if ne .Issue.DeadlineUnix 0}} class="gt-hidden"{{end}} id="deadlineForm">
|
||||||
<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">
|
<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">
|
||||||
{{$.CsrfTokenHtml}}
|
{{$.CsrfTokenHtml}}
|
||||||
<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">
|
<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">
|
||||||
<button class="ui icon button">
|
<button class="ui icon button">
|
||||||
{{if ne .Issue.DeadlineUnix 0}}
|
{{if ne .Issue.DeadlineUnix 0}}
|
||||||
{{svg "octicon-pencil"}}
|
{{svg "octicon-pencil"}}
|
||||||
|
|
|
@ -114,7 +114,7 @@
|
||||||
<span class="due-date flex-text-inline" data-tooltip-content="{{ctx.Locale.Tr "repo.issues.due_date"}}">
|
<span class="due-date flex-text-inline" data-tooltip-content="{{ctx.Locale.Tr "repo.issues.due_date"}}">
|
||||||
<span{{if .IsOverdue}} class="text red"{{end}}>
|
<span{{if .IsOverdue}} class="text red"{{end}}>
|
||||||
{{svg "octicon-calendar" 14}}
|
{{svg "octicon-calendar" 14}}
|
||||||
{{DateTime "short" (.DeadlineUnix.Format "2006-01-02")}}
|
{{DateTime "short" (.DeadlineUnix.FormatDate)}}
|
||||||
</span>
|
</span>
|
||||||
</span>
|
</span>
|
||||||
{{end}}
|
{{end}}
|
||||||
|
|
|
@ -24,8 +24,8 @@ func TestUserHeatmap(t *testing.T) {
|
||||||
token := getUserToken(t, adminUsername, auth_model.AccessTokenScopeReadUser)
|
token := getUserToken(t, adminUsername, auth_model.AccessTokenScopeReadUser)
|
||||||
|
|
||||||
fakeNow := time.Date(2011, 10, 20, 0, 0, 0, 0, time.Local)
|
fakeNow := time.Date(2011, 10, 20, 0, 0, 0, 0, time.Local)
|
||||||
timeutil.Set(fakeNow)
|
timeutil.MockSet(fakeNow)
|
||||||
defer timeutil.Unset()
|
defer timeutil.MockUnset()
|
||||||
|
|
||||||
req := NewRequest(t, "GET", fmt.Sprintf("/api/v1/users/%s/heatmap", normalUsername)).
|
req := NewRequest(t, "GET", fmt.Sprintf("/api/v1/users/%s/heatmap", normalUsername)).
|
||||||
AddTokenAuth(token)
|
AddTokenAuth(token)
|
||||||
|
|
Loading…
Reference in a new issue