@@ -2,6 +2,7 @@ package util
2
2
3
3
import (
4
4
"io"
5
+ "time"
5
6
6
7
"github.com/lucassabreu/clockify-cli/api"
7
8
"github.com/lucassabreu/clockify-cli/api/dto"
@@ -19,20 +20,29 @@ type OutputFlags struct {
19
20
Markdown bool
20
21
DurationFormatted bool
21
22
DurationFloat bool
22
-
23
- TimeFormat string
23
+ TimeFormat string
24
+ TimeZone string
24
25
}
25
26
26
27
func (of OutputFlags ) Check () error {
27
- return cmdutil .XorFlag (map [string ]bool {
28
+
29
+ if err := cmdutil .XorFlag (map [string ]bool {
28
30
"format" : of .Format != "" ,
29
31
"json" : of .JSON ,
30
32
"csv" : of .CSV ,
31
33
"quiet" : of .Quiet ,
32
34
"md" : of .Markdown ,
33
35
"duration-float" : of .DurationFloat ,
34
36
"duration-formatted" : of .DurationFormatted ,
35
- })
37
+ }); err != nil {
38
+ return err
39
+ }
40
+
41
+ if of .TimeZone != "local" && of .TimeFormat != "" {
42
+ _ , err := time .LoadLocation (of .TimeZone )
43
+ return err
44
+ }
45
+ return nil
36
46
}
37
47
38
48
// AddPrintMultipleTimeEntriesFlags add flags to print multiple time entries
@@ -45,6 +55,8 @@ func AddPrintMultipleTimeEntriesFlags(cmd *cobra.Command) {
45
55
func AddPrintTimeEntriesFlags (cmd * cobra.Command , of * OutputFlags ) {
46
56
cmd .Flags ().StringVarP (& of .Format , "format" , "f" , "" ,
47
57
"golang text/template format to be applied on each time entry" )
58
+ cmd .Flags ().StringVarP (& of .TimeZone , "time-zone" , "z" , "local" ,
59
+ "time zone to be used on the time entries" )
48
60
cmd .Flags ().BoolVarP (& of .JSON , "json" , "j" , false , "print as JSON" )
49
61
cmd .Flags ().BoolVarP (& of .CSV , "csv" , "v" , false , "print as CSV" )
50
62
cmd .Flags ().BoolVarP (& of .Quiet , "quiet" , "q" , false , "print only ID" )
@@ -91,18 +103,45 @@ func PrintTimeEntry(
91
103
b := config .GetBool (cmdutil .CONF_SHOW_TOTAL_DURATION )
92
104
config .SetBool (cmdutil .CONF_SHOW_TOTAL_DURATION , false )
93
105
94
- err := PrintTimeEntries (ts , out , config , of )
106
+ err := PrintTimeEntries (updateTimeZone ( ts , of ) , out , config , of )
95
107
96
108
config .SetBool (cmdutil .CONF_SHOW_TOTAL_DURATION , b )
97
109
98
110
return err
99
111
}
100
112
113
+ func updateTimeZone (tes []dto.TimeEntry , of OutputFlags ) []dto.TimeEntry {
114
+
115
+ if of .TimeZone == "" {
116
+ return tes
117
+ }
118
+
119
+ var loc * time.Location = time .UTC
120
+
121
+ if of .TimeZone == "local" || of .TimeFormat == "" {
122
+ loc = time .Local
123
+ } else {
124
+ loc , _ = time .LoadLocation (of .TimeZone )
125
+ }
126
+
127
+ // iterate tes and set the end/start time to the timezone
128
+ for i := range tes {
129
+ // parses of.TimeZone as a time.Location
130
+ tes [i ].TimeInterval .Start = tes [i ].TimeInterval .Start .In (loc )
131
+ if tes [i ].TimeInterval .End != nil {
132
+ end := tes [i ].TimeInterval .End .In (loc )
133
+ tes [i ].TimeInterval .End = & end
134
+ }
135
+ }
136
+ return tes
137
+ }
138
+
101
139
// PrintTimeEntries will print out a list of time entries using parameters and
102
140
// flags
103
141
func PrintTimeEntries (
104
142
tes []dto.TimeEntry , out io.Writer , config cmdutil.Config , of OutputFlags ,
105
143
) error {
144
+ tes = updateTimeZone (tes , of )
106
145
switch {
107
146
case of .Markdown :
108
147
return output .TimeEntriesMarkdownPrint (tes , out )
0 commit comments