changeset 3115:524bc6545b20

Simplified code.
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Sun, 28 Apr 2019 21:29:41 +0200
parents c68cef0346b7
children ff0b9a94e0e4
files pkg/controllers/bottlenecks.go
diffstat 1 files changed, 15 insertions(+), 12 deletions(-) [+]
line wrap: on
line diff
--- a/pkg/controllers/bottlenecks.go	Sun Apr 28 15:26:14 2019 +0200
+++ b/pkg/controllers/bottlenecks.go	Sun Apr 28 21:29:41 2019 +0200
@@ -227,6 +227,19 @@
 	return percents
 }
 
+func parseTime(s, what string) (time.Time, error) {
+	var t time.Time
+	var err error
+	if t, err = time.Parse(common.TimeFormat, s); err != nil {
+		return time.Time{}, JSONError{
+			Code: http.StatusBadRequest,
+			Message: fmt.Sprintf(
+				"Invalid time format for '%s' field: %v", what, err),
+		}
+	}
+	return t.UTC(), nil
+}
+
 func bottleneckAvailabilty(
 	_ interface{},
 	req *http.Request,
@@ -245,27 +258,17 @@
 	var from, to time.Time
 
 	if f := req.FormValue("from"); f != "" {
-		if from, err = time.Parse(common.TimeFormat, f); err != nil {
-			err = JSONError{
-				Code:    http.StatusBadRequest,
-				Message: fmt.Sprintf("Invalid time format for 'from' field: %v", err),
-			}
+		if from, err = parseTime(f, "from"); err != nil {
 			return
 		}
-		from = from.UTC()
 	} else {
 		from = time.Now().AddDate(-1, 0, 0).UTC()
 	}
 
 	if t := req.FormValue("to"); t != "" {
-		if to, err = time.Parse(common.TimeFormat, t); err != nil {
-			err = JSONError{
-				Code:    http.StatusBadRequest,
-				Message: fmt.Sprintf("Invalid time format for 'from' field: %v", err),
-			}
+		if to, err = parseTime(t, "to"); err != nil {
 			return
 		}
-		to = to.UTC()
 	} else {
 		to = from.AddDate(1, 0, 0).UTC()
 	}