Skip to content

Commit fa65e25

Browse files
Sysoev, Vladimirvsysoev
Sysoev, Vladimir
authored andcommitted
Tracked time representation changed to hours
Tracked time in the issue represented not in the same manner as estimated. Because of estimated time viewed in hours it might be convinient to have tracked time represented in the same way. Signed-off-by: Sysoev, Vladimir <[email protected]>
1 parent fffc855 commit fa65e25

File tree

6 files changed

+19
-63
lines changed

6 files changed

+19
-63
lines changed

models/issues/stopwatch.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ func (s Stopwatch) Seconds() int64 {
4848

4949
// Duration returns a human-readable duration string based on local server time
5050
func (s Stopwatch) Duration() string {
51-
return util.SecToTime(s.Seconds())
51+
return util.SecToHours(s.Seconds())
5252
}
5353

5454
func getStopwatch(ctx context.Context, userID, issueID int64) (sw *Stopwatch, exists bool, err error) {
@@ -201,7 +201,7 @@ func FinishIssueStopwatch(ctx context.Context, user *user_model.User, issue *Iss
201201
Doer: user,
202202
Issue: issue,
203203
Repo: issue.Repo,
204-
Content: util.SecToTime(timediff),
204+
Content: util.SecToHours(timediff),
205205
Type: CommentTypeStopTracking,
206206
TimeID: tt.ID,
207207
}); err != nil {

modules/templates/helper.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ func NewFuncMap() template.FuncMap {
6969
// time / number / format
7070
"FileSize": base.FileSize,
7171
"CountFmt": countFmt,
72-
"Sec2Time": util.SecToTime,
72+
"Sec2Time": util.SecToHours,
7373

7474
"TimeEstimateString": timeEstimateString,
7575

modules/util/sec_to_time.go

Lines changed: 6 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -8,59 +8,20 @@ import (
88
"strings"
99
)
1010

11-
// SecToTime converts an amount of seconds to a human-readable string. E.g.
12-
// 66s -> 1 minute 6 seconds
13-
// 52410s -> 14 hours 33 minutes
14-
// 563418 -> 6 days 12 hours
15-
// 1563418 -> 2 weeks 4 days
16-
// 3937125s -> 1 month 2 weeks
17-
// 45677465s -> 1 year 6 months
18-
func SecToTime(durationVal any) string {
11+
// SecToHours converts an amount of seconds to a human-readable hours string.
12+
// This is sutable for planning and managing timesheets.
13+
func SecToHours(durationVal any) string {
1914
duration, _ := ToInt64(durationVal)
2015

2116
formattedTime := ""
2217

23-
// The following four variables are calculated by taking
24-
// into account the previously calculated variables, this avoids
25-
// pitfalls when using remainders. As that could lead to incorrect
26-
// results when the calculated number equals the quotient number.
27-
remainingDays := duration / (60 * 60 * 24)
28-
years := remainingDays / 365
29-
remainingDays -= years * 365
30-
months := remainingDays * 12 / 365
31-
remainingDays -= months * 365 / 12
32-
weeks := remainingDays / 7
33-
remainingDays -= weeks * 7
34-
days := remainingDays
35-
3618
// The following three variables are calculated without depending
3719
// on the previous calculated variables.
38-
hours := (duration / 3600) % 24
20+
hours := (duration / 3600)
3921
minutes := (duration / 60) % 60
40-
seconds := duration % 60
4122

42-
// Extract only the relevant information of the time
43-
// If the time is greater than a year, it makes no sense to display seconds.
44-
switch {
45-
case years > 0:
46-
formattedTime = formatTime(years, "year", formattedTime)
47-
formattedTime = formatTime(months, "month", formattedTime)
48-
case months > 0:
49-
formattedTime = formatTime(months, "month", formattedTime)
50-
formattedTime = formatTime(weeks, "week", formattedTime)
51-
case weeks > 0:
52-
formattedTime = formatTime(weeks, "week", formattedTime)
53-
formattedTime = formatTime(days, "day", formattedTime)
54-
case days > 0:
55-
formattedTime = formatTime(days, "day", formattedTime)
56-
formattedTime = formatTime(hours, "hour", formattedTime)
57-
case hours > 0:
58-
formattedTime = formatTime(hours, "hour", formattedTime)
59-
formattedTime = formatTime(minutes, "minute", formattedTime)
60-
default:
61-
formattedTime = formatTime(minutes, "minute", formattedTime)
62-
formattedTime = formatTime(seconds, "second", formattedTime)
63-
}
23+
formattedTime = formatTime(hours, "hour", formattedTime)
24+
formattedTime = formatTime(minutes, "minute", formattedTime)
6425

6526
// The formatTime() function always appends a space at the end. This will be trimmed
6627
return strings.TrimRight(formattedTime, " ")

modules/util/sec_to_time_test.go

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,17 @@ import (
99
"github.com/stretchr/testify/assert"
1010
)
1111

12-
func TestSecToTime(t *testing.T) {
12+
func TestSecToHours(t *testing.T) {
1313
second := int64(1)
1414
minute := 60 * second
1515
hour := 60 * minute
1616
day := 24 * hour
17-
year := 365 * day
1817

19-
assert.Equal(t, "1 minute 6 seconds", SecToTime(minute+6*second))
20-
assert.Equal(t, "1 hour", SecToTime(hour))
21-
assert.Equal(t, "1 hour", SecToTime(hour+second))
22-
assert.Equal(t, "14 hours 33 minutes", SecToTime(14*hour+33*minute+30*second))
23-
assert.Equal(t, "6 days 12 hours", SecToTime(6*day+12*hour+30*minute+18*second))
24-
assert.Equal(t, "2 weeks 4 days", SecToTime((2*7+4)*day+2*hour+16*minute+58*second))
25-
assert.Equal(t, "4 weeks", SecToTime(4*7*day))
26-
assert.Equal(t, "4 weeks 1 day", SecToTime((4*7+1)*day))
27-
assert.Equal(t, "1 month 2 weeks", SecToTime((6*7+3)*day+13*hour+38*minute+45*second))
28-
assert.Equal(t, "11 months", SecToTime(year-25*day))
29-
assert.Equal(t, "1 year 5 months", SecToTime(year+163*day+10*hour+11*minute+5*second))
18+
assert.Equal(t, "1 minute", SecToHours(minute+6*second))
19+
assert.Equal(t, "1 hour", SecToHours(hour))
20+
assert.Equal(t, "1 hour", SecToHours(hour+second))
21+
assert.Equal(t, "14 hours 33 minutes", SecToHours(14*hour+33*minute+30*second))
22+
assert.Equal(t, "156 hours 30 minutes", SecToHours(6*day+12*hour+30*minute+18*second))
23+
assert.Equal(t, "98 hours 16 minutes", SecToHours(4*day+2*hour+16*minute+58*second))
24+
assert.Equal(t, "672 hours", SecToHours(4*7*day))
3025
}

routers/web/repo/issue_timetrack.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ func DeleteTime(c *context.Context) {
8181
return
8282
}
8383

84-
c.Flash.Success(c.Tr("repo.issues.del_time_history", util.SecToTime(t.Time)))
84+
c.Flash.Success(c.Tr("repo.issues.del_time_history", util.SecToHours(t.Time)))
8585
c.JSONRedirect("")
8686
}
8787

services/convert/issue_comment.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ func ToTimelineComment(ctx context.Context, repo *repo_model.Repository, c *issu
7474
c.Content[0] == '|' {
7575
// TimeTracking Comments from v1.21 on store the seconds instead of an formatted string
7676
// so we check for the "|" delimiter and convert new to legacy format on demand
77-
c.Content = util.SecToTime(c.Content[1:])
77+
c.Content = util.SecToHours(c.Content[1:])
7878
}
7979

8080
if c.Type == issues_model.CommentTypeChangeTimeEstimate {

0 commit comments

Comments
 (0)