From db7c4a793078ae8f94f7ac87d52ea1d0a9a9e1bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Schaar?= Date: Sun, 13 Feb 2022 18:25:42 +0100 Subject: [PATCH 1/8] Fix display time of milestones --- models/issue_stopwatch.go | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/models/issue_stopwatch.go b/models/issue_stopwatch.go index 530a524218763..6bfe4c800ac5c 100644 --- a/models/issue_stopwatch.go +++ b/models/issue_stopwatch.go @@ -268,22 +268,32 @@ func cancelStopwatch(ctx context.Context, user *user_model.User, issue *Issue) e func SecToTime(duration int64) string { seconds := duration % 60 minutes := (duration / (60)) % 60 - hours := duration / (60 * 60) + hours := duration / (60 * 60) % 24 + days := duration / (60 * 60) / 24 var hrs string + if days > 1 { + hrs = fmt.Sprintf("%d days", days) + } else { + hrs = fmt.Sprintf("%d day", days) + } if hours > 0 { - hrs = fmt.Sprintf("%dh", hours) + if days == 0 { + hrs = fmt.Sprintf("%dh", hours) + } else { + hrs = fmt.Sprintf("%s %dh", hrs, hours) + } } if minutes > 0 { - if hours == 0 { + if days == 0 && hours == 0 { hrs = fmt.Sprintf("%dmin", minutes) } else { hrs = fmt.Sprintf("%s %dmin", hrs, minutes) } } if seconds > 0 { - if hours == 0 && minutes == 0 { + if days == 0 && hours == 0 && minutes == 0 { hrs = fmt.Sprintf("%ds", seconds) } else { hrs = fmt.Sprintf("%s %ds", hrs, seconds) From 0130bed2483f6bcc43590ce19b7060a9952665b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Schaar?= Date: Mon, 14 Feb 2022 22:00:12 +0100 Subject: [PATCH 2/8] Move the SecToTime function From the models/issue_stopwatch.go file to the modules/util package --- models/issue_stopwatch.go | 44 ++--------------------------- models/issue_tracked_time.go | 9 +++--- modules/templates/helper.go | 4 +-- modules/util/sectotime.go | 44 +++++++++++++++++++++++++++++ modules/util/sectotime_test.go | 20 +++++++++++++ routers/web/repo/issue_timetrack.go | 3 +- 6 files changed, 76 insertions(+), 48 deletions(-) create mode 100644 modules/util/sectotime.go create mode 100644 modules/util/sectotime_test.go diff --git a/models/issue_stopwatch.go b/models/issue_stopwatch.go index 6bfe4c800ac5c..3be9ad4e3f415 100644 --- a/models/issue_stopwatch.go +++ b/models/issue_stopwatch.go @@ -12,6 +12,7 @@ import ( "code.gitea.io/gitea/models/db" user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/timeutil" + "code.gitea.io/gitea/modules/util" ) // ErrIssueStopwatchNotExist represents an error that stopwatch is not exist @@ -53,7 +54,7 @@ func (s Stopwatch) Seconds() int64 { // Duration returns a human-readable duration string based on local server time func (s Stopwatch) Duration() string { - return SecToTime(s.Seconds()) + return util.SecToTime(s.Seconds()) } 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 Doer: user, Issue: issue, Repo: issue.Repo, - Content: SecToTime(timediff), + Content: util.SecToTime(timediff), Type: CommentTypeStopTracking, TimeID: tt.ID, }); err != nil { @@ -263,42 +264,3 @@ func cancelStopwatch(ctx context.Context, user *user_model.User, issue *Issue) e } return nil } - -// SecToTime converts an amount of seconds to a human-readable string (example: 66s -> 1min 6s) -func SecToTime(duration int64) string { - seconds := duration % 60 - minutes := (duration / (60)) % 60 - hours := duration / (60 * 60) % 24 - days := duration / (60 * 60) / 24 - - var hrs string - - if days > 1 { - hrs = fmt.Sprintf("%d days", days) - } else { - hrs = fmt.Sprintf("%d day", days) - } - if hours > 0 { - if days == 0 { - hrs = fmt.Sprintf("%dh", hours) - } else { - hrs = fmt.Sprintf("%s %dh", hrs, hours) - } - } - if minutes > 0 { - if days == 0 && hours == 0 { - hrs = fmt.Sprintf("%dmin", minutes) - } else { - hrs = fmt.Sprintf("%s %dmin", hrs, minutes) - } - } - if seconds > 0 { - if days == 0 && hours == 0 && minutes == 0 { - hrs = fmt.Sprintf("%ds", seconds) - } else { - hrs = fmt.Sprintf("%s %ds", hrs, seconds) - } - } - - return hrs -} diff --git a/models/issue_tracked_time.go b/models/issue_tracked_time.go index c887baae15c29..2d7bef19e1a81 100644 --- a/models/issue_tracked_time.go +++ b/models/issue_tracked_time.go @@ -11,6 +11,7 @@ import ( "code.gitea.io/gitea/models/db" user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/setting" + "code.gitea.io/gitea/modules/util" "xorm.io/builder" ) @@ -177,7 +178,7 @@ func AddTime(user *user_model.User, issue *Issue, amount int64, created time.Tim Issue: issue, Repo: issue.Repo, Doer: user, - Content: SecToTime(amount), + Content: util.SecToTime(amount), Type: CommentTypeAddTimeManual, TimeID: t.ID, }); err != nil { @@ -226,7 +227,7 @@ func TotalTimes(options *FindTrackedTimesOptions) (map[*user_model.User]string, } return nil, err } - totalTimes[user] = SecToTime(total) + totalTimes[user] = util.SecToTime(total) } return totalTimes, nil } @@ -260,7 +261,7 @@ func DeleteIssueUserTimes(issue *Issue, user *user_model.User) error { Issue: issue, Repo: issue.Repo, Doer: user, - Content: "- " + SecToTime(removedTime), + Content: "- " + util.SecToTime(removedTime), Type: CommentTypeDeleteTimeManual, }); err != nil { return err @@ -289,7 +290,7 @@ func DeleteTime(t *TrackedTime) error { Issue: t.Issue, Repo: t.Issue.Repo, Doer: t.User, - Content: "- " + SecToTime(t.Time), + Content: "- " + util.SecToTime(t.Time), Type: CommentTypeDeleteTimeManual, }); err != nil { return err diff --git a/modules/templates/helper.go b/modules/templates/helper.go index 255866e2ed1c6..63c165bc8bd24 100644 --- a/modules/templates/helper.go +++ b/modules/templates/helper.go @@ -256,7 +256,7 @@ func NewFuncMap() []template.FuncMap { }, "Printf": fmt.Sprintf, "Escape": Escape, - "Sec2Time": models.SecToTime, + "Sec2Time": util.SecToTime, "ParseDeadline": func(deadline string) []string { return strings.Split(deadline, "|") }, @@ -447,7 +447,7 @@ func NewTextFuncMap() []texttmpl.FuncMap { }, "Printf": fmt.Sprintf, "Escape": Escape, - "Sec2Time": models.SecToTime, + "Sec2Time": util.SecToTime, "ParseDeadline": func(deadline string) []string { return strings.Split(deadline, "|") }, diff --git a/modules/util/sectotime.go b/modules/util/sectotime.go new file mode 100644 index 0000000000000..335465f59628e --- /dev/null +++ b/modules/util/sectotime.go @@ -0,0 +1,44 @@ +package util + +import( + "fmt" +) + +// SecToTime converts an amount of seconds to a human-readable string (example: 66s -> 1min 6s) +func SecToTime(duration int64) string { + seconds := duration % 60 + minutes := (duration / (60)) % 60 + hours := duration / (60 * 60) % 24 + days := duration / (60 * 60) / 24 + + var hrs string + + if days > 1 { + hrs = fmt.Sprintf("%d days", days) + } else { + hrs = fmt.Sprintf("%d day", days) + } + if hours > 0 { + if days == 0 { + hrs = fmt.Sprintf("%dh", hours) + } else { + hrs = fmt.Sprintf("%s %dh", hrs, hours) + } + } + if minutes > 0 { + if days == 0 && hours == 0 { + hrs = fmt.Sprintf("%dmin", minutes) + } else { + hrs = fmt.Sprintf("%s %dmin", hrs, minutes) + } + } + if seconds > 0 { + if days == 0 && hours == 0 && minutes == 0 { + hrs = fmt.Sprintf("%ds", seconds) + } else { + hrs = fmt.Sprintf("%s %ds", hrs, seconds) + } + } + + return hrs +} diff --git a/modules/util/sectotime_test.go b/modules/util/sectotime_test.go new file mode 100644 index 0000000000000..a21af3b6ae0df --- /dev/null +++ b/modules/util/sectotime_test.go @@ -0,0 +1,20 @@ +// Copyright 2021 Gitea. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package util + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestSecToTime(t *testing.T) { + assert.Equal(t, SecToTime(10), "10s") + assert.Equal(t, SecToTime(100), "1min 40s") + assert.Equal(t, SecToTime(1000), "16min 40s") + assert.Equal(t, SecToTime(10000), "2h 46min 40s") + assert.Equal(t, SecToTime(100000), "1 day 3h 46min 40s") + assert.Equal(t, SecToTime(1000000), "11 days 13h 46min 40s") +} diff --git a/routers/web/repo/issue_timetrack.go b/routers/web/repo/issue_timetrack.go index 3770cd7b4edd1..ec6bb6142dfe6 100644 --- a/routers/web/repo/issue_timetrack.go +++ b/routers/web/repo/issue_timetrack.go @@ -10,6 +10,7 @@ import ( "code.gitea.io/gitea/models" "code.gitea.io/gitea/modules/context" + "code.gitea.io/gitea/modules/util" "code.gitea.io/gitea/modules/web" "code.gitea.io/gitea/services/forms" ) @@ -81,6 +82,6 @@ func DeleteTime(c *context.Context) { return } - c.Flash.Success(c.Tr("repo.issues.del_time_history", models.SecToTime(t.Time))) + c.Flash.Success(c.Tr("repo.issues.del_time_history", util.SecToTime(t.Time))) c.Redirect(issue.HTMLURL()) } From fecd9596fd8c25c1f40772152e792561ad61cbb9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Schaar?= Date: Mon, 14 Feb 2022 22:02:25 +0100 Subject: [PATCH 3/8] Rename the sec_to_time file --- modules/util/{sectotime.go => sec_to_time.go} | 0 modules/util/{sectotime_test.go => sec_to_time_test.go} | 0 2 files changed, 0 insertions(+), 0 deletions(-) rename modules/util/{sectotime.go => sec_to_time.go} (100%) rename modules/util/{sectotime_test.go => sec_to_time_test.go} (100%) diff --git a/modules/util/sectotime.go b/modules/util/sec_to_time.go similarity index 100% rename from modules/util/sectotime.go rename to modules/util/sec_to_time.go diff --git a/modules/util/sectotime_test.go b/modules/util/sec_to_time_test.go similarity index 100% rename from modules/util/sectotime_test.go rename to modules/util/sec_to_time_test.go From 268f28ddb69781f178f4d2296656f827b13ef271 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Schaar?= Date: Mon, 14 Feb 2022 22:07:12 +0100 Subject: [PATCH 4/8] Updated formatting --- modules/util/sec_to_time.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/util/sec_to_time.go b/modules/util/sec_to_time.go index 335465f59628e..96f56659fa6da 100644 --- a/modules/util/sec_to_time.go +++ b/modules/util/sec_to_time.go @@ -1,6 +1,6 @@ package util -import( +import ( "fmt" ) From 449f5cc240287cb501600362da2a6b5f1e7dc4c2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Schaar?= Date: Mon, 14 Feb 2022 22:11:54 +0100 Subject: [PATCH 5/8] Include copyright notice in sec_to_time.go --- modules/util/sec_to_time.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/modules/util/sec_to_time.go b/modules/util/sec_to_time.go index 96f56659fa6da..c20d41b015ba2 100644 --- a/modules/util/sec_to_time.go +++ b/modules/util/sec_to_time.go @@ -1,3 +1,7 @@ +// Copyright 2021 Gitea. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + package util import ( From 5090a37fcc29d9ae90de1d8793abf66617f90687 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Schaar?= Date: Mon, 14 Feb 2022 23:54:07 +0100 Subject: [PATCH 6/8] Apply PR review suggestions - Update copyright notice dates to 2022 - Change `1 day 3h 5min 7s` to `1d 3h 5m 7s` --- modules/util/sec_to_time.go | 16 ++++++---------- modules/util/sec_to_time_test.go | 12 ++++++------ 2 files changed, 12 insertions(+), 16 deletions(-) diff --git a/modules/util/sec_to_time.go b/modules/util/sec_to_time.go index c20d41b015ba2..5643822762c21 100644 --- a/modules/util/sec_to_time.go +++ b/modules/util/sec_to_time.go @@ -1,12 +1,10 @@ -// Copyright 2021 Gitea. All rights reserved. +// Copyright 2022 Gitea. All rights reserved. // Use of this source code is governed by a MIT-style // license that can be found in the LICENSE file. package util -import ( - "fmt" -) +import "fmt" // SecToTime converts an amount of seconds to a human-readable string (example: 66s -> 1min 6s) func SecToTime(duration int64) string { @@ -17,10 +15,8 @@ func SecToTime(duration int64) string { var hrs string - if days > 1 { - hrs = fmt.Sprintf("%d days", days) - } else { - hrs = fmt.Sprintf("%d day", days) + if days > 0 { + hrs = fmt.Sprintf("%dd", days) } if hours > 0 { if days == 0 { @@ -31,9 +27,9 @@ func SecToTime(duration int64) string { } if minutes > 0 { if days == 0 && hours == 0 { - hrs = fmt.Sprintf("%dmin", minutes) + hrs = fmt.Sprintf("%dm", minutes) } else { - hrs = fmt.Sprintf("%s %dmin", hrs, minutes) + hrs = fmt.Sprintf("%s %dm", hrs, minutes) } } if seconds > 0 { diff --git a/modules/util/sec_to_time_test.go b/modules/util/sec_to_time_test.go index a21af3b6ae0df..915dcbf727904 100644 --- a/modules/util/sec_to_time_test.go +++ b/modules/util/sec_to_time_test.go @@ -1,4 +1,4 @@ -// Copyright 2021 Gitea. All rights reserved. +// Copyright 2022 Gitea. All rights reserved. // Use of this source code is governed by a MIT-style // license that can be found in the LICENSE file. @@ -12,9 +12,9 @@ import ( func TestSecToTime(t *testing.T) { assert.Equal(t, SecToTime(10), "10s") - assert.Equal(t, SecToTime(100), "1min 40s") - assert.Equal(t, SecToTime(1000), "16min 40s") - assert.Equal(t, SecToTime(10000), "2h 46min 40s") - assert.Equal(t, SecToTime(100000), "1 day 3h 46min 40s") - assert.Equal(t, SecToTime(1000000), "11 days 13h 46min 40s") + assert.Equal(t, SecToTime(100), "1m 40s") + assert.Equal(t, SecToTime(1000), "16m 40s") + assert.Equal(t, SecToTime(10000), "2h 46m 40s") + assert.Equal(t, SecToTime(100000), "1d 3h 46m 40s") + assert.Equal(t, SecToTime(1000000), "11d 13h 46m 40s") } From f0cf5c3b0de26a1486efc440b1706483a178e715 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Schaar?= Date: Tue, 15 Feb 2022 12:43:57 +0100 Subject: [PATCH 7/8] Rename hrs var and combine conditions --- modules/util/sec_to_time.go | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/modules/util/sec_to_time.go b/modules/util/sec_to_time.go index 5643822762c21..657b30cddffc1 100644 --- a/modules/util/sec_to_time.go +++ b/modules/util/sec_to_time.go @@ -13,32 +13,32 @@ func SecToTime(duration int64) string { hours := duration / (60 * 60) % 24 days := duration / (60 * 60) / 24 - var hrs string + var formattedTime string if days > 0 { - hrs = fmt.Sprintf("%dd", days) + formattedTime = fmt.Sprintf("%dd", days) } if hours > 0 { - if days == 0 { - hrs = fmt.Sprintf("%dh", hours) + if formattedTime == "" { + formattedTime = fmt.Sprintf("%dh", hours) } else { - hrs = fmt.Sprintf("%s %dh", hrs, hours) + formattedTime = fmt.Sprintf("%s %dh", formattedTime, hours) } } if minutes > 0 { - if days == 0 && hours == 0 { - hrs = fmt.Sprintf("%dm", minutes) + if formattedTime == "" { + formattedTime = fmt.Sprintf("%dm", minutes) } else { - hrs = fmt.Sprintf("%s %dm", hrs, minutes) + formattedTime = fmt.Sprintf("%s %dm", formattedTime, minutes) } } if seconds > 0 { - if days == 0 && hours == 0 && minutes == 0 { - hrs = fmt.Sprintf("%ds", seconds) + if formattedTime == "" { + formattedTime = fmt.Sprintf("%ds", seconds) } else { - hrs = fmt.Sprintf("%s %ds", hrs, seconds) + formattedTime = fmt.Sprintf("%s %ds", formattedTime, seconds) } } - return hrs + return formattedTime } From 67efab53cfbd0bd46dd4ab7fc7c8d460b8a375f3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Schaar?= Date: Tue, 15 Feb 2022 13:33:09 +0100 Subject: [PATCH 8/8] Update unit tests to match new time pattern Changed `1min` to `1m` --- models/issue_tracked_time_test.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/models/issue_tracked_time_test.go b/models/issue_tracked_time_test.go index 97efd8bb729cf..e6c9caf90013b 100644 --- a/models/issue_tracked_time_test.go +++ b/models/issue_tracked_time_test.go @@ -34,7 +34,7 @@ func TestAddTime(t *testing.T) { assert.Equal(t, int64(3661), tt.Time) comment := unittest.AssertExistsAndLoadBean(t, &Comment{Type: CommentTypeAddTimeManual, PosterID: 3, IssueID: 1}).(*Comment) - assert.Equal(t, comment.Content, "1h 1min 1s") + assert.Equal(t, comment.Content, "1h 1m 1s") } func TestGetTrackedTimes(t *testing.T) { @@ -86,7 +86,7 @@ func TestTotalTimes(t *testing.T) { assert.Len(t, total, 1) for user, time := range total { assert.Equal(t, int64(1), user.ID) - assert.Equal(t, "6min 40s", time) + assert.Equal(t, "6m 40s", time) } total, err = TotalTimes(&FindTrackedTimesOptions{IssueID: 2}) @@ -94,7 +94,7 @@ func TestTotalTimes(t *testing.T) { assert.Len(t, total, 2) for user, time := range total { if user.ID == 2 { - assert.Equal(t, "1h 1min 2s", time) + assert.Equal(t, "1h 1m 2s", time) } else if user.ID == 1 { assert.Equal(t, "20s", time) } else {