times: format duration as seconds for machine-readable outputs (#168)

times: format duration as seconds for machine-readable outputs

Co-authored-by: Norwin Roosen <git@nroo.de>
Reviewed-on: https://gitea.com/gitea/tea/pulls/168
Reviewed-by: 6543 <6543@noreply.gitea.io>
Reviewed-by: mrsdizzie <info@mrsdizzie.com>
This commit is contained in:
Norwin 2020-08-30 06:28:58 +00:00 committed by Lunny Xiao
parent 25a7e85c1c
commit 9ae7196a50

View File

@ -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),
})
}