Skip to content

Commit

Permalink
Add BreakTimeToday to the Report and calculate it on Status
Browse files Browse the repository at this point in the history
  • Loading branch information
rknizzle committed Jun 3, 2021
1 parent a17401f commit 14d605e
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions core/timetrace.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"io"
"os"
"sort"
"time"

"github.com/dominikbraun/timetrace/config"
Expand All @@ -20,6 +21,7 @@ type Report struct {
Current *Record
TrackedTimeCurrent *time.Duration
TrackedTimeToday time.Duration
BreakTimeToday time.Duration
}

// Filesystem represents a filesystem used for storing and loading resources.
Expand Down Expand Up @@ -109,8 +111,14 @@ func (t *Timetrace) Status() (*Report, error) {
return nil, err
}

breakTimeToday, err := t.breakTime(now)
if err != nil {
return nil, err
}

report := &Report{
TrackedTimeToday: trackedTimeToday,
BreakTimeToday: breakTimeToday,
}

// If the latest record has been stopped, there is no active time tracking.
Expand All @@ -129,6 +137,26 @@ func (t *Timetrace) Status() (*Report, error) {
return report, nil
}

func (t *Timetrace) breakTime(date time.Time) (time.Duration, error) {
records, err := t.loadAllRecords(date)
if err != nil {
return 0, err
}

// sort the records by start time
sort.Slice(records, func(i, j int) bool {
return records[i].Start.Before(records[j].Start)
})

// add up the time between records
var breakTime time.Duration
for i := 0; i < len(records)-1; i++ {
breakTime += records[i+1].Start.Sub(*records[i].End)
}

return breakTime, nil
}

// Stop stops the time tracking and marks the current record as ended.
func (t *Timetrace) Stop() error {
latestRecord, err := t.LoadLatestRecord()
Expand Down

0 comments on commit 14d605e

Please sign in to comment.