Skip to content

Commit 609c916

Browse files
schorsch13lunny
andauthored
Fix display time of milestones (#18753)
* Fix display time of milestones * Move the SecToTime function From the models/issue_stopwatch.go file to the modules/util package * Rename the sec_to_time file * Updated formatting * Include copyright notice in sec_to_time.go * Apply PR review suggestions - Update copyright notice dates to 2022 - Change `1 day 3h 5min 7s` to `1d 3h 5m 7s` * Rename hrs var and combine conditions * Update unit tests to match new time pattern Changed `1min` to `1m` Co-authored-by: Lunny Xiao <[email protected]>
1 parent 2be49de commit 609c916

File tree

7 files changed

+79
-41
lines changed

7 files changed

+79
-41
lines changed

models/issue_stopwatch.go

Lines changed: 3 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"code.gitea.io/gitea/models/db"
1313
user_model "code.gitea.io/gitea/models/user"
1414
"code.gitea.io/gitea/modules/timeutil"
15+
"code.gitea.io/gitea/modules/util"
1516
)
1617

1718
// ErrIssueStopwatchNotExist represents an error that stopwatch is not exist
@@ -53,7 +54,7 @@ func (s Stopwatch) Seconds() int64 {
5354

5455
// Duration returns a human-readable duration string based on local server time
5556
func (s Stopwatch) Duration() string {
56-
return SecToTime(s.Seconds())
57+
return util.SecToTime(s.Seconds())
5758
}
5859

5960
func getStopwatch(ctx context.Context, userID, issueID int64) (sw *Stopwatch, exists bool, err error) {
@@ -164,7 +165,7 @@ func FinishIssueStopwatch(ctx context.Context, user *user_model.User, issue *Iss
164165
Doer: user,
165166
Issue: issue,
166167
Repo: issue.Repo,
167-
Content: SecToTime(timediff),
168+
Content: util.SecToTime(timediff),
168169
Type: CommentTypeStopTracking,
169170
TimeID: tt.ID,
170171
}); err != nil {
@@ -263,32 +264,3 @@ func cancelStopwatch(ctx context.Context, user *user_model.User, issue *Issue) e
263264
}
264265
return nil
265266
}
266-
267-
// SecToTime converts an amount of seconds to a human-readable string (example: 66s -> 1min 6s)
268-
func SecToTime(duration int64) string {
269-
seconds := duration % 60
270-
minutes := (duration / (60)) % 60
271-
hours := duration / (60 * 60)
272-
273-
var hrs string
274-
275-
if hours > 0 {
276-
hrs = fmt.Sprintf("%dh", hours)
277-
}
278-
if minutes > 0 {
279-
if hours == 0 {
280-
hrs = fmt.Sprintf("%dmin", minutes)
281-
} else {
282-
hrs = fmt.Sprintf("%s %dmin", hrs, minutes)
283-
}
284-
}
285-
if seconds > 0 {
286-
if hours == 0 && minutes == 0 {
287-
hrs = fmt.Sprintf("%ds", seconds)
288-
} else {
289-
hrs = fmt.Sprintf("%s %ds", hrs, seconds)
290-
}
291-
}
292-
293-
return hrs
294-
}

models/issue_tracked_time.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"code.gitea.io/gitea/models/db"
1212
user_model "code.gitea.io/gitea/models/user"
1313
"code.gitea.io/gitea/modules/setting"
14+
"code.gitea.io/gitea/modules/util"
1415

