Skip to content
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

feat: Add flag to set the timezone on reports #253

Merged
2 changes: 1 addition & 1 deletion pkg/cmd/time-entry/report/util/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func (rf ReportFlags) Check() error {
// NewReportFlags helps creating a util.ReportFlags for report commands
func NewReportFlags() ReportFlags {
return ReportFlags{
OutputFlags: util.OutputFlags{TimeFormat: timehlp.FullTimeFormat},
OutputFlags: util.OutputFlags{TimeFormat: timehlp.FullTimeFormat, TimeZone: "local"},
}
}

Expand Down
1 change: 1 addition & 0 deletions pkg/cmd/time-entry/report/util/reportwithrange_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,7 @@ func TestReportWithRange(t *testing.T) {
},
flags: func(t *testing.T) util.ReportFlags {
rf := util.NewReportFlags()
rf.TimeZone = "UTC"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add another entry in the table, but with the following format:

rf.Format = "{{.ID}};{{ .TimeInterval.Start.Format " +
	`"2006-01-02 15:04:05"` +
	" }}"

and a offset timezone so we can see the effect here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lucassabreu thanks for the comments, I have some strange bugs with this feature (the date-filling flag), I will try to fix them this week.

rf.FillMissingDates = true
rf.Format = "{{.ID}};{{ .TimeInterval.Start.Format " +
`"2006-01-02"` +
Expand Down
48 changes: 43 additions & 5 deletions pkg/cmd/time-entry/util/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package util

import (
"io"
"time"

"github.com/lucassabreu/clockify-cli/api"
"github.com/lucassabreu/clockify-cli/api/dto"
Expand All @@ -19,20 +20,29 @@ type OutputFlags struct {
Markdown bool
DurationFormatted bool
DurationFloat bool

TimeFormat string
TimeFormat string
TimeZone string
}

func (of OutputFlags) Check() error {
return cmdutil.XorFlag(map[string]bool{

if err := cmdutil.XorFlag(map[string]bool{
"format": of.Format != "",
"json": of.JSON,
"csv": of.CSV,
"quiet": of.Quiet,
"md": of.Markdown,
"duration-float": of.DurationFloat,
"duration-formatted": of.DurationFormatted,
})
}); err != nil {
return err
}

if of.TimeZone != "local" && of.TimeFormat != "" {
_, err := time.LoadLocation(of.TimeZone)
return err
}
return nil
}

// AddPrintMultipleTimeEntriesFlags add flags to print multiple time entries
Expand All @@ -45,6 +55,8 @@ func AddPrintMultipleTimeEntriesFlags(cmd *cobra.Command) {
func AddPrintTimeEntriesFlags(cmd *cobra.Command, of *OutputFlags) {
cmd.Flags().StringVarP(&of.Format, "format", "f", "",
"golang text/template format to be applied on each time entry")
cmd.Flags().StringVarP(&of.TimeZone, "time-zone", "z", "local",
"time zone to be used on the time entries")
cmd.Flags().BoolVarP(&of.JSON, "json", "j", false, "print as JSON")
cmd.Flags().BoolVarP(&of.CSV, "csv", "v", false, "print as CSV")
cmd.Flags().BoolVarP(&of.Quiet, "quiet", "q", false, "print only ID")
Expand Down Expand Up @@ -91,18 +103,44 @@ func PrintTimeEntry(
b := config.GetBool(cmdutil.CONF_SHOW_TOTAL_DURATION)
config.SetBool(cmdutil.CONF_SHOW_TOTAL_DURATION, false)

err := PrintTimeEntries(ts, out, config, of)
err := PrintTimeEntries(updateTimeZone(ts, of), out, config, of)

config.SetBool(cmdutil.CONF_SHOW_TOTAL_DURATION, b)

return err
}

func updateTimeZone(tes []dto.TimeEntry, of OutputFlags) []dto.TimeEntry {

if of.TimeZone == "" {
return tes
}

var loc *time.Location

if of.TimeZone == "local" || of.TimeFormat == "" {
loc = time.Local
} else {
// parses of.TimeZone as a time.Location
loc, _ = time.LoadLocation(of.TimeZone)
}

for i := range tes {
tes[i].TimeInterval.Start = tes[i].TimeInterval.Start.In(loc)
if tes[i].TimeInterval.End != nil {
end := tes[i].TimeInterval.End.In(loc)
tes[i].TimeInterval.End = &end
}
}
return tes
}

// PrintTimeEntries will print out a list of time entries using parameters and
// flags
func PrintTimeEntries(
tes []dto.TimeEntry, out io.Writer, config cmdutil.Config, of OutputFlags,
) error {
tes = updateTimeZone(tes, of)
switch {
case of.Markdown:
return output.TimeEntriesMarkdownPrint(tes, out)
Expand Down