Make tracked time representation display as hours (#33315)

Estimated time represented in hours it might be convenient to
have tracked time represented in the same way to be compared and
managed.

---------

Co-authored-by: Sysoev, Vladimir <i@vsysoev.ru>
Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
pull/33204/head^2
Vladimir Sysoev 2025-01-19 04:30:44 +03:00 committed by GitHub
parent f250ee6360
commit dc2308a959
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
8 changed files with 29 additions and 94 deletions

View File

@ -46,11 +46,6 @@ func (s Stopwatch) Seconds() int64 {
return int64(timeutil.TimeStampNow() - s.CreatedUnix) return int64(timeutil.TimeStampNow() - s.CreatedUnix)
} }
// Duration returns a human-readable duration string based on local server time
func (s Stopwatch) Duration() string {
return util.SecToTime(s.Seconds())
}
func getStopwatch(ctx context.Context, userID, issueID int64) (sw *Stopwatch, exists bool, err error) { func getStopwatch(ctx context.Context, userID, issueID int64) (sw *Stopwatch, exists bool, err error) {
sw = new(Stopwatch) sw = new(Stopwatch)
exists, err = db.GetEngine(ctx). exists, err = db.GetEngine(ctx).
@ -201,7 +196,7 @@ func FinishIssueStopwatch(ctx context.Context, user *user_model.User, issue *Iss
Doer: user, Doer: user,
Issue: issue, Issue: issue,
Repo: issue.Repo, Repo: issue.Repo,
Content: util.SecToTime(timediff), Content: util.SecToHours(timediff),
Type: CommentTypeStopTracking, Type: CommentTypeStopTracking,
TimeID: tt.ID, TimeID: tt.ID,
}); err != nil { }); err != nil {

View File

@ -69,7 +69,7 @@ func NewFuncMap() template.FuncMap {
// time / number / format // time / number / format
"FileSize": base.FileSize, "FileSize": base.FileSize,
"CountFmt": countFmt, "CountFmt": countFmt,
"Sec2Time": util.SecToTime, "Sec2Time": util.SecToHours,
"TimeEstimateString": timeEstimateString, "TimeEstimateString": timeEstimateString,

View File

@ -8,59 +8,17 @@ import (
"strings" "strings"
) )
// SecToTime converts an amount of seconds to a human-readable string. E.g. // SecToHours converts an amount of seconds to a human-readable hours string.
// 66s -> 1 minute 6 seconds // This is stable for planning and managing timesheets.
// 52410s -> 14 hours 33 minutes // Here it only supports hours and minutes, because a work day could contain 6 or 7 or 8 hours.
// 563418 -> 6 days 12 hours func SecToHours(durationVal any) string {
// 1563418 -> 2 weeks 4 days
// 3937125s -> 1 month 2 weeks
// 45677465s -> 1 year 6 months
func SecToTime(durationVal any) string {
duration, _ := ToInt64(durationVal) duration, _ := ToInt64(durationVal)
hours := duration / 3600
minutes := (duration / 60) % 60
formattedTime := "" formattedTime := ""
formattedTime = formatTime(hours, "hour", formattedTime)
// The following four variables are calculated by taking formattedTime = formatTime(minutes, "minute", formattedTime)
// into account the previously calculated variables, this avoids
// pitfalls when using remainders. As that could lead to incorrect
// results when the calculated number equals the quotient number.
remainingDays := duration / (60 * 60 * 24)
years := remainingDays / 365
remainingDays -= years * 365
months := remainingDays * 12 / 365
remainingDays -= months * 365 / 12
weeks := remainingDays / 7
remainingDays -= weeks * 7
days := remainingDays
// The following three variables are calculated without depending
// on the previous calculated variables.
hours := (duration / 3600) % 24
minutes := (duration / 60) % 60
seconds := duration % 60
// Extract only the relevant information of the time
// If the time is greater than a year, it makes no sense to display seconds.
switch {
case years > 0:
formattedTime = formatTime(years, "year", formattedTime)
formattedTime = formatTime(months, "month", formattedTime)
case months > 0:
formattedTime = formatTime(months, "month", formattedTime)
formattedTime = formatTime(weeks, "week", formattedTime)
case weeks > 0:
formattedTime = formatTime(weeks, "week", formattedTime)
formattedTime = formatTime(days, "day", formattedTime)
case days > 0:
formattedTime = formatTime(days, "day", formattedTime)
formattedTime = formatTime(hours, "hour", formattedTime)
case hours > 0:
formattedTime = formatTime(hours, "hour", formattedTime)
formattedTime = formatTime(minutes, "minute", formattedTime)
default:
formattedTime = formatTime(minutes, "minute", formattedTime)
formattedTime = formatTime(seconds, "second", formattedTime)
}
// The formatTime() function always appends a space at the end. This will be trimmed // The formatTime() function always appends a space at the end. This will be trimmed
return strings.TrimRight(formattedTime, " ") return strings.TrimRight(formattedTime, " ")
@ -76,6 +34,5 @@ func formatTime(value int64, name, formattedTime string) string {
} else if value > 1 { } else if value > 1 {
formattedTime = fmt.Sprintf("%s%d %ss ", formattedTime, value, name) formattedTime = fmt.Sprintf("%s%d %ss ", formattedTime, value, name)
} }
return formattedTime return formattedTime
} }

View File

