8000 [Issue #36] Remove hard-coded "15:04" strings by aaronsheah · Pull Request #40 · dominikbraun/timetrace · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

[Issue #36] Remove hard-coded "15:04" strings #40

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cli/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func deleteRecordCommand(t *core.Timetrace) *cobra.Command {
return
}

showRecord(record)
showRecord(record, t.Formatter())
if !confirmed {
if !askForConfirmation() {
out.Info("Record NOT deleted.")
Expand Down
9 changes: 4 additions & 5 deletions cli/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,24 +68,23 @@ func getRecordCommand(t *core.Timetrace) *cobra.Command {
return
}

showRecord(record)
showRecord(record, t.Formatter())
},
}

return getRecord
}

func showRecord(record *core.Record) {
func showRecord(record *core.Record, formatter *core.Formatter) {
isBillable := defaultBool

if record.IsBillable {
isBillable = "yes"
}

end := defaultString

if record.End != nil {
end = record.End.Format("15:04")
end = formatter.TimeString(*record.End)
}

project := defaultString
Expand All @@ -96,7 +95,7 @@ func showRecord(record *core.Record) {

rows := [][]string{
{
record.Start.Format("15:04"),
formatter.TimeString(record.Start),
end,
project,
isBillable,
Expand Down
15 changes: 2 additions & 13 deletions cli/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,6 @@ import (
"github.com/spf13/cobra"
)

const (
defaultTimeLayout = "15:04"
)

func listCommand(t *core.Timetrace) *cobra.Command {
list := &cobra.Command{
Use: "list",
Expand Down Expand Up @@ -94,19 +90,12 @@ func listRecordsCommand(t *core.Timetrace) *cobra.Command {
records = filterBillableRecords(records)
}

dateLayout := defaultTimeLayout

if t.Config().Use12Hours {
dateLayout = "03:04PM"
}

rows := make([][]string, len(records))

for i, record := range records {
end := defaultString

if record.End != nil {
end = record.End.Format(dateLayout)
end = t.Formatter().TimeString(*record.End)
}

billable := defaultBool
Expand All @@ -118,7 +107,7 @@ func listRecordsCommand(t *core.Timetrace) *cobra.Command {
rows[i] = make([]string, 5)
rows[i][0] = strconv.Itoa(i + 1)
rows[i][1] = record.Project.Key
rows[i][2] = record.Start.Format(dateLayout)
rows[i][2] = t.Formatter().TimeString(record.Start)
rows[i][3] = end
rows[i][4] = billable
}
Expand Down
23 changes: 23 additions & 0 deletions core/formatter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package core

import "time"

const (
defaultTimeLayout = "15:04"
default12HoursTimeLayout = "03:04PM"
)

type Formatter struct {
use12Hours bool
}

func (f *Formatter) timeLayout() string {
if f.use12Hours {
return default12HoursTimeLayout
}
return defaultTimeLayout
}

func (f *Formatter) TimeString(input time.Time) string {
return input.Format(f.timeLayout())
}
12 changes: 10 additions & 2 deletions core/timetrace.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,18 @@ type Filesystem interface {
}

type Timetrace struct {
config *config.Config
fs Filesystem
config *config.Config
fs Filesystem
formatter *Formatter
}

func New(config *config.Config, fs Filesystem) *Timetrace {
return &Timetrace{
config: config,
fs: fs,
formatter: &Formatter{
use12Hours: config.Use12Hours,
},
}
}

Expand Down Expand Up @@ -147,6 +151,10 @@ func (t *Timetrace) Config() *config.Config {
return t.config
}

func (t *Timetrace) Formatter() *Formatter {
return t.formatter
}

func (t *Timetrace) trackedTime(date time.Time) (time.Duration, error) {
records, err := t.loadAllRecords(date)
if err != nil {
Expand Down
0