From 9ae7196a50982c5674e824f671283467ba037ed6 Mon Sep 17 00:00:00 2001 From: Norwin Date: Sun, 30 Aug 2020 06:28:58 +0000 Subject: [PATCH] times: format duration as seconds for machine-readable outputs (#168) times: format duration as seconds for machine-readable outputs Co-authored-by: Norwin Roosen Reviewed-on: https://gitea.com/gitea/tea/pulls/168 Reviewed-by: 6543 <6543@noreply.gitea.io> Reviewed-by: mrsdizzie --- cmd/times.go | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/cmd/times.go b/cmd/times.go index e484111..5bd8b36 100644 --- a/cmd/times.go +++ b/cmd/times.go @@ -101,6 +101,15 @@ func runTrackedTimes(ctx *cli.Context) error { return nil } +func formatDuration(seconds int64, outputType string) string { + switch outputType { + case "yaml": + case "csv": + return fmt.Sprint(seconds) + } + return time.Duration(1e9 * seconds).String() +} + func printTrackedTimes(times []*gitea.TrackedTime, outputType string, from, until time.Time, printTotal bool) { var outputValues [][]string var totalDuration int64 @@ -126,14 +135,14 @@ func printTrackedTimes(times []*gitea.TrackedTime, outputType string, from, unti t.Created.In(localLoc).Format("2006-01-02 15:04:05"), "#" + strconv.FormatInt(t.Issue.Index, 10), t.UserName, - time.Duration(1e9 * t.Time).String(), + formatDuration(t.Time, outputType), }, ) } if printTotal { outputValues = append(outputValues, []string{ - "TOTAL", "", "", time.Duration(1e9 * totalDuration).String(), + "TOTAL", "", "", formatDuration(totalDuration, outputType), }) }