@ -9,22 +9,17 @@ import (
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
func TestSecToTime(t *testing.T) { func TestSecToHours(t *testing.T) {
second := int64(1) second := int64(1)
minute := 60 * second minute := 60 * second
hour := 60 * minute hour := 60 * minute
day := 24 * hour day := 24 * hour
year := 365 * day
assert.Equal(t, "1 minute 6 seconds", SecToTime(minute+6*second)) assert.Equal(t, "1 minute", SecToHours(minute+6*second))
assert.Equal(t, "1 hour", SecToTime(hour)) assert.Equal(t, "1 hour", SecToHours(hour))
assert.Equal(t, "1 hour", SecToTime(hour+second)) assert.Equal(t, "1 hour", SecToHours(hour+second))
assert.Equal(t, "14 hours 33 minutes", SecToTime(14*hour+33*minute+30*second)) assert.Equal(t, "14 hours 33 minutes", SecToHours(14*hour+33*minute+30*second))
assert.Equal(t, "6 days 12 hours", SecToTime(6*day+12*hour+30*minute+18*second)) assert.Equal(t, "156 hours 30 minutes", SecToHours(6*day+12*hour+30*minute+18*second))
assert.Equal(t, "2 weeks 4 days", SecToTime((2*7+4)*day+2*hour+16*minute+58*second)) assert.Equal(t, "98 hours 16 minutes", SecToHours(4*day+2*hour+16*minute+58*second))
assert.Equal(t, "4 weeks", SecToTime(4*7*day)) assert.Equal(t, "672 hours", SecToHours(4*7*day))
assert.Equal(t, "4 weeks 1 day", SecToTime((4*7+1)*day))
assert.Equal(t, "1 month 2 weeks", SecToTime((6*7+3)*day+13*hour+38*minute+45*second))
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))
} }

View File

@ -81,7 +81,7 @@ func DeleteTime(c *context.Context) {
return 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.JSONRedirect("") c.JSONRedirect("")
} }

View File

@ -16,6 +16,7 @@ import (
"code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/setting"
api "code.gitea.io/gitea/modules/structs" api "code.gitea.io/gitea/modules/structs"
"code.gitea.io/gitea/modules/util"
) )
func ToIssue(ctx context.Context, doer *user_model.User, issue *issues_model.Issue) *api.Issue { 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
result = append(result, api.StopWatch{ result = append(result, api.StopWatch{
Created: sw.CreatedUnix.AsTime(), Created: sw.CreatedUnix.AsTime(),
Seconds: sw.Seconds(), Seconds: sw.Seconds(),
Duration: sw.Duration(), Duration: util.SecToHours(sw.Seconds()),
IssueIndex: issue.Index, IssueIndex: issue.Index,
IssueTitle: issue.Title, IssueTitle: issue.Title,
RepoOwnerName: repo.OwnerName, RepoOwnerName: repo.OwnerName,

View File

@ -74,7 +74,7 @@ func ToTimelineComment(ctx context.Context, repo *repo_model.Repository, c *issu
c.Content[0] == '|' { c.Content[0] == '|' {
// TimeTracking Comments from v1.21 on store the seconds instead of an formatted string // TimeTracking Comments from v1.21 on store the seconds instead of an formatted string
// so we check for the "|" delimiter and convert new to legacy format on demand // so we check for the "|" delimiter and convert new to legacy format on demand
c.Content = util.SecToTime(c.Content[1:]) c.Content = util.SecToHours(c.Content[1:])
} }
if c.Type == issues_model.CommentTypeChangeTimeEstimate { if c.Type == issues_model.CommentTypeChangeTimeEstimate {

View File

@ -1,6 +1,6 @@
import {createTippy} from '../modules/tippy.ts'; import {createTippy} from '../modules/tippy.ts';
import {GET} from '../modules/fetch.ts'; import {GET} from '../modules/fetch.ts';
import {hideElem, showElem} from '../utils/dom.ts'; import {hideElem, queryElems, showElem} from '../utils/dom.ts';
import {logoutFromWorker} from '../modules/worker.ts'; import {logoutFromWorker} from '../modules/worker.ts';
const {appSubUrl, notificationSettings, enableTimeTracking, assetVersionEncoded} = window.config; const {appSubUrl, notificationSettings, enableTimeTracking, assetVersionEncoded} = window.config;
@ -144,23 +144,10 @@ function updateStopwatchData(data) {
return Boolean(data.length); return Boolean(data.length);
} }
// TODO: This flickers on page load, we could avoid this by making a custom // TODO: This flickers on page load, we could avoid this by making a custom element to render time periods.
// element to render time periods. Feeding a datetime in backend does not work function updateStopwatchTime(seconds: number) {
// when time zone between server and client differs. const hours = seconds / 3600 || 0;
function updateStopwatchTime(seconds) { const minutes = seconds / 60 || 0;
if (!Number.isFinite(seconds)) return; const timeText = hours >= 1 ? `${Math.round(hours)}h` : `${Math.round(minutes)}m`;
const datetime = (new Date(Date.now() - seconds * 1000)).toISOString(); queryElems(document, '.header-stopwatch-dot', (el) => el.textContent = timeText);
for (const parent of document.querySelectorAll('.header-stopwatch-dot')) {
const existing = parent.querySelector(':scope > relative-time');
if (existing) {
existing.setAttribute('datetime', datetime);
} else {
const el = document.createElement('relative-time');
el.setAttribute('format', 'micro');
el.setAttribute('datetime', datetime);
el.setAttribute('lang', 'en-US');
el.setAttribute('title', ''); // make <relative-time> show no title and therefor no tooltip
parent.append(el);
}
}
} }