Skip to content

Commit

Permalink
fix(flextime): fix sign for diffs below one hour
Browse files Browse the repository at this point in the history
The formatting provided the sign from `time.Duration.Hour`. If the hour
is "0", there is no "-" for negative Durations, though.

This commit fixes the issue by handling the signage itself.
  • Loading branch information
aibor committed Jul 16, 2024
1 parent caf34f2 commit 9d0afc3
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 10 deletions.
16 changes: 8 additions & 8 deletions cmd/flextime/main_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ flextime.hours_per_day: 4
{"id":1,"start":"20240630T144010Z","end":"20240630T163943Z"}
]`,
expectedStdout: `
date actual target diff
total 1:59 8:00 -6:00
date actual target diff
total 1h:59m 8h:00m -6h:00m
`,
},
{
Expand All @@ -50,10 +50,10 @@ flextime.hours_per_day: 4
{"id":1,"start":"20240630T144010Z","end":"20240630T163943Z"}
]`,
expectedStdout: `
date actual target diff
2024-06-29 0:00 8:00 -7:59
2024-06-30 1:59 8:00 -6:00
total 1:59 16:00 -14:00
date actual target diff
2024-06-29 0h:00m 8h:00m -7h:59m
2024-06-30 1h:59m 8h:00m -6h:00m
total 1h:59m 16h:00m -14h:00m
`,
},
{
Expand All @@ -64,8 +64,8 @@ flextime.hours_per_day: 4
{"id":3,"start":"20240629T102128Z","end":"20240630T102131Z"}
]`,
expectedStdout: `
date actual target diff
total 0:00 8:00 -8:00
date actual target diff
total 0h:00m 8h:00m -8h:00m
`,
expectedStderr: `debug [flextime] - cfg - DailyTarget: 8h0m0s
debug [flextime] - cfg - Debug: true
Expand Down
11 changes: 9 additions & 2 deletions cmd/flextime/printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,16 @@ import (
const minutesPerHour = 60

func fmtDuration(d time.Duration) string {
var prefix string

if d < 0 {
prefix = "-"
}

return fmt.Sprintf(
"%d:%02d",
int64(d.Hours()),
"%s%dh:%02dm",
prefix,
int64(d.Abs().Hours()),
int64(d.Abs().Minutes())%minutesPerHour,
)
}
Expand Down

0 comments on commit 9d0afc3

Please sign in to comment.