Skip to content

Commit

Permalink
Add function to load all records sorted in ascending order
Browse files Browse the repository at this point in the history
	- It uses the less argument to RecordFilepaths to do the sorting
  • Loading branch information
rknizzle committed Jun 3, 2021
1 parent 289529f commit 9b537a4
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions core/record.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,31 @@ func (t *Timetrace) loadAllRecords(date time.Time) ([]*Record, error) {
return records, nil
}

func (t *Timetrace) loadAllRecordsSortedAscending(date time.Time) ([]*Record, error) {
dir := t.fs.RecordDirFromDate(date)

recordFilepaths, err := t.fs.RecordFilepaths(dir, func(a, b string) bool {
timeA, _ := time.Parse(recordLayout, a)
timeB, _ := time.Parse(recordLayout, b)
return timeA.Before(timeB)
})
if err != nil {
return nil, err
}

var records []*Record

for _, recordFilepath := range recordFilepaths {
record, err := t.loadRecord(recordFilepath)
if err != nil {
return nil, err
}
records = append(records, record)
}

return records, nil
}

// LoadLatestRecord loads the youngest record. This may also be a record from
// another day. If there is no latest record, nil and no error will be returned.
func (t *Timetrace) LoadLatestRecord() (*Record, error) {
Expand Down

0 comments on commit 9b537a4

Please sign in to comment.