Skip to content

Commit eef6355

Browse files
wxiaoguangSysoev, Vladimir
and
Sysoev, Vladimir
authored
Make tracked time representation display as hours (#33315) (#33334)
Try to backport #33315, the only trivial conflict is in the helper functions map in the helper.go Fix #33333 Co-authored-by: Sysoev, Vladimir <[email protected]>
1 parent 8f45a11 commit eef6355

File tree

8 files changed

+30
-90
lines changed

8 files changed

+30
-90
lines changed

Diff for: models/issues/stopwatch.go

+2-2
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 {

Diff for: modules/templates/helper.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ func NewFuncMap() template.FuncMap {
7070
// time / number / format
7171
"FileSize": base.FileSize,
7272
"CountFmt": base.FormatNumberSI,
73-
"Sec2Time": util.SecToTime,
73+
"Sec2Time": util.SecToHours,
7474

7575
"TimeEstimateString": timeEstimateString,
7676

Diff for: modules/util/sec_to_time.go

+8-51
Original file line numberDiff line numberDiff line change
@@ -8,59 +8,17 @@ 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 stable for planning and managing timesheets.
13+
// Here it only supports hours and minutes, because a work day could contain 6 or 7 or 8 hours.
14+
func SecToHours(durationVal any) string {
1915
duration, _ := ToInt64(durationVal)
20-
21-
formattedTime := ""
22-
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-
36-
// The following three variables are calculated without depending
37-
// on the previous calculated variables.
38-
hours := (duration / 3600) % 24
16+
hours := duration / 3600
3917
minutes := (duration / 60) % 60
40-
seconds := duration % 60
4118

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-
}
19+
formattedTime := ""
20+
formattedTime = formatTime(hours, "hour", formattedTime)
21+
formattedTime = formatTime(minutes, "minute", formattedTime)
6422

6523
// The formatTime() function always appends a space at the end. This will be trimmed
6624
return strings.TrimRight(formattedTime, " ")
@@ -76,6 +34,5 @@ func formatTime(value int64, name, formattedTime string) string {
7634
} else if value > 1 {
7735
formattedTime = fmt.Sprintf("%s%d %ss ", formattedTime, value, name)
7836
}
79-
8037
return formattedTime
8138
}

Diff for: modules/util/sec_to_time_test.go

+8-13
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
}

Diff for: routers/web/repo/issue_timetrack.go

+1-1
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

Diff for: services/convert/issue.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"code.gitea.io/gitea/modules/log"
1717
"code.gitea.io/gitea/modules/setting"
1818
api "code.gitea.io/gitea/modules/structs"
19+
"code.gitea.io/gitea/modules/util"
1920
)
2021

2122
func ToIssue(ctx context.Context, doer *user_model.User, issue *issues_model.Issue) *api.Issue {
@@ -186,7 +187,7 @@ func ToStopWatches(ctx context.Context, sws []*issues_model.Stopwatch) (api.Stop
186187
result = append(result, api.StopWatch{
187188
Created: sw.CreatedUnix.AsTime(),
188189
Seconds: sw.Seconds(),
189-
Duration: sw.Duration(),
190+
Duration: util.SecToHours(sw.Seconds()),
190191
IssueIndex: issue.Index,
191192
IssueTitle: issue.Title,
192193
RepoOwnerName: repo.OwnerName,

Diff for: services/convert/issue_comment.go

+1-1
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 {

Diff for: web_src/js/features/stopwatch.ts

+7-20
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {createTippy} from '../modules/tippy.ts';
22
import {GET} from '../modules/fetch.ts';
3-
import {hideElem, showElem} from '../utils/dom.ts';
3+
import {hideElem, queryElems, showElem} from '../utils/dom.ts';
44
import {logoutFromWorker} from '../modules/worker.ts';
55

66
const {appSubUrl, notificationSettings, enableTimeTracking, assetVersionEncoded} = window.config;
@@ -144,23 +144,10 @@ function updateStopwatchData(data) {
144144
return Boolean(data.length);
145145
}
146146

147-
// TODO: This flickers on page load, we could avoid this by making a custom
148-
// element to render time periods. Feeding a datetime in backend does not work
149-
// when time zone between server and client differs.
150-
function updateStopwatchTime(seconds) {
151-
if (!Number.isFinite(seconds)) return;
152-
const datetime = (new Date(Date.now() - seconds * 1000)).toISOString();
153-
for (const parent of document.querySelectorAll('.header-stopwatch-dot')) {
154-
const existing = parent.querySelector(':scope > relative-time');
155-
if (existing) {
156-
existing.setAttribute('datetime', datetime);
157-
} else {
158-
const el = document.createElement('relative-time');
159-
el.setAttribute('format', 'micro');
160-
el.setAttribute('datetime', datetime);
161-
el.setAttribute('lang', 'en-US');
162-
el.setAttribute('title', ''); // make <relative-time> show no title and therefor no tooltip
163-
parent.append(el);
164-
}
165-
}
147+
// TODO: This flickers on page load, we could avoid this by making a custom element to render time periods.
148+
function updateStopwatchTime(seconds: number) {
149+
const hours = seconds / 3600 || 0;
150+
const minutes = seconds / 60 || 0;
151+
const timeText = hours >= 1 ? `${Math.round(hours)}h` : `${Math.round(minutes)}m`;
152+
queryElems(document, '.header-stopwatch-dot', (el) => el.textContent = timeText);
166153
}

0 commit comments

Comments
 (0)