From dac9b058b97edef8055ecdab11d1ec27fee0e174 Mon Sep 17 00:00:00 2001 From: "Sysoev, Vladimir" Date: Fri, 1 Dec 2023 08:14:52 +0300 Subject: [PATCH] Replaced Sec2Time with Sec2Hours for better timesheets reporting. Current situation. Tracked time informations is displayed in years, monthes, days, hours, minutes. This is not suitable for timesheets reporting. For example work day is 8 hours, work week often has 5 work days. What is changed. Added function Sec2Hours which shows tracked time in hours and minutes which is more convenient while reporting timesheets to management. Signed-off-by: Sysoev, Vladimir --- models/issues/stopwatch.go | 4 ++-- modules/templates/helper.go | 2 +- modules/util/sec_to_time.go | 19 +++++++++++++++++++ modules/util/sec_to_time_test.go | 13 +++++++++++++ routers/web/repo/issue_timetrack.go | 2 +- services/convert/issue_comment.go | 2 +- templates/base/head_navbar.tmpl | 2 +- templates/repo/issue/filters.tmpl | 2 +- templates/repo/issue/list.tmpl | 2 +- templates/repo/issue/milestone_issues.tmpl | 2 +- templates/repo/issue/milestones.tmpl | 2 +- .../repo/issue/view_content/comments.tmpl | 6 +++--- .../repo/issue/view_content/sidebar.tmpl | 4 ++-- templates/shared/issuelist.tmpl | 2 +- templates/user/dashboard/milestones.tmpl | 2 +- 15 files changed, 49 insertions(+), 17 deletions(-) diff --git a/models/issues/stopwatch.go b/models/issues/stopwatch.go index 2c662bdb06a80..55734bdfd3a57 100644 --- a/models/issues/stopwatch.go +++ b/models/issues/stopwatch.go @@ -62,7 +62,7 @@ func (s Stopwatch) Seconds() int64 { // Duration returns a human-readable duration string based on local server time func (s Stopwatch) Duration() string { - return util.SecToTime(s.Seconds()) + return util.SecToHours(s.Seconds()) } func getStopwatch(ctx context.Context, userID, issueID int64) (sw *Stopwatch, exists bool, err error) { @@ -215,7 +215,7 @@ func FinishIssueStopwatch(ctx context.Context, user *user_model.User, issue *Iss Doer: user, Issue: issue, Repo: issue.Repo, - Content: util.SecToTime(timediff), + Content: util.SecToHours(timediff), Type: CommentTypeStopTracking, TimeID: tt.ID, }); err != nil { diff --git a/modules/templates/helper.go b/modules/templates/helper.go index 235fd96b73d8f..e6858d5e43f2a 100644 --- a/modules/templates/helper.go +++ b/modules/templates/helper.go @@ -67,7 +67,7 @@ func NewFuncMap() template.FuncMap { "TimeSince": timeutil.TimeSince, "TimeSinceUnix": timeutil.TimeSinceUnix, "DateTime": timeutil.DateTime, - "Sec2Time": util.SecToTime, + "Sec2Hours": util.SecToHours, "LoadTimes": func(startTime time.Time) string { return fmt.Sprint(time.Since(startTime).Nanoseconds()/1e6) + "ms" }, diff --git a/modules/util/sec_to_time.go b/modules/util/sec_to_time.go index ad0fb1a68b4a7..b88580dacc002 100644 --- a/modules/util/sec_to_time.go +++ b/modules/util/sec_to_time.go @@ -66,6 +66,25 @@ func SecToTime(durationVal any) string { return strings.TrimRight(formattedTime, " ") } +// SecToHours converts an amount of seconds to a human-readable hours string. +// This is sutable for planning and managing timesheets. +func SecToHours(durationVal any) string { + duration, _ := ToInt64(durationVal) + + formattedTime := "" + + // The following three variables are calculated without depending + // on the previous calculated variables. + hours := (duration / 3600) + minutes := (duration / 60) % 60 + + formattedTime = formatTime(hours, "hour", formattedTime) + formattedTime = formatTime(minutes, "minute", formattedTime) + + // The formatTime() function always appends a space at the end. This will be trimmed + return strings.TrimRight(formattedTime, " ") +} + // formatTime appends the given value to the existing forammattedTime. E.g: // formattedTime = "1 year" // input: value = 3, name = "month" diff --git a/modules/util/sec_to_time_test.go b/modules/util/sec_to_time_test.go index 4d1213a52c05e..cde8c4ca35b4a 100644 --- a/modules/util/sec_to_time_test.go +++ b/modules/util/sec_to_time_test.go @@ -28,3 +28,16 @@ func TestSecToTime(t *testing.T) { assert.Equal(t, "11 months", SecToTime(year-25*day)) assert.Equal(t, "1 year 5 months", SecToTime(year+163*day+10*hour+11*minute+5*second)) } + +func TestSecToHours(t *testing.T) { + second := int64(1) + minute := 60 * second + hour := 60 * minute + day := 24 * hour + + assert.Equal(t, "1 minute", SecToHours(minute+6*second)) + assert.Equal(t, "1 hour", SecToHours(hour)) + assert.Equal(t, "1 hour", SecToHours(hour+second)) + assert.Equal(t, "14 hours 33 minutes", SecToHours(14*hour+33*minute+30*second)) + assert.Equal(t, "156 hours 30 minutes", SecToHours(6*day+12*hour+30*minute+18*second)) +} diff --git a/routers/web/repo/issue_timetrack.go b/routers/web/repo/issue_timetrack.go index c9bf861b844b8..e288e1cc8f651 100644 --- a/routers/web/repo/issue_timetrack.go +++ b/routers/web/repo/issue_timetrack.go @@ -82,6 +82,6 @@ func DeleteTime(c *context.Context) { return } - c.Flash.Success(c.Tr("repo.issues.del_time_history", util.SecToTime(t.Time))) + c.Flash.Success(c.Tr("repo.issues.del_time_history", util.SecToHours(t.Time))) c.Redirect(issue.Link()) } diff --git a/services/convert/issue_comment.go b/services/convert/issue_comment.go index b034a50897190..cf3e4cfa036df 100644 --- a/services/convert/issue_comment.go +++ b/services/convert/issue_comment.go @@ -74,7 +74,7 @@ func ToTimelineComment(ctx context.Context, repo *repo_model.Repository, c *issu c.Content[0] == '|' { // TimeTracking Comments from v1.21 on store the seconds instead of an formated string // so we check for the "|" delimeter and convert new to legacy format on demand - c.Content = util.SecToTime(c.Content[1:]) + c.Content = util.SecToHours(c.Content[1:]) } } diff --git a/templates/base/head_navbar.tmpl b/templates/base/head_navbar.tmpl index effe4dcea9f8f..51df01c0ab9bf 100644 --- a/templates/base/head_navbar.tmpl +++ b/templates/base/head_navbar.tmpl @@ -88,7 +88,7 @@ {{svg "octicon-issue-opened" 16 "gt-mr-3"}} {{.ActiveStopwatch.RepoSlug}}#{{.ActiveStopwatch.IssueIndex}} - {{if .ActiveStopwatch}}{{Sec2Time .ActiveStopwatch.Seconds}}{{end}} + {{if .ActiveStopwatch}}{{Sec2Hours .ActiveStopwatch.Seconds}}{{end}}
diff --git a/templates/repo/issue/filters.tmpl b/templates/repo/issue/filters.tmpl index 1d200e23b7a7f..9cac7c0478615 100644 --- a/templates/repo/issue/filters.tmpl +++ b/templates/repo/issue/filters.tmpl @@ -9,7 +9,7 @@ {{end}} diff --git a/templates/repo/issue/list.tmpl b/templates/repo/issue/list.tmpl index 012b613fbf879..8476a642b127c 100644 --- a/templates/repo/issue/list.tmpl +++ b/templates/repo/issue/list.tmpl @@ -39,7 +39,7 @@ {{end}} diff --git a/templates/repo/issue/milestone_issues.tmpl b/templates/repo/issue/milestone_issues.tmpl index ea19518efac83..b79adaf77b6b9 100644 --- a/templates/repo/issue/milestone_issues.tmpl +++ b/templates/repo/issue/milestone_issues.tmpl @@ -49,7 +49,7 @@ {{if .TotalTrackedTime}}
{{svg "octicon-clock"}} - {{.TotalTrackedTime | Sec2Time}} + {{.TotalTrackedTime | Sec2Hours}}
{{end}} diff --git a/templates/repo/issue/milestones.tmpl b/templates/repo/issue/milestones.tmpl index 3d4bbfd8b1ada..4d93d3c1dc390 100644 --- a/templates/repo/issue/milestones.tmpl +++ b/templates/repo/issue/milestones.tmpl @@ -41,7 +41,7 @@ {{if .TotalTrackedTime}}
{{svg "octicon-clock"}} - {{.TotalTrackedTime|Sec2Time}} + {{.TotalTrackedTime|Sec2Hours}}
{{end}} {{if .UpdatedUnix}} diff --git a/templates/repo/issue/view_content/comments.tmpl b/templates/repo/issue/view_content/comments.tmpl index 817f20af203ec..f6103b6d9d629 100644 --- a/templates/repo/issue/view_content/comments.tmpl +++ b/templates/repo/issue/view_content/comments.tmpl @@ -252,7 +252,7 @@ {{/* compatibility with time comments made before v1.21 */}} {{.RenderedContent}} {{else}} - {{.Content|Sec2Time}} + {{.Content|Sec2Hours}} {{end}} @@ -271,7 +271,7 @@ {{/* compatibility with time comments made before v1.21 */}} {{.RenderedContent}} {{else}} - {{.Content|Sec2Time}} + {{.Content|Sec2Hours}} {{end}} @@ -647,7 +647,7 @@ {{/* compatibility with time comments made before v1.21 */}} {{.RenderedContent}} {{else}} - - {{.Content|Sec2Time}} + - {{.Content|Sec2Hours}} {{end}} diff --git a/templates/repo/issue/view_content/sidebar.tmpl b/templates/repo/issue/view_content/sidebar.tmpl index 4be1f52dd5c69..5afcba8fc8d51 100644 --- a/templates/repo/issue/view_content/sidebar.tmpl +++ b/templates/repo/issue/view_content/sidebar.tmpl @@ -342,7 +342,7 @@ {{if gt (len .WorkingUsers) 0}}
- {{ctx.Locale.Tr "repo.issues.time_spent_from_all_authors" ($.Issue.TotalTrackedTime | Sec2Time) | Safe}} + {{ctx.Locale.Tr "repo.issues.time_spent_from_all_authors" ($.Issue.TotalTrackedTime | Sec2Hours) | Safe}}
{{range $user, $trackedtime := .WorkingUsers}}
@@ -352,7 +352,7 @@
{{template "shared/user/authorlink" $user}}
- {{$trackedtime|Sec2Time}} + {{$trackedtime|Sec2Hours}}
diff --git a/templates/shared/issuelist.tmpl b/templates/shared/issuelist.tmpl index e0d2e102e5ef7..3bbd6f055afc2 100644 --- a/templates/shared/issuelist.tmpl +++ b/templates/shared/issuelist.tmpl @@ -30,7 +30,7 @@ {{if .TotalTrackedTime}}
{{svg "octicon-clock" 16}} - {{.TotalTrackedTime | Sec2Time}} + {{.TotalTrackedTime | Sec2Hours}}
{{end}} {{if .Assignees}} diff --git a/templates/user/dashboard/milestones.tmpl b/templates/user/dashboard/milestones.tmpl index 390457a60a3fe..8bb633424228d 100644 --- a/templates/user/dashboard/milestones.tmpl +++ b/templates/user/dashboard/milestones.tmpl @@ -100,7 +100,7 @@ {{if .TotalTrackedTime}}
{{svg "octicon-clock"}} - {{.TotalTrackedTime|Sec2Time}} + {{.TotalTrackedTime|Sec2Hours}}
{{end}} {{if .UpdatedUnix}}