# HG changeset patch # User Sascha L. Teichmann # Date 1564062193 -7200 # Node ID 80bdcd137a1dcd8c9a9248951a47eb02eb705004 # Parent cb74aa69954eb2b4e3aecc2f9e0a4c4d56c5411a Parse timezones from time inputs and send timezones in results. diff -r cb74aa69954e -r 80bdcd137a1d pkg/common/time.go --- 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 diff -r cb74aa69954e -r 80bdcd137a1d pkg/controllers/gauges.go --- 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) diff -r cb74aa69954e -r 80bdcd137a1d pkg/controllers/importqueue.go --- 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 } diff -r cb74aa69954e -r 80bdcd137a1d pkg/models/import.go --- 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 {