From e8bee60ed342ab4df93bf2fb582706ad7fd42546 Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Sat, 3 Jun 2017 17:25:26 +0200 Subject: Change `TimeEntry.Time` to a `float64` I had forgotten that time spent can be a decimal. Make this field a float and change related code accordingly: * --time flag * `NewTimeEntry()` `time` argument * `buildSubmissionParams()` can't use `strconv.Itoa` as that's an integer function. Instead we use `FormatFloat`. Truncate time parsing to two decimal places because to me that seems like enough. Who's going to say they spent `0.324` hours on something? Also, in order to be able to properly submit values to TimeTask (at least on the edition I use), the times must be written in European/French format, with commas (`,`) as decimal separators. Do a string replace to get this, as the float formatter will give us a period (`.`) separator. --- main.go | 2 +- timetask/http.go | 5 ++++- timetask/time_entry.go | 4 ++-- 3 files changed, 7 insertions(+), 4 deletions(-) diff --git a/main.go b/main.go index 8b3db02..e685d25 100644 --- a/main.go +++ b/main.go @@ -40,7 +40,7 @@ func main() { time_spent := kingpin.Flag("time", "Time spent working on project."). Short('t'). Default("7"). - Int() + Float() date_str := kingpin.Flag("date", "Date when work was done (e.g. 2017-01-31)"). Short('d'). String() diff --git a/timetask/http.go b/timetask/http.go index 6e73276..f236665 100644 --- a/timetask/http.go +++ b/timetask/http.go @@ -5,6 +5,7 @@ import ( "net/http/cookiejar" "net/url" "strconv" + "strings" "golang.org/x/net/publicsuffix" ) @@ -97,9 +98,11 @@ func buildSubmissionParams(time_entry TimeEntry) url.Values { time_entry.Date.Format("02/01/06"), // day/month/year ) + time_str := strconv.FormatFloat(time_entry.Time, 'f', 2, 64) + time_european_format := strings.Replace(time_str, ".", ",", 0) v.Set( "f_time0", - strconv.Itoa(time_entry.Time), + time_european_format, ) var billable string diff --git a/timetask/time_entry.go b/timetask/time_entry.go index ff0ad1f..bb7a741 100644 --- a/timetask/time_entry.go +++ b/timetask/time_entry.go @@ -10,7 +10,7 @@ type TimeEntry struct { Task int WorkType int Date time.Time - Time int + Time float64 Billable bool Description string } @@ -19,7 +19,7 @@ func NewTimeEntry( profile Profile, project Project, date time.Time, - time int, + time float64, description string, ) TimeEntry { return TimeEntry{ -- cgit v1.2.3