changeset 4078:80bdcd137a1d timezone

Parse timezones from time inputs and send timezones in results.
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Thu, 25 Jul 2019 15:43:13 +0200
parents cb74aa69954e
children 2df48e364721
files pkg/common/time.go pkg/controllers/gauges.go pkg/controllers/importqueue.go pkg/models/import.go
diffstat 4 files changed, 21 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- a/pkg/common/time.go	Thu Jul 25 15:14:44 2019 +0200
+++ b/pkg/common/time.go	Thu Jul 25 15:43:13 2019 +0200
@@ -23,13 +23,22 @@
 	// time.RFC3339 equals "simplified ISO format as defined by ECMA-262"
 	//   https://tc39.github.io/ecma262/#sec-date-time-string-format
 	// and "SHOULD be used in new protocols on the Internet." (RFC section 5.6)
-	TimeFormat = time.RFC3339
-	DateFormat = "2006-01-02"
+	TimeFormat           = time.RFC3339
+	TimeFormatMicro      = "2006-01-02T15:04:05.999Z07:00"
+	TimeFormatMicroLocal = "2006-01-02T15:04:05.000"
+	DateFormat           = "2006-01-02"
 )
 
 // TimeParser is a list of time formats.
 type TimeParser []string
 
+var ParseTime = TimeParser{
+	TimeFormat,
+	TimeFormatMicro,
+	TimeFormatMicroLocal,
+	DateFormat,
+}.Parse
+
 var utc0 = time.Unix(0, 0)
 
 // Parse tries to parse a given string by the entries of the layout
--- a/pkg/controllers/gauges.go	Thu Jul 25 15:14:44 2019 +0200
+++ b/pkg/controllers/gauges.go	Thu Jul 25 15:43:13 2019 +0200
@@ -588,7 +588,7 @@
 
 	var when time.Time
 	if w := req.FormValue("when"); w != "" {
-		if when, err = time.Parse(models.ImportTimeFormat, w); err != nil {
+		if when, err = common.ParseTime(w); err != nil {
 			err = JSONError{
 				Code:    http.StatusBadRequest,
 				Message: fmt.Sprintf("error: wrong time format: %v", err),
@@ -690,7 +690,7 @@
 	}
 
 	if from := req.FormValue("from"); from != "" {
-		fromTime, err := time.Parse(models.ImportTimeFormat, from)
+		fromTime, err := common.ParseTime(from)
 		if err != nil {
 			http.Error(
 				rw, fmt.Sprintf("error: Invalid from time: %v", err),
@@ -701,7 +701,7 @@
 	}
 
 	if to := req.FormValue("to"); to != "" {
-		toTime, err := time.Parse(models.ImportTimeFormat, to)
+		toTime, err := common.ParseTime(to)
 		if err != nil {
 			http.Error(
 				rw, fmt.Sprintf("error: Invalid from time: %v", err),
@@ -767,7 +767,7 @@
 			// Too late for an HTTP error code.
 			return
 		}
-		record[0] = measureDate.Format(models.ImportTimeFormat)
+		record[0] = measureDate.Format(common.TimeFormat)
 		record[1] = float64format(waterlevel)
 		record[2] = nullFloat64format(valueMin)
 		record[3] = nullFloat64format(valueMax)
--- a/pkg/controllers/importqueue.go	Thu Jul 25 15:14:44 2019 +0200
+++ b/pkg/controllers/importqueue.go	Thu Jul 25 15:43:13 2019 +0200
@@ -27,6 +27,7 @@
 	"github.com/gorilla/mux"
 
 	"gemma.intevation.de/gemma/pkg/auth"
+	"gemma.intevation.de/gemma/pkg/common"
 	"gemma.intevation.de/gemma/pkg/imports"
 	"gemma.intevation.de/gemma/pkg/models"
 )
@@ -136,7 +137,7 @@
 	}
 
 	if from := req.FormValue("from"); from != "" {
-		fromTime, err := time.Parse(models.ImportTimeFormat, from)
+		fromTime, err := common.ParseTime(from)
 		if err != nil {
 			return nil, nil, nil, err
 		}
@@ -147,7 +148,7 @@
 	}
 
 	if to := req.FormValue("to"); to != "" {
-		toTime, err := time.Parse(models.ImportTimeFormat, to)
+		toTime, err := common.ParseTime(to)
 		if err != nil {
 			return nil, nil, nil, err
 		}
--- a/pkg/models/import.go	Thu Jul 25 15:14:44 2019 +0200
+++ b/pkg/models/import.go	Thu Jul 25 15:43:13 2019 +0200
@@ -17,10 +17,10 @@
 	"encoding/json"
 	"errors"
 	"time"
+
+	"gemma.intevation.de/gemma/pkg/common"
 )
 
-const ImportTimeFormat = "2006-01-02T15:04:05.000"
-
 type (
 	ImportTime struct{ time.Time }
 
@@ -64,7 +64,7 @@
 }
 
 func (it ImportTime) MarshalJSON() ([]byte, error) {
-	return json.Marshal(it.Format(ImportTimeFormat))
+	return json.Marshal(it.Format(common.TimeFormatMicro))
 }
 
 func (it *ImportTime) Scan(x interface{}) error {