Skip to content

Commit

Permalink
Move Format*Time functions to the formatter package.. fixes #101
Browse files Browse the repository at this point in the history
  • Loading branch information
rknizzle committed Jun 3, 2021
1 parent 6a3d203 commit 3536a41
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 39 deletions.
6 changes: 3 additions & 3 deletions cli/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,15 @@ func statusCommand(t *core.Timetrace) *cobra.Command {
trackedTimeCurrent := defaultString

if report.TrackedTimeCurrent != nil {
trackedTimeCurrent = report.FormatCurrentTime()
trackedTimeCurrent = t.Formatter().FormatCurrentTime(report)
}

rows := [][]string{
{
project,
trackedTimeCurrent,
report.FormatTodayTime(),
report.FormatBreakTime(),
t.Formatter().FormatTodayTime(report),
t.Formatter().FormatBreakTime(report),
},
}
out.Table([]string{"Current project", "Worked since start", "Worked today", "Breaks"}, rows, nil)
Expand Down
36 changes: 36 additions & 0 deletions core/formatter.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package core

import (
"fmt"
"time"
)

Expand Down Expand Up @@ -67,3 +68,38 @@ func (f *Formatter) ParseRecordKey(key string) (time.Time, error) {
func (f *Formatter) RecordKey(record *Record) string {
return record.Start.Format(f.RecordKeyLayout())
}

// FormatTodayTime returns the formated string of the total
// time of today follwoing the format convention
func (f *Formatter) FormatTodayTime(report *Report) string {
return f.formatDuration(report.TrackedTimeToday)
}

// FormatCurrentTime returns the formated string of the current
// report time follwoing the format convention
func (f *Formatter) FormatCurrentTime(report *Report) string {
return f.formatDuration(*report.TrackedTimeCurrent)
}

// FormatBreakTime returns the formated string of the total time
// taking breaks today following the format convention
func (f *Formatter) FormatBreakTime(report *Report) string {
return f.formatDuration(report.BreakTimeToday)
}

// formatDuration formats the passed duration into a string.
// The format will be "8h 24min". If the duration is less then 60 secods
// the format will be "0h 0min 12sec".
func (f *Formatter) formatDuration(duration time.Duration) string {

hours := int64(duration.Hours()) % 60
minutes := int64(duration.Minutes()) % 60
secods := int64(duration.Seconds()) % 60

// as by convention if the duarion is < then 60 secods
// return "0h 0min Xsec"
if hours == 0 && minutes == 0 {
return fmt.Sprintf("0h 0min %dsec", secods)
}
return fmt.Sprintf("%dh %dmin", hours, minutes)
}
36 changes: 0 additions & 36 deletions core/timetrace.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package core

import (
"errors"
"fmt"
"io"
"os"
"time"
Expand Down Expand Up @@ -202,41 +201,6 @@ func (t *Timetrace) trackedTime(date time.Time) (time.Duration, error) {
return trackedTime, nil
}

// FormatTodayTime returns the formated string of the total
// time of today follwoing the format convention
func (report *Report) FormatTodayTime() string {
return formatDuration(report.TrackedTimeToday)
}

// FormatCurrentTime returns the formated string of the current
// report time follwoing the format convention
func (report *Report) FormatCurrentTime() string {
return formatDuration(*report.TrackedTimeCurrent)
}

// FormatBreakTime returns the formated string of the total time
// taking breaks today following the format convention
func (report *Report) FormatBreakTime() string {
return formatDuration(report.BreakTimeToday)
}

// formatDuration formats the passed duration into a string.
// The format will be "8h 24min". If the duration is less then 60 secods
// the format will be "0h 0min 12sec".
func formatDuration(duration time.Duration) string {

hours := int64(duration.Hours()) % 60
minutes := int64(duration.Minutes()) % 60
secods := int64(duration.Seconds()) % 60

// as by convention if the duarion is < then 60 secods
// return "0h 0min Xsec"
if hours == 0 && minutes == 0 {
return fmt.Sprintf("0h 0min %dsec", secods)
}
return fmt.Sprintf("%dh %dmin", hours, minutes)
}

func (t *Timetrace) isDirEmpty(dir string) (bool, error) {
openedDir, err := os.Open(dir)
if err != nil {
Expand Down

0 comments on commit 3536a41

Please sign in to comment.