Skip to content

Commit

Permalink
refactor: add entry duration splitting as a method
Browse files Browse the repository at this point in the history
  • Loading branch information
gabor-boros committed Oct 13, 2021
1 parent 481eb3b commit 4fbb077
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
8 changes: 8 additions & 0 deletions internal/pkg/worklog/entry.go
Expand Up @@ -2,6 +2,7 @@ package worklog

import (
"fmt"
"math"
"time"
)

Expand Down Expand Up @@ -46,3 +47,10 @@ func (e *Entry) IsComplete() bool {

return isMetadataFilled && isTimeFilled
}

// SplitDuration splits the billable and unbillable duration to N parts.
func (e *Entry) SplitDuration(parts int) (splitBillableDuration time.Duration, splitUnbillableDuration time.Duration) {
splitBillableDuration = time.Duration(math.Round(float64(e.BillableDuration.Nanoseconds()) / float64(parts)))
splitUnbillableDuration = time.Duration(math.Round(float64(e.UnbillableDuration.Nanoseconds()) / float64(parts)))
return splitBillableDuration, splitUnbillableDuration
}
15 changes: 15 additions & 0 deletions internal/pkg/worklog/entry_test.go
Expand Up @@ -88,3 +88,18 @@ func TestEntryIsCompleteIncomplete(t *testing.T) {
entry.UnbillableDuration = 0
assert.False(t, entry.IsComplete())
}

func TestEntry_SplitDuration(t *testing.T) {
var splitBillable time.Duration
var splitUnbillable time.Duration
entry := getTestEntry()

splitBillable, splitUnbillable = entry.SplitDuration(1)
assert.Equal(t, entry.BillableDuration, splitBillable)
assert.Equal(t, entry.UnbillableDuration, splitUnbillable)

entry.UnbillableDuration = time.Hour * 2
splitBillable, splitUnbillable = entry.SplitDuration(2)
assert.Equal(t, time.Hour*1, splitBillable)
assert.Equal(t, time.Hour*1, splitUnbillable)
}

0 comments on commit 4fbb077

Please sign in to comment.