From 3f6638fc859ae9cdda908214cbb868b058c57466 Mon Sep 17 00:00:00 2001 From: Riyyi Date: Mon, 9 Sep 2024 21:09:56 +0200 Subject: [PATCH] Convert timestamps to UTC time before posting to API --- src/api.go | 22 +++++++++++++++------- src/process.go | 2 +- 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/src/api.go b/src/api.go index e44a6f0..8c38d1b 100644 --- a/src/api.go +++ b/src/api.go @@ -17,22 +17,29 @@ var Api api type api struct {} func (api) CallApi(date string, fromTime string, toTime string, itemID string, description string) error { - if itemID == "break" || itemID == "lunch" || itemID == "pauze" { return nil } + if itemID == "break" || itemID == "lunch" || itemID == "pauze" || itemID == "T1-break" || itemID == "T1-lunch" || itemID == "T1-pauze" { return nil } if date == "" || fromTime == "" || toTime == "" || itemID == "" { return fmt.Errorf("incomplete log entry: %s, %s-%s, %s, %s", date, fromTime, toTime, itemID, description) } - time1, err := time.Parse("15:04", fromTime) + const layout = "2006-01-02 15:04:05" + timestamp1 := date + " " + fromTime + ":00" + timestamp2 := date + " " + toTime + ":00" + + location, err := time.LoadLocation("Local") + if err != nil { + return fmt.Errorf("error loading location: %s", err) + } + + time1, err := time.ParseInLocation(layout, timestamp1, location) if err != nil { return fmt.Errorf("error parsing from_time: %s", err) } - time2, err := time.Parse("15:04", toTime) + time2, err := time.ParseInLocation(layout, timestamp2, location) if err != nil { return fmt.Errorf("error parsing to_time: %s", err) } // Convert local timezone to UTC time - _, offset := time.Now().Zone() - time1.Add(-time.Duration(offset) * time.Second); - time2.Add(-time.Duration(offset) * time.Second); + time1UTC := time1.UTC() duration := time2.Sub(time1) seconds := int(duration.Seconds()) @@ -42,7 +49,8 @@ func (api) CallApi(date string, fromTime string, toTime string, itemID string, d data := map[string]string{ "comment": description, - "started": fmt.Sprintf("%sT%s:00.000+0000", date, fromTime), // "2021-01-17T12:34:00.000+0000", + "started": fmt.Sprintf("%s:00.000+0000", + time1UTC.Format("2006-01-02T15:04")), // "2021-01-17T12:34:00.000+0000", "timeSpentSeconds": strconv.Itoa(seconds), } diff --git a/src/process.go b/src/process.go index 2883032..982fd79 100644 --- a/src/process.go +++ b/src/process.go @@ -92,7 +92,7 @@ func (self *Process) parseTask(line string, line_number int) error { if err != nil { return err } // Set last_time, last_item_id, description - if data[3] == "X" { + if data[3] == "X" || data[3] == "x" { date.last_time = data[1] date.last_item_id = data[2] date.last_description = data[4]