1516
"xorm.io/builder"
1617
)
@@ -177,7 +178,7 @@ func AddTime(user *user_model.User, issue *Issue, amount int64, created time.Tim
177178
Issue: issue,
178179
Repo: issue.Repo,
179180
Doer: user,
180-
Content: SecToTime(amount),
181+
Content: util.SecToTime(amount),
181182
Type: CommentTypeAddTimeManual,
182183
TimeID: t.ID,
183184
}); err != nil {
@@ -226,7 +227,7 @@ func TotalTimes(options *FindTrackedTimesOptions) (map[*user_model.User]string,
226227
}
227228
return nil, err
228229
}
229-
totalTimes[user] = SecToTime(total)
230+
totalTimes[user] = util.SecToTime(total)
230231
}
231232
return totalTimes, nil
232233
}
@@ -260,7 +261,7 @@ func DeleteIssueUserTimes(issue *Issue, user *user_model.User) error {
260261
Issue: issue,
261262
Repo: issue.Repo,
262263
Doer: user,
263-
Content: "- " + SecToTime(removedTime),
264+
Content: "- " + util.SecToTime(removedTime),
264265
Type: CommentTypeDeleteTimeManual,
265266
}); err != nil {
266267
return err
@@ -289,7 +290,7 @@ func DeleteTime(t *TrackedTime) error {
289290
Issue: t.Issue,
290291
Repo: t.Issue.Repo,
291292
Doer: t.User,
292-
Content: "- " + SecToTime(t.Time),
293+
Content: "- " + util.SecToTime(t.Time),
293294
Type: CommentTypeDeleteTimeManual,
294295
}); err != nil {
295296
return err

models/issue_tracked_time_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ func TestAddTime(t *testing.T) {
3434
assert.Equal(t, int64(3661), tt.Time)
3535

3636
comment := unittest.AssertExistsAndLoadBean(t, &Comment{Type: CommentTypeAddTimeManual, PosterID: 3, IssueID: 1}).(*Comment)
37-
assert.Equal(t, comment.Content, "1h 1min 1s")
37+
assert.Equal(t, comment.Content, "1h 1m 1s")
3838
}
3939

4040
func TestGetTrackedTimes(t *testing.T) {
@@ -86,15 +86,15 @@ func TestTotalTimes(t *testing.T) {
8686
assert.Len(t, total, 1)
8787
for user, time := range total {
8888
assert.Equal(t, int64(1), user.ID)
89-
assert.Equal(t, "6min 40s", time)
89+
assert.Equal(t, "6m 40s", time)
9090
}
9191

9292
total, err = TotalTimes(&FindTrackedTimesOptions{IssueID: 2})
9393
assert.NoError(t, err)
9494
assert.Len(t, total, 2)
9595
for user, time := range total {
9696
if user.ID == 2 {
97-
assert.Equal(t, "1h 1min 2s", time)
97+
assert.Equal(t, "1h 1m 2s", time)
9898
} else if user.ID == 1 {
9999
assert.Equal(t, "20s", time)
100100
} else {

modules/templates/helper.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ func NewFuncMap() []template.FuncMap {
256256
},
257257
"Printf": fmt.Sprintf,
258258
"Escape": Escape,
259-
"Sec2Time": models.SecToTime,
259+
"Sec2Time": util.SecToTime,
260260
"ParseDeadline": func(deadline string) []string {
261261
return strings.Split(deadline, "|")
262262
},
@@ -447,7 +447,7 @@ func NewTextFuncMap() []texttmpl.FuncMap {
447447
},
448448
"Printf": fmt.Sprintf,
449449
"Escape": Escape,
450-
"Sec2Time": models.SecToTime,
450+
"Sec2Time": util.SecToTime,
451451
"ParseDeadline": func(deadline string) []string {
452452
return strings.Split(deadline, "|")
453453
},

modules/util/sec_to_time.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// Copyright 2022 Gitea. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE file.
4+
5+
package util
6+
7+
import "fmt"
8+
9+
// SecToTime converts an amount of seconds to a human-readable string (example: 66s -> 1min 6s)
10+
func SecToTime(duration int64) string {
11+
seconds := duration % 60
12+
minutes := (duration / (60)) % 60
13+
hours := duration / (60 * 60) % 24
14+
days := duration / (60 * 60) / 24
15+
16+
var formattedTime string
17+
18+
if days > 0 {
19+
formattedTime = fmt.Sprintf("%dd", days)
20+
}
21+
if hours > 0 {
22+
if formattedTime == "" {
23+
formattedTime = fmt.Sprintf("%dh", hours)
24+
} else {
25+
formattedTime = fmt.Sprintf("%s %dh", formattedTime, hours)
26+
}
27+
}
28+
if minutes > 0 {
29+
if formattedTime == "" {
30+
formattedTime = fmt.Sprintf("%dm", minutes)
31+
} else {
32+
formattedTime = fmt.Sprintf("%s %dm", formattedTime, minutes)
33+
}
34+
}
35+
if seconds > 0 {
36+
if formattedTime == "" {
37+
formattedTime = fmt.Sprintf("%ds", seconds)
38+
} else {
39+
formattedTime = fmt.Sprintf("%s %ds", formattedTime, seconds)
40+
}
41+
}
42+
43+
return formattedTime
44+
}

modules/util/sec_to_time_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Copyright 2022 Gitea. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE file.
4+
5+
package util
6+
7+
import (
8+
"testing"
9+
10+
"github.com/stretchr/testify/assert"
11+
)
12+
13+
func TestSecToTime(t *testing.T) {
14+
assert.Equal(t, SecToTime(10), "10s")
15+
assert.Equal(t, SecToTime(100), "1m 40s")
16+
assert.Equal(t, SecToTime(1000), "16m 40s")
17+
assert.Equal(t, SecToTime(10000), "2h 46m 40s")
18+
assert.Equal(t, SecToTime(100000), "1d 3h 46m 40s")
19+
assert.Equal(t, SecToTime(1000000), "11d 13h 46m 40s")
20+
}

routers/web/repo/issue_timetrack.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010

1111
"code.gitea.io/gitea/models"
1212
"code.gitea.io/gitea/modules/context"
13+
"code.gitea.io/gitea/modules/util"
1314
"code.gitea.io/gitea/modules/web"
1415
"code.gitea.io/gitea/services/forms"
1516
)
@@ -81,6 +82,6 @@ func DeleteTime(c *context.Context) {
8182
return
8283
}
8384

84-
c.Flash.Success(c.Tr("repo.issues.del_time_history", models.SecToTime(t.Time)))
85+
c.Flash.Success(c.Tr("repo.issues.del_time_history", util.SecToTime(t.Time)))
8586
c.Redirect(issue.HTMLURL())
8687
}

0 commit comments

Comments
 (